Turn off fragments programmatically

I have a pager with two fragments, each with its own xml. My question is, is it possible to programmatically switch from one fragment to another and start any listener?

Best wishes

+6
source share
3 answers

I assume you mean ViewPager . You can switch between them using pager.setCurrentItem (index)

+7
source

If you are using ViewPager , you can try setSelectedNavigationItem() . That is, you can switch from one fragment to another by calling:

 int position = 0; // position of the tab you want ((ParentActivity) getActivity()).getActionBar().setSelectedNavigationItem(position); 

or, if you use ActionBarSherlock:

 int position = 0; // position of the tab you want ((ParentActivity) getActivity()).getSupportActionBar().setSelectedNavigationItem(position); 
0
source

You need to set the current item in the ViewPager, and then you need to notify the adapter so that it brings a new fragment.

 ViewPager ViewPager; FragmentPagerAdapter adapter; pager.setCurrentItem(2); adapter.notifyDataSetChanged(); 
0
source

Source: https://habr.com/ru/post/904934/


All Articles