Question

From a ListFragment we can call getListAdapter() to access the list's adapter from one of its child fragments. Is there a simple and class-native way to access the ViewPager adapter from one of its child fragments, similar to getListAdapter() in ListFragment?

If there isn't, is the correct approach to include a reference to the ViewPager adapter on every one of its child fragments?

Was it helpful?

Solution

Lets say you have protected MyPagerAdapter mAdopter; in Parent class.

From child fragment class do this

((YourParentClass)getActivity()).mAdopter.WHAT_EVER_YOU_WANT_THIS_FOR;

Note: you can change access modifier (protected) according to your need.

OTHER TIPS

There is a way to do that without including a reference to the ViewPager in a fragment.

  1. You can declare a public method in your activity that would obtain a reference to the ViewPager adapter.

  2. In your fragment, you can obtain the reference to the activity using getActivity(). You can then cast that reference to the actual activity type if you don't mind tighter coupling this brings.

  3. You can now use the method from your activity you declared in point 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top