3

I am creating ViewPager and want to call the next Pager on Button.OnClickListener. Setting adapter from the following code in Activity:

@EActivity(R.layout.activity_city)
public class CountrySelectionActivity extends Activity {

@ViewById ViewPager viewPager;
@Bean CustomAdapter customAdapter;

@AfterViews
void afterViewCreated(){
    viewPager.setAdapter(customAdapter);
  }
}

Here is the code for CustomAdapter class:

@EBean
public class CustomAdapter extends PagerAdapter {

public CustomAdapter(Context context) {
    mContext = context;
    loadData();   // loading data as in List
}

......
......

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public void destroyItem(View container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    TextView firstCountry;
    TextView secondCountry;

    LayoutInflater layoutInflater = LayoutInflater.from(mContext);
    View view = layoutInflater.inflate(R.layout.country_pager_item, container, false);
    ....
    ....
}

Everything is working fine. I can see the ViewPager loading the two countries on every pages.

I can also slide the pages (Previous and Next). But I want to move to next Pager as soon as click on TextView and so on. Is it possible to do it in ViewPager?

Amit Pal
  • 10,604
  • 26
  • 80
  • 160
  • Possible duplicate of [How to programmatically show next view in ViewPager?](https://stackoverflow.com/questions/7801954/how-to-programmatically-show-next-view-in-viewpager) – Pitel Oct 11 '18 at 09:28

2 Answers2

2

Sure! You just have to call viewPager.setCurrentItem(...)

EDIT:

If you're wondering how you can call this method from within your Adapter, here's an example:

public class CustomAdapter extends PagerAdapter {

    // ... other methods

    public static interface Callback {
        public void onNextClick(int nextPage);
    }

    private Callback callback;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        TextView firstCountry;
        TextView secondCountry;

        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.country_pager_item, container, false);

        secondCountry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (callback != null){
                        callback.onNextClick(2);
                    }
                }
            });
    }
}

and in your activity:

@AfterViews
void afterViewCreated(){
    customAdapter.setCallback(new Callback() {
        @Override
        public void onNextClick(int nextPage) {
           viewPager.setCurrentItem(nextPage);
        }
    });
    viewPager.setAdapter(customAdapter);
  }
}
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
0
yourButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View view) {
// replace 1 by the page you want 
      yourViewPager.setCurrentItem(1, true);
   }
});

if the button is inside Fragment then create an interface in your first fragment when you click the button call the interface ,implement it in your mainactivity and there you set the view pager page

yourViewPager.setCurrentItem(2, true);
Nouman Shah
  • 534
  • 1
  • 9
  • 22