Search code examples
javaandroidandroid-viewpager

Viewpager with diffrent page number


My app has 10 items on home screen. Each of them has sub-categories that are different (say item1 his 5, item8 his 3) my question is about the best practice to implement such a problem. what I've already tried is using a fragment page adapter to load pages(categories), but it does not seem to solve my problem.

@Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return x.newInstance();
            case 1:
                return y.newInstance();
            case 2:
                return z.newInstance();
            default:
                return null;
        }
    }

Solution

  • can you provide more details? do you refer to the Tab as item? is each category presneted by the same Class? because if that's the case then provide arguments to the newInstance method where you specify the category and the number of items you need like so:

    public class Frag_UserItems extends Fragment {
    ...
           public static MyFragment newInstance (int itemsNumber, Category category){ 
                 // assuming category is an enum
                 Bundle bundle = new Bundle();
                 MyFragment fragment = new MyFragment();
                 bundle.setInt("num", itemsNumber);
                 bundle.setString("category", category.toString());
                 fragment.setArguments(args);
                 return fragment;
                }
    ...
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_user_items, container, false);
    
        //fetch the arguments and get the data from the reposotry
       // and filter them how you want
         Bundle args = getArguments();
         int num = args.getInt("num");
         String num = args.getString("category");
         List<MyItem> items = ...
         // bind view..
          initview(rootView, items);
          return rootView;
        }
    }
    

    T think you can store the fragment instances in an array also, or you can pass the index number to the newInstance method and there you would deal with creating the fragment based on the index, that way it will reduce getItem to:

    @Override
    public Fragment getItem(int position) {
          return MyFragment.newInstance(position);
    }
    
        public class Frag_UserItems extends Fragment {
    ...
           public static MyFragment newInstance (int position){ 
                 // assuming category is an enum
                 Bundle bundle = new Bundle();
                 MyFragment fragment = new MyFragment();
                 bundle.setInt("num", position);
                 fragment.setArguments(args);
                 return fragment;
                }
    ...
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_user_items, container, false);
    
        //fetch the arguments and get the data from the reposotry
       // and filter them how you want
         Bundle args = getArguments();
         int num = args.getInt("num");
         List<MyItem> items = ...
         // bind view..
          initview(rootView, items);
          return rootView;
        }
    }
    

    this is basicly following the Factory method pattern.

    I hope this helps