1. 引言:
Honeycomb3.0中,在Fragment添加或移除时为其添加画动效果。
2. 功能实现:
(1) 主布局(main.xml)实现:
<LinearLayout
android:orientation = "horizontal"
android:padding = "4dip"
android:gravity = "center_vertical"
android:layout_weight = "1"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
>
<Button
android:id = "@+id/firstButton"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Hide"
/>
<fragment
android:name = "com.flora.FragmentAnimationActivity$FirstFragment"
android:id = "@+id/firstFragment"
android:layout_weight = "1"
android:layout_width = "0px"
android:layout_height = "wrap_content"
/>
</LinearLayout>
<LinearLayout
android:orientation = "horizontal"
android:padding = "4dip"
android:gravity = "center_vertical"
android:layout_weight = "1"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
>
<Button
android:id = "@+id/secondButton"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Hide"
/>
<fragment
android:name = "com.flora.FragmentAnimationActivity$SecondFragment"
android:id = "@+id/secondFragment"
android:layout_weight = "1"
android:layout_width = "0px"
android:layout_height = "wrap_content"
/>
</LinearLayout>
</LinearLayout>
(2) FirstFragment布局(first_fragment.xml)实现:
(3) SecondFragment布局(second_fragment.xml)实现:
<TextView
android:id = "@+id/msg"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_weight = "0"
android:paddingBottom = "4dip"
/>
</LinearLayout>
(4) 主Activity实现:
setContentView(R.layout.main);
FragmentManager fm = getFragmentManager();
addListener(R.id.firstButton, fm.findFragmentById(R.id.firstFragment));
addListener(R.id.secondButton, fm.findFragmentById(R.id.secondFragment));
}
private void addListener(int buttonID, final Fragment fragment) {
final Button button = (Button)findViewById(buttonID);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
if (fragment.isHidden()) {
ft.show(fragment);
button.setText("隐藏");
} else {
ft.hide(fragment);
button.setText("显示");
}
ft.commit();
}
});
}
public static class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_fragment, container, false);
TextView tv = (TextView)v.findViewById(R.id.msg);
tv.setText("This is first fragment.");
tv.setBackgroundColor(Color.GREEN);
return v;
}
}
public static class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.second_fragment, container, false);
TextView tv = (TextView)v.findViewById(R.id.msg);
tv.setText("This is second fragment.");
tv.setBackgroundColor(Color.RED);
return v;
}
}
}