Use Single Fragment in Multiple tabs of ViewPager

前端 未结 3 1463
粉色の甜心
粉色の甜心 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条回答
  • 2020-12-16 08:52

    I had a smaller issue, I was using a single fragment with multiple TabLayout. I kept getting the wrong position in my fragment which was the last position in the TabLayout.

    The problem is saving fragment position in a static variable or in Bundle will save the last created fragment and the ViewPager will create fragment ahead of the current position so that is why we're losing the current position.

    So what I've done to solve this issue is creating a local variable holding the tab position of the created fragment.

    The code in Kotlin

    class TabFragment : Fragment() {
    private var tabPosition = 0
    
    companion object {
        fun newInstance(tabPosition: Int): TabFragment {
            val tabFragment = TabFragment()
            tabFragment.tabPosition = tabPosition
            return tabFragment
         }
      }
    }
    

    The code in Java

    public class TabFragment extends Fragment {
    private int tabPosition = 0;
    static TabFragment newInstance(int tabPosition){
        TabFragment tabFragment = new TabFragment();
        tabFragment.tabPosition = tabPosition;
        return tabFragment;
      }
    }
    
    0 讨论(0)
  • 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<ProductInfo>) 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.

    0 讨论(0)
  • 2020-12-16 09:03

    In PagerAdapter edit as below, it might help:

    @Override
    public Fragment getItem(int position) {
       /* ProductFragment pf = ProductFragment.newInstance(data.get(position),position);
        return pf;*/
    
        return ProductFragment.newInstance(data.get(position),position);
    }
    

    And in your Fragment make changes as below:

    public class ProductFragment extends Fragment {
    private static final String ARG_PRODUCTS = "PRODS";
    private static List<ProductInfo> allProducts;
    int position = 0;
    RecyclerView prodList;
    ProductAdapter productAdapter=null;
    
    
    public static ProductFragment newInstance(List<ProductInfo> products,int position) {        
    ProductFragment fragment = new ProductFragment();
    Bundle args = new Bundle();
    args.putParcelableArrayList(ARG_PRODUCTS, (ArrayList<ProductInfo>) products);
    args.putInt("KEY_POSITION",position);
    args.putInt("KEY_ID",id);
    fragment.setArguments(args);
    return fragment;
    }
    
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
      if(isVisibleToUser){
          productAdapter.notifyDataSetChanged();
        }
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //if(isVisibleToUser){
        if (getArguments() != null) {
            allProducts = getArguments().getParcelableArrayList(ARG_PRODUCTS);
            this.position = getArguments().getInt("KEY_POSITION");
        }
    

    // } }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_product, container, false);
    prodList = (RecyclerView) view.findViewById(R.id.product_list);
    prodList.setLayoutManager(new LinearLayoutManager(getActivity()));
    productAdapter= new ProductAdapter(getActivity(), allProducts)
    prodList.setAdapter(productAdapter);
            return view;
       }
    }
    
    0 讨论(0)
提交回复
热议问题