The correct answer upstairs is actually a viewPager. Just write a fragmentStatePagerAdapter and put your list in it. This can control the number of saved fragments and create new fragments.
Suppose there are three fragments, namely f1, f2, and f3. Then when I initialize the PagerAdapter of ViewPager, a total of 5 fragments are returned. The order is: f3,f1,f2,f3,f1. The default currentItem is 1, which is f1. Then customize ViewPager.OnPageChangeListener. In the onPageScrollStateChanged method, when the state is IDLE (stopped), if the current currentItem is 0, use setCurrentItem(4, false). If it is 4, use setCurrentItem(1, false).
I believe that smart students will understand my implementation logic.
It would be better if this is not just
ViewPager + Fragment
的实现么,只不过他要实现无限滑动的功能罢了。传值还是在不同的Fragment
中传值,当然能优化成一个Fragment
.The correct answer upstairs is actually a viewPager. Just write a fragmentStatePagerAdapter and put your list in it. This can control the number of saved fragments and create new fragments.
I have done this before, and the implementation method is
ViewPager
+Fragment
. The actual content and some details depend on the application logic.My approach is:
Suppose there are three fragments, namely f1, f2, and f3. Then when I initialize the PagerAdapter of ViewPager, a total of 5 fragments are returned. The order is: f3,f1,f2,f3,f1. The default currentItem is 1, which is f1.
Then customize ViewPager.OnPageChangeListener. In the onPageScrollStateChanged method, when the state is IDLE (stopped), if the current currentItem is 0, use setCurrentItem(4, false). If it is 4, use setCurrentItem(1, false).
I believe that smart students will understand my implementation logic.