Title: Viewpager with diffrent page number
Tags: java;android;android-viewpager
Question: 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;
        }
    }
```


Here is the accepted answer: 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&gt; 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&gt; items = ...
     // bind view..
      initview(rootView, items);
      return rootView;
    }
}
```

this is basicly following the Factory method pattern.

I hope this helps

Comment for this answer: Well the answer above should cover you, use the second example and nwrite a method that filters the objects based on what position you are at in the viewpager
Comment for this answer: I'm mean by item a  product that has a categories  **ex(item 0  has A, B, C )**   of elements I want to put on tapped layout and switch between them with viewpager. yes,  each category presneted by the same Class.
Comment for this answer: Thanks for your help, greatly appreciated.

Here is another answer: You should give us more information about what you are going to do! But as far as I understood, you have some items and each of them have some sub-items. If it was me, I would do that with nested RecyclerView!
Here I found a good article for helping you with:
https://android.jlelse.eu/easily-adding-nested-recycler-view-in-android-a7e9f7f04047

I hope it's what you're trying to do.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:12/09/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><android><android-viewpager>', 'Title': 'Viewpager with diffrent page number', 'ViewCount': 45, 'id': 59251158, 'user_AboutMe': None, 'user_AccountId': 17232211.0, 'user_DisplayName': 'elnzeer', 'user_Reputation': 3.0, 'user_Views': 3.0, 'user_id': 12476512.0}}