Android — Fragment注意事项 – 我爱物联网

ViewPager+Fragment                                                                

让Fragment成为ViewPager的一页时,FragmentManager会一直保存管理创建好了的Fragment,即使当前不是显示的这一页,Fragment对象也不会被销毁,在后台默默等待重新显示。但如果Fragment不再可见时,它的视图层次会被销毁掉,下次显示时视图会重新创建。

出于使用FragmentPagerAdapter  时,Fragment对象会一直存留在内存中,所以当有大量的显示页时,就不适合用FragmentPagerAdapter 了,FragmentPagerAdapter  适用于只有少数的page情况,像选项卡。这个时候你可以考虑使用FragmentStatePagerAdapter ,当使用FragmentStatePagerAdapter  时,如果Fragment不显示,那么Fragment对象会被销毁,但在回调onDestroy()方法之前会回调onSaveInstanceState(Bundle outState)方法来保存Fragment的状态,下次Fragment显示时通过onCreate(Bundle savedInstanceState)把存储的状态值取出来,FragmentStatePagerAdapter  比较适合页面比较多的情况,像一个页面的ListView。

动画                                                                                    

1 FragmentTransaction ft = getFragmentManager().beginTransaction();
2 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
3 ft.replace(R.id.details, detail);
4 ft.addToBackStack(null);
5 ft.commit();

代码设置的fade(渐变)效果。

1 FragmentTransaction ft = getFragmentManager().beginTransaction();
2 //setCustomAnimations()必须位于replace()之前,否则效果不起所中。它的两个参数分别为enter,exit的效果。系统目前提供两个效果,分别为android.R.animator.fade_in和android.R.animator.fade_out
3 ft.setCustomAnimations(R.animator.slide_in_left,R.animator.slide_out_right);
4 ft.addToBackStack(null);
5 ft.replace(R.id.details,”detail”);
6 ft.commit();

利用setCustomAnimations()

无参构造函数&传参数                                                                

fragment一定要有一个无参构造函数,如果要给fragment传参,一定要使用Bundle方式传参,而不重载构造函数传参,因为在fragment重新生成的使用不会执行这个带参构造函数,而是执行无参构造函数。

1 public static VechileFrag newInstance(Vehicle vehicle, String userId, boolean isAdd) {
2 VechileFrag mf = new VechileFrag();
3 Bundle args = new Bundle();
4 args.putString(“userId”, userId);
5 args.putBoolean(“isAdd”, isAdd);
6 args.putParcelable(“vehicle”, vehicle);
7 mf.setArguments(args);
8 return mf;
9 }
10
11
12 @Override
13 public void onCreate(Bundle savedInstanceState) {
14 super.onCreate(savedInstanceState);
15 Bundle args = getArguments();
16 if (args != null) {
17 userId = args.getString(“userId”);
18 isAdd = args.getBoolean(“isAdd”);
19 vehicle = args.getParcelable(“vehicle”);
20 if (vehicle == null) {
21 vehicle = new Vehicle();
22 }
23 }
24 }

Menu                                                                                   

1 @Override
2 public void onCreate(Bundle savedInstanceState) {
3 super.onCreate(savedInstanceState);
4 setHasOptionsMenu(true);
5 }

这样之后,就可以调用onCreateOptionsMenu了。

保存Fragment的切换                                                                 

Fragment fragment = fragments.get(i);
if(!fragment.isAdded()) {
if(currentIndex!=-1)
fragments.get(currentIndex).onPause();
ft
= fm.beginTransaction();
ft.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.add(R.id.framelayout,fragment);
// ft.addToBackStack(null);
ft.commit();
currentIndex
=i;
}
else
{
ft
= fm.beginTransaction();
fragments.get(currentIndex).onPause();
ft.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.hide(fragments.get(currentIndex));
ft.show(fragments.get(i));
fragments.get(i).onStart();
ft.commit();
currentIndex
=i;
}

 

我是天王盖地虎的分割线                                                             

本文链接:Android — Fragment注意事项,转载请注明。



You must enable javascript to see captcha here!

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress

无觅相关文章插件,快速提升流量