0

I have 4 fragment in viewpager (Easy, Normal, Hard, Advance) and I just want to open Normal fragment when onClick from other activity.

PageAdapter:

public class PagerAdapter extends FragmentPagerAdapter {

private List<Fragment> fragments;
public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}
@Override
public int getCount() {
    return this.fragments.size();
}
}

FragmentActivity:

public class level_selection extends FragmentActivity {

private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.level_selection);
    //initialsie the pager
    this.initialisePaging();
}

private void initialisePaging() {

    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, Easy.class.getName()));
    fragments.add(Fragment.instantiate(this, Normal.class.getName()));
    fragments.add(Fragment.instantiate(this, Hard.class.getName()));
    fragments.add(Fragment.instantiate(this, Advance.class.getName()));
    this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);
    //
    ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
    pager.setAdapter(this.mPagerAdapter);
}
}

Based on code above anybody can guide me on how to modify it to allow open fragment "Normal" by onClick from other Activity?

Best Regards,

user3208987
  • 119
  • 15

2 Answers2

3

make pager a class field and then use from onClick method:

 pager.setCurrentItem(1);

This assumes your "normal" Fragment always has the index 1 in your List (from you code, it is actually at index 1)

Droidman
  • 11,485
  • 17
  • 93
  • 141
  • can you explain please how the whole OnClick process done ?? – Reem Aziz May 09 '17 at 08:10
  • @ReemAziz take a look at [this](http://stackoverflow.com/questions/10231309/android-button-onclick) question to get an idea about how "the whole OnClick process" works – Droidman May 09 '17 at 11:26
  • No you don't understand my Question I mint the onClick of the activity and what should i pass to the intent to to get the position of the ViewPager .. I really did what you said , but still give me wrong position since I combine BottomNavigationView with ViewPager – Reem Aziz May 09 '17 at 13:37
1

Try this way,hope this will help you to solve your problem.

ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
pager.setAdapter(this.mPagerAdapter);
pager.setCurrentItem(1);

OR

List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Normal.class.getName()));    
fragments.add(Fragment.instantiate(this, Easy.class.getName()));
fragments.add(Fragment.instantiate(this, Hard.class.getName()));
fragments.add(Fragment.instantiate(this, Advance.class.getName()));
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67