Search code examples
androidandroid-layoutandroid-fragmentsandroid-viewpagerandroid-view

How to call next pages of ViewPager programmatically?


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?


Solution

  • 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);
      }
    }