App>Fragment>Fragment Hide Show
先把fragment定义在xml文件中
<fragment android:name="com.example.android.apis.app.FragmentHideShow$SecondFragment"
android:id="@+id/fragment2"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="wrap_content" />然后通过fragmentManager通过id去找
FragmentManager fm = getFragmentManager();
fm.findFragmentById(R.id.fragment1)再然后去设置定制动画效果:通过一个button的点击事件去设置fragment的显示和隐藏效果。
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("Hide");
} else {
ft.hide(fragment);
button.setText("Show");
}
ft.commit();
}
});关于fragment的操作都要使用事务去操作。