通知ViewPager它采用FragmentPagerAdapter通知、ViewPager、FragmentPagerAdapter

2023-09-07 13:05:17 作者:快到锅里来

我想实现一个ViewPager它采用片段,并且可以在一个curcular运动,例如可以刷卡佩奇(A< - > B< - > C< - > A)。 我读了几个帖子就如何做到这一点,例如:返回多少内容有假计数和设定在一开始在中间的位置。 如何打造循环viewpager?

I would like to implement a ViewPager which uses Fragments and can be swiped in a curcular motion e.g. Page (A<-->B<-->C<-->A). I have read a couple of posts on how this is done, e.g. returning a fake count of how many elements there are and setting the position at the start in the middle. how to create circular viewpager?

这些似乎都基于PagerAdapter的。当我尝试做类似的事情,同时延长FragmentPagerAdapter,当我返回时,我通过我的片段轻扫我得到一个异常页面fakeCount,我只有2个片段。 例外:java.lang.IllegalStateException:不能改变片段的标签。

These all seem to be based of a PagerAdapter. When I try to do a similar thing while extending FragmentPagerAdapter, as soon as I return a fakeCount of pages I get an exception when I Swipe through my Fragments, I only have 2 Fragments. Exception: java.lang.IllegalStateException: Can't change tag of fragment.

我认为这是由于作为FragmentManager认为我是2号位,但位置2点,该片段在位置0有谁知道我怎么能避免这种情况?我想我应该尝试扩展Fragmentmanager。任何与此示例或帮助将是很大的AP preciated。

I think this is caused as the FragmentManager thinks I am in position 2 but position 2 points to the fragment at position 0. Does anyone know how I can avoid this? I am thinking I should experiment with extending Fragmentmanager. Any examples or help with this would be greatly appreciated.

推荐答案

我知道这是一个有点晚了,但是这是怎么为我工作:

I know it is a bit late but this is how it worked for me:

我需要3个片段之间的循环刷卡,所以我做了这3个,另外两个虚拟帮我实现的页面循环:

I needed a circular swipe between 3 fragments, so I made those 3 and two more virtual to help me implement the page looping:

public static class FirstViewFragment extends Fragment {
    // Empty Constructor
    public FirstViewFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_landing_1, container, false);
    }
}

public static class SecondViewFragment extends Fragment {
    // Empty Constructor
    public SecondViewFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_landing_2, container, false);
    }
}

public static class ThirdViewFragment extends Fragment {
    // Empty Constructor
    public ThirdViewFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_landing_3, container, false);
    }
}

和两个这使我的虚拟片段刷卡从第一和右侧从上离开了。第一个虚拟膨胀相同的布局,最后的实际和最后一个虚拟相同的布局作为第一个实际的:

And two more virtual fragments that enabled me to swipe left from the first and right from the last. The first virtual inflates the same layout as the last actual and the last virtual the same layout as the first actual:

public static class StartVirtualFragment extends Fragment {
    public StartVirtualFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_landing_3, container, false);
    }
}

public static class EndVirtualFragment extends Fragment {
    public EndVirtualFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_landing_1, container, false);
    }
}

我的适配器:

My Adapter:

private class ViewPagerAdapter extends FragmentPagerAdapter {

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0:
                return new StartVirtualFragment();
            case 1:
                if (firstViewFragment == null) {
                    firstViewFragment = new FirstViewFragment();
                }
                return firstViewFragment;
            case 2:
                if (secondViewFragment == null) {
                    secondViewFragment = new SecondViewFragment();
                }
                return secondViewFragment;
            case 3:
                if (thirdViewFragment == null) {
                    thirdViewFragment = new ThirdViewFragment();
                }
                return thirdViewFragment;
            case 4:
                return new EndVirtualFragment();
        }
        return null;
    }

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

和我的网页监听器我用onPageScrollStateChanged设置正确的页面,并实现循环:

And my page listener I used the onPageScrollStateChanged to set the correct page and implement the loop:

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_DRAGGING) {
                int pageCount = viewPager.getChildCount();
                int currentItem = viewPager.getCurrentItem();
                if (currentItem == 0) {
                    viewPager.setCurrentItem(pageCount - 2, false);
                } else if (currentItem == pageCount - 1) {
                    viewPager.setCurrentItem(1, false);
                }
            }
        }
    });

和中底:

viewPager.setCurrentItem(1);

希望我帮