Use Single Fragment in Multiple tabs of ViewPager

前端 未结 3 1468
粉色の甜心
粉色の甜心 2020-12-16 08:39

I am using ViewPager to slide left and right, I have also added the tabs, The number of tabs is depends on the server data, So, I cannot make the number of tabs as Fixed. To

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 08:57

    change this line in : PagerAdapter.java

            @Override
            public Fragment getItem(int position) {
              ProductFragment fragment = new ProductFragment();
              Bundle args = new Bundle();
              args.putParcelableArrayList(ARG_PRODUCTS, (ArrayList) products);
              args.putInt("KEY_POSITION",position);
              args.putInt("KEY_ID",id);
              fragment.setArguments(args);
              return fragment;
            }
    

    and remove static method from fragment as :

    public static ProductFragment newInstance()
    

    bcoz this static method set data of the latest page from viewPager. and FYI viewPager load next page in memory in advance. and static method makes it show now rather then show in future.

提交回复
热议问题