Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set currentItem of a viewPager from a fragment

I am developing a Project in which I am using View Pager on one screen, It only has two pages.

On the first one i have a simple form with some widgets. On the second page I have listview with some products name and quantitys.

Is possible to change from the first page to second after selecting an specific option of a spinner placed in this first page (Frgament)?

I have read on similar questions that this method :

ViewPager.setCurrentItem(int index);

makes possible changing to an specific page, the thing is i don't know how to reference to this viewpager object that i declare on my activity host from my fragment.

like image 756
Henry1988 Avatar asked Nov 29 '22 16:11

Henry1988


1 Answers

Though, I don't understand the scenario completely. From what I understood, this is a hack that'd work in this case.

In Activity:

public ViewPager getViewPager() {
   if (null == mViewPager) {
       mViewPager = (ViewPager) findViewById(R.id.view_pager_id)
   }
   return mViewPager;
}

In Fragment:

((YourActivitName)getActivity()).getViewPager().setCurrentItem(index);
like image 169
Gaurav Arora Avatar answered Dec 04 '22 13:12

Gaurav Arora