如何在单个Viewpager中多次使用同一片段?

时间:2019-08-21 12:48:20

标签: android-fragments

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    ArrayList<DashboardList> arrayList;

    SectionsPagerAdapter(FragmentManager fm, ArrayList<DashboardList> arrayList) {
        super(fm);
        this.arrayList = arrayList;
    }

    public Fragment getActiveFragment(FragmentManager fm, ViewPager container, int position) {
        String name = makeFragmentName(container.getId(), position);
        return fm.findFragmentByTag(name);
    }

    private String makeFragmentName(int viewId, int index) {
        return "android:switcher:" + viewId + ":" + index;
    }


    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).

        String DashboardID = arrayList.get(position).dashboardlistid;
        switch (DashboardID) {

            default:
                return DashboardDataV_2.newInstance(arrayList.get(position).dashboardlistid);
        }
    }

    @Override
    public int getCount() {
        return arrayList.size() > 0 ? arrayList.size() : 0;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return arrayList.get(position).dashboardname;
    }

}

当我在viewpager中更改页面时,将创建单个片段并替换所有数据。每次用户滑动时如何创建片段的新实例?

0 个答案:

没有答案