Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch fragments programmatically

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

Best Regards

like image 368
Albert078 Avatar asked Jan 02 '12 19:01

Albert078


2 Answers

You need to set current item in ViewPager and then you should notify the adapter to make it bring the new Fragment.

ViewPager ViewPager;
FragmentPagerAdapter adapter;

pager.setCurrentItem(2);
adapter.notifyDataSetChanged();
like image 162
Camilo Ortegón Avatar answered Oct 26 '22 20:10

Camilo Ortegón


I'm assuming you mean ViewPager. You can toggle between them by using pager.setCurrentItem(index)

like image 29
sgarman Avatar answered Oct 26 '22 18:10

sgarman