jublikon
jublikon

Reputation: 3447

View Pager - get pager number for each page

I have a fragment that holds a view pager. Each page of the view pager contains a fragment.

Is there a possibility to get the index numbers of the current pages inside of the content fragment?

Or do I have to pass each fragment its number on creation?

Or do I have to use a static "newInstance" method of the fragment to pass it the data?

Upvotes: 0

Views: 52

Answers (2)

Anoop M Maddasseri
Anoop M Maddasseri

Reputation: 10569

Instantiate your view pager/child fragments using Static factory methods

 private String fragmentId= "";

 /**
   * The argument keys for creating a fragment.
  */
 private static final String ARG_ONE = "arg1";

 /**
   * Factory method for this fragment class. Constructs a new fragment with the given arguments.
  */
  public static MyFragment create(String fragmentId) {
            MyFragment tab = new MyFragment();
            Bundle bundle = new Bundle(1);
            bundle.putString(ARG_ONE, fragmentId);
            tab.setArguments(bundle);
            return tab;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.tab_home_screen, container, false);
        this.fragmentId=getArguments().getString(ARG_ONE);
        return view;
     }

Later you can use the fragmentId for knowing the fragment.

Upvotes: 1

Zubair Akber
Zubair Akber

Reputation: 2828

You can do this by setting a tag to your fragment or set a tag to any of your view object of fragment and get in inside your fragment

Upvotes: 0

Related Questions