Search code examples
androidandroid-fragmentsandroid-viewpager

Getting Parent ViewPager View from inside Fragment


I am trying to access the parent viewpager from inside a fragment, but i have no idea how to do that. I need to switch the currentItem on the ViewPager after a onClick event inside the fragment.

Any ideas?

EDIT:

I want access to the parent view(ViewPager View) so that i can change the currentItem which is visible, from inside one of my fragments.


Solution

  • From fragment, call getActivity() which will gives you the activity in which the fragment is hosted. Then call findViewById(ViewPagerId) to get the ViewPager.

    ViewPager vp=(ViewPager) getActivity().findViewById(ViewPagerId);