Search code examples
androidandroid-fragmentsandroid-viewpager

calling viewpager page2 from page 1 in android


  1. First i have one activity in which i have view pager
  2. Each page of view pager holds fragment
  3. Now in first page fragment i have button and when i click on button i want page two of view pager, **when i click view all it should open womens page which is position2 of viewpager **

    here is my main activity viewpager page 1 when i click view all it should go to next page

here i should go


Solution

  • Define a method in the Activity (containing the ViewPager and the sub-fragments) like so:

    public void setPagerFragment(int a)
    {
        viewPager.setCurrentItem(a);
    }
    

    This will set the current Fragment in the ViewPager to be the one specified. You can then call this method from any child Fragment with:

    int newFrag = 0; //the number of the new Fragment to show
    ParentActivity parent = (ParentActivity) getActivity();
    parent.setPagerFragment(newFrag);