使用片段的最佳方式

时间:2014-09-11 13:38:39

标签: java android android-fragments

问题描述

在我的应用程序中,我使用包含Fragments的选项卡,为此我使用FragmentPagerAdapter,其中函数getItem(int position)总是创建我的Fragment的新实例并返回它。

问题

使用Fragment Adapter Version 1或Version 2的最佳方法是什么?

源代码

FragmentFavorites.java

public class FragmentFavorites extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        /** Gets Fragmnet view. */
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        Logger.d("onCreateView(FragmentFavorites)");
        return rootView;
    }
};

FragmentCategories.java

public class FragmentCategories extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        /** Gets Fragmnet view. */
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        Logger.d("onCreateView(FragmentCategories)");
        return rootView;
    }
};

FragmentHome.java

public class FragmentHome extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        /** Gets Fragmnet view. */
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        Logger.d("onCreateView(FragmentHome)");
        return rootView;
    }
};

FragmentFactory.java

public class FragmentFactory {

    private static final int F_FAVORITES_INDEX = 0;
    private static final int F_HOME_INDEX      = 1;
    private static final int F_CATEGORY_INDEX  = 2;

    /** Keeps the fragments which are put in the tabs of the main screen. */
    private static final ArrayList<Fragment> fragmentsList = new ArrayList<Fragment>();

    static {
        fragmentsList.add(F_FAVORITES_INDEX, new FragmentFavorites());
        fragmentsList.add(F_HOME_INDEX, new FragmentHome());
        fragmentsList.add(F_CATEGORY_INDEX, new FragmentCategories());
    }

    public static int getCount() {
        return fragmentsList.size();
    }

    public static Fragment getFragment(int index) {
        return fragmentsList.get(index);
    }
};

SectionsPagerAdapter.java(版本1)

public class SectionsPagerAdapter extends FragmentPagerAdapter {

        @Override
        public Fragment getItem(int position) {
            return FragmentFactory.getFragment(position);
        }

        @Override
        public int getCount() {
            return FragmentFactory.getCount();
        }
}

SectionsPagerAdapter.java(第2版)

public class SectionsPagerAdapter extends FragmentPagerAdapter {

        @Override
        public Fragment getItem(int position) {
            switch(position) {
              case 0: return new FragmentFavorites();
              case 1: return new FragmentHome();
              case 2: return new FragmentCategories();
            }
        }

        @Override
        public int getCount() {
            return 3;
        }
}

2 个答案:

答案 0 :(得分:0)

只有在需要创建项目时才会调用适配器getItem()。如果片段已经在内存中,则将使用先前创建的实例。

默认情况下,视图寻呼机还会根据屏幕外页面限制设置预先实例化下一个和之前的片段。

您不应该在数组中存储大量片段。这可以防止它们被垃圾收集并占用内存等资源。同时保持片段引用不会阻止调用片段生命周期方法。

如果你想增加内存中保存的碎片数量,只需使用更大的屏幕外页面限制。

答案 1 :(得分:0)

您应该将FragmentPagerAdapter与ViewPager一起使用,因此我们具有友好的滑动功能。此外,视图寻呼机会自动缓存片段,而无需每次都创建新实例。我们可以使用ViewPager.setOffScreenLimit()

设置要缓存的片段数量
相关问题