Home » Android » android – Change viewpager fragment by a buttonClick

android – Change viewpager fragment by a buttonClick

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to change the viewpager fragment by clicking on a button. I have 5 fragments, each fragment has it’s own xml file (frag1.xml, frag2.xml, and so on). Every fragment has it’s 5 buttons that should go to other pages of the viewpager. But the problem is how do I check in the FragmentPageAdapter which button is clicked and how to get there?

I’ll show the code I have then it should be clear I think. Think of it like a homescreen that has dots at the bottom and I you click a certain dot, you’ll go to the corresponding screen.

FragmentPagerAdapter

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    final int PAGE_COUNT = 6;

    /** Constructor of the class */
    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    /** This method will be invoked when a page is requested to create */
    @Override
    public Fragment getItem(int arg0) {

        switch(arg0){

        case 0:

            return new Fragment1();

        case 1:
            return new Fragment2();

        case 2:
            return new Fragment3();

        case 3:
            return new Fragment4();

        case 4:
            return new Fragment5();

        default:
            return null;

        }       
    }

    /** Returns the number of pages */
    @Override
    public int getCount() {
        return PAGE_COUNT;
    }
}

Frag1.java

public class Frag1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_one, container, false);


        OnClickListener changeFrag = new OnClickListener() {            
            @Override
            public void onClick(View v) {
            /*When I click this button in my fragment, I'd like it to go to fragment 3 for example*/
            }
        };

        ImageButton btnT = (ImageButton) v.findViewById(R.id.frag3);
        btnT.setOnClickListener(changeFrag);

        return v;
    }
}

MainActivity

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);/** Getting a reference to the ViewPager defined the layout file */
        ViewPager pager = (ViewPager) findViewById(R.id.pager);

        /** Getting fragment manager */
        FragmentManager fm = getSupportFragmentManager();

        /** Instantiating FragmentPagerAdapter */
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

        /** Setting the pagerAdapter to the pager object */
        pager.setAdapter(pagerAdapter);

       //pager.setPageTransformer(true, new ZoomOutPageTransformer());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Is something like this possible? Can someone help me in the right direction please?

How to&Answers:

I’m not sure what exactly you said about “Change”.

If you’re asking about change page display index, you can try

ViewPager.setCurrentItem(int pageIndex, boolean isSmoothScroll);

Or, if you’re asking about change content, here’s what I did

public class MyPagerAdapter extends FragmentStatePagerAdapter
{
  private FragmentManager fragMan;
  private ArrayList<Fragment> fragments=new ArrayList<Fragment>();

  public void clearAll() //You can clear any specified page if you want...
  {
    for(int i=0; i<fragments.size(); i++)
    fragMan.beginTransaction().remove(fragments.get(i)).commit();
    fragments.clear();
    fragments=new ArrayList<Fragment>();
    notifyDataSetChanged();
  }

  public void addList() //Add some new fragment...
  {
    listFrag=new ListFragment();
    fragments.add(list);
  }
}

Hope it helps~

Answer:

I’m posting a new answer because – from what I can see – the real answer is on the comment of the accepted answer:

“You have 2 way to do. 1) Put your ViewPager object as public static, so that you can access in any of your Fragment. 2) Stay your ViewPager in as member of your FragmentActivity, send a broadcast intent from your Fragment to FragmentActivity, and access your ViewPager object. – RRTW May 28 ’13 at 2:18 “

So thanks to RRTW for that answer, but I would like to improve it.

1) is NOT an option. The purpose of fragments is to be reusable, so they need to be independant from the Activity. Linking them to activity is I think a bad practise, even if – for now – you are not re-using it on other activity.

2) Sounds very good, but I think it needs more explanation. The purpose of getAcitivty().sendBroadcast() is to send an event to all application of the user’s system. It’s definitly not what you want to achieve. So the good answer, from my point of view, is to use LoacalBroadcastManager, and you can have a look on that post to understand how to use it.

Answer:

In main just call make your Viewpager public so its accessable from other java files( ex. from frag1.java)

MainActivity.Java:

public static ViewPager pager;

Now, in the Onclick() function inside frag1.java just call your mViewPager and set the item(fragment) to whatever fragment you want to load. Ex. If you want to load the next fragment then you set it to 2.

frag1.java

public void onClick(View v) {

    ViewPager.setCurrentItem(2);

}