Android: FragmentPagerAdapter: getItem method called twice on First time

前端 未结 5 1470
一整个雨季
一整个雨季 2020-12-15 04:44

In My application, I have used the ViewPager. Like,

(say main.xml)



        
5条回答
  •  鱼传尺愫
    2020-12-15 05:07

    The FragmentPagerAdapter instantiates 2 Fragments on start, for index 0 and for index 1. If you want to get data from the Fragment which is on the screen, you can use setOnPageChangeListener for the Pager to get current position. Then have SparseArray with WeakReference to your fragments. Update that array in the getItem call. When onPageSelected gets called use that position to get reference to right Fragment and update User data.

    Initialization of array:

    private SparseArray> mFragments = new SparseArray>(3);
    

提交回复
热议问题