具有不同片段类的无限FragmentStatePagerAdapter

时间:2016-08-22 17:47:14

标签: android android-fragments android-viewpager infinite-scroll fragmentstatepageradapter

我在项目中遇到了一个奇怪的问题。

我使用FragmentStatePagerAdapter实现了无限的viewpager适配器

相同的代码是:

public class DashBoardAdapter extends FragmentStatePagerAdapter {

    ArrayList<AbstractFragment> fragments;

    private int MAXFRAGMENTS=0;

    public DashBoardAdapter(FragmentManager fm, ArrayList<AbstractFragment> fragments) {
        super(fm);
        this.fragments = fragments;
        MAXFRAGMENTS=fragments.size();
    }

    @Override
    public AbstractFragment getItem(int position) {
        return fragments.get(position%MAXFRAGMENTS);
    }


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

    @Override
    public CharSequence getPageTitle(int position) {
        return fragments.get(position%MAXFRAGMENTS).getTitle();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        int virtualPosition = position % MAXFRAGMENTS;
        return super.instantiateItem(container, virtualPosition);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        int virtualPosition = position % MAXFRAGMENTS;
        super.destroyItem(container, virtualPosition, object);
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        MAXFRAGMENTS = fragments.size();
    }
}

Activity代码(仅限函数)是这样的:

public void initializeViewPager()
    {

        verticalViewPager = (VerticalViewPager) findViewById(R.id.verticalviewpager);
        enabledPages=new ArrayList<>();
        enabledPages.addAll(getEnabledPages(false));
        adapter=new DashBoardAdapter(getSupportFragmentManager(),enabledPages);
        verticalViewPager.setAdapter(adapter);
        verticalViewPager.setPageTransformer(true, new PageTransformer());
        verticalViewPager.setCurrentItem(999+position);
        verticalViewPager.setOffscreenPageLimit(1);

        verticalViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(adapter!=null)
                    title.setText(adapter.getPageTitle(position));
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private ArrayList<AbstractFragment> getEnabledPages(boolean a)
    {
        ArrayList<AbstractFragment> fragments=new ArrayList<>();
        fragments.add(FragmentAttendance.newInstance(fragments.size()));
        fragments.add(FragmentCompensation.newInstance(fragments.size()));
        if(a)
            fragments.add(FragmentTimeOff.newInstance(fragments.size()));
        if(a)
         fragments.add(FragmentCalendar.newInstance(fragments.size()));
        if(a)
            fragments.add(FragmentPeople.newInstance(fragments.size()));
        if(a)
            fragments.add(FragmentProvisions.newInstance(fragments.size()));

        position = fragments.size()-(999+position)%fragments.size();

        return fragments;
    }

活动布局仅包含Viewpager

我使用的碎片只是下面片段的副本,具有不同的名称

public class FragmentAttendance extends AbstractFragment {
    public static final String ARG_SECTION_NUMBER = "section_number";
    String title ="Testing";
    public static FragmentAttendance newInstance(int sectionNumber) {
        FragmentAttendance fragment = new FragmentAttendance();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public FragmentAttendance() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.card_attendance, container, false);

        return rootView;
    }

    @Override
    public String getTitle() {
        return title;
    }
}

片段的布局只有LinearLayoutTextView

如果我添加超过三个片段的片段,代码运行极好。无论何时添加的片段数量低于4。我的代码以奇怪的行为工作。

有些错误是Fragment already initialised 其他是向前滚动显示fragement视图滚动backward No view appears时无论如何。

可以通过将true传递给getEnabledPages(true)来测试工作代码; 并且可以通过将false传递给getEnabledPages(false);

来测试非工作代码

请有人帮我解决这个奇怪的行为。

0 个答案:

没有答案