0

I currently have a app that allows me to changed between fragments by swiping left on the screen. How can i change it so it switches after i click a button.

     public class MainActivity extends FragmentActivity {
    private  PagerAdapter mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpager_layout);
    initial();
}

private void initial() {
    // TODO Auto-generated method stub
    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
    fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
    mp = new PagerAdapter(this.getSupportFragmentManager(),fragments);
    ViewPager pager = (ViewPager)findViewById(R.id.view);

    pager.setAdapter(mp);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

      }

1 Answers1

2

You can achieve this(if you are using ViewPager) by calling the method on button click

ViewPager.setCurrentItem(int pageIndex, boolean isSmoothScroll);

Here is the exact way to do this :

Click Me

Community
  • 1
  • 1
Cyph3rCod3r
  • 1,978
  • 23
  • 33