Reputation: 129
I want to build a ViewPager
with 2 Pages. On the first page you can choose a category. When a List item is selected
the screen swipes to the second page and you can choose some subcategories.
I implemented the clicklistener
and so on. But I have no idea how to set the current page from within a fragment
. Is it even possible? Or do you have another solution for me? Hope you understand what I want to achieve.
Upvotes: 2
Views: 4516
Reputation: 317
use ((ActivityName)getActivity()).getViewPager().setCurrentItem(position);
note: getViewPager()
should return the viewpager instance
Upvotes: 2
Reputation: 129
Ok, found a solution for my own. Thanks to Sayem for the hint.
I passed the ID of the ViewPager via bundle to my first fragment. in my fragment i could use then this simple code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
viewPagerId = getArguments().getInt("viewPagerId");
View rootView = (View) inflater.inflate(R.layout.fragment_cat, container, false);
populateListView(rootView);
viewPager = (ViewPager) getActivity().findViewById(viewPagerId);
return rootView;
}
and to switch pages then simply this:
viewPager.setCurrentItem(position);
Thanks for the help
Upvotes: 5