FragmentStatePagerAdapter有一个片段

时间:2015-12-13 13:48:54

标签: android android-fragments fragmentstatepageradapter

我有一个FragmentStatePagerAdapter,我希望多次显示一个相同的片段。因为这个片段包含很多视图我正在寻找一种方法,每次用户滑动时都不会创建该片段我只是想重新加载数据。 我试图实现的是从我的数据库中按天显示数据。问题在于我的适配器代码

public class MainViewPagerAdapter extends FragmentStatePagerAdapter {

    private String[] titles;

    private int currentPossition;

    public MainViewPagerAdapter(final FragmentManager fragManager, final String[] titles) {
        super(fragManager);
        this.titles = titles;
    }
    private static final int MAX_ITEMS = 400;
    @Override
    public Fragment getItem(final int position) {
        Calendar cal = Calendar.getInstance();

        cal.setTimeInMillis(System.currentTimeMillis());
        if (position < 200) {
            cal.add(Calendar.DAY_OF_YEAR, -1);
        } else if (position > 200) {
            cal.add(Calendar.DAY_OF_YEAR, 1);
        }
        return HomeFragment.getInstance(cal.getTimeInMillis());
    }

    /** Return the max items */
    @Override
    public int getCount() {
        return MAX_ITEMS;
    }

    @Override
    public CharSequence getPageTitle(final int position) {
        String title = "";
        if (position < currentPossition) {
            title = titles[0];
        } else if (position == currentPossition) {
            title = titles[1];
        } else { // else if (position > currentPossition
            title = titles[2];
        }
        return title;
    }

    /** Set the titles */
    public void setTitles(final String[] titles) {
        this.titles = titles;
    }

    /** Set the current position */
    public void setCurrentPossition(final int currentPossition) {
        this.currentPossition = currentPossition;
    }

    @Override
    public int getItemPosition(Object object) {
        if (object instanceof HomeFragment) {
            ((HomeFragment) object).changeMainScreen(DateManager.INSTANCE.getDateSetted());
        }
        return super.getItemPosition(object);
    }
}

为了完整性,这里是调用我的适配器的类

public class MainScreenHolder extends Fragment {

    private static Resources res;

    private static final int START_POINT = 200;

    public static ViewPager pager;

    private MainViewPagerAdapter adapter;

    private static String[] titles = new String[3];

    private static int currentPosition = START_POINT;

    /** Creates and returns the view hierarchy associated with the fragment. */
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
            final Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.food_tabs_container, container, false);
        pager = (ViewPager) view.findViewById(R.id.pager);
        pager.setId(R.id.pager);

        titles = initTitles(0);
        adapter = new MainViewPagerAdapter(getChildFragmentManager(), titles);
        adapter.setCurrentPossition(START_POINT);
        pager.setAdapter(adapter);
        pager.setCurrentItem(START_POINT);

        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(final int position) {
                final OnRefreshListener fragment = (OnRefreshListener) adapter.instantiateItem(pager,
                        position);

                adapter.setCurrentPossition(position);
                adapter.setTitles(setTitles(position));

                if (fragment != null) {
                    fragment.onRefresh();
                }
            }

            @Override
            public void onPageScrollStateChanged(final int state) {
                // nothing
            }

            @Override
            public void onPageScrolled(final int position, final float positionOffset,
                    final int positionOffsetPixels) {
                // nothing
            }
        });
        return view;
    }

    /** Called to do initial creation of a fragment */
    @Override
    public void onCreate(final Bundle savedState) {
        super.onCreate(savedState);
        res = getResources();
    }

    private String[] setTitles(final int position) {
        if (currentPosition < position) {
            titles[0] = titles[1];
            titles[1] = titles[2];
            DateManager.INSTANCE.addDays(2);
            titles[2] = Utility.dateAsString(DateManager.INSTANCE.getDateSettedCal(), res);
            DateManager.INSTANCE.addDays(-1);
        } else if (currentPosition > position) {
            DateManager.INSTANCE.addDays(-2);
            titles[2] = titles[1];
            titles[1] = titles[0];
            titles[0] = Utility.dateAsString(DateManager.INSTANCE.getDateSettedCal(), res);
            DateManager.INSTANCE.addDays(1);
        }
        currentPosition = position;
        return titles;
    }

    /** Set initial value for titles */
    public static String[] initTitles(final int offset) {
        titles[1] = Utility.dateAsString(DateManager.INSTANCE.getDateSettedCal(), res);
        DateManager.INSTANCE.addDays(1);
        titles[2] = Utility.dateAsString(DateManager.INSTANCE.getDateSettedCal(), res);
        DateManager.INSTANCE.addDays(-2);
        titles[0] = Utility.dateAsString(DateManager.INSTANCE.getDateSettedCal(), res);
        DateManager.INSTANCE.addDays(1);
        currentPosition = START_POINT + offset;
        return titles;
    }
}

注意:我的代码有效,但我希望提高性能 我的起始位置为200,因此用户可以向后滑动200天,向前滑动200天 滑动发生时,是否有一种方法不能反复创建新片段。

1 个答案:

答案 0 :(得分:0)

我从未使用过FragmentStatePagerAdapter,但我对这个问题很感兴趣。 试试这个,覆盖instantiateItem中的方法MainViewPagerAdapter()来控制返回的视图或片段,在你的情况下。 示例代码:

@Override
public Object instantiateItem(ViewGroup container, int position) {
   return HomeFragment;
}

我假设对象HomeFragment已经定义,因为我没有看到它的代码。