Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager set current page programmatically

I wrote a custom ViewPager to disable Swipe Scroll, but I want to swipe programmatically. I have three Tab in my view pager, but when I call viewPager.setCurrentItem(viewPager.getCurrentItem()+1) on the first Fragment, it moves to the third Fragment instead of the second Fragment. And if I call same function in the second Fragment, it goes to the third. If I call (viewPager.getCurrentItem()-1)` in the third fragment, it works fine by moving back. Any help would be appreciated. My code is below:

NonSwipeAbleViewPager

public class NonSwipeableViewPager extends ViewPager {  private boolean swipeable;  public NonSwipeableViewPager(Context context, AttributeSet attrs) {     super(context, attrs);     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyViewPager);     try {         swipeable = a.getBoolean(R.styleable.MyViewPager_swipeable, true);     } finally {         a.recycle();     } }  @Override public boolean onInterceptTouchEvent(MotionEvent event) {     return swipeable ? super.onInterceptTouchEvent(event) : false; }  @Override public boolean onTouchEvent(MotionEvent event) {     return swipeable ? super.onTouchEvent(event) : false; } } 

Declaration in XML

<co.example.customview.NonSwipeableViewPager     android:id="@+id/pager"     android:layout_width="match_parent"     android:layout_height="0dp"     android:layout_weight="1"     app:swipeable="false" /> 

Calling it

 @Override public boolean onOptionsItemSelected(MenuItem item) {     switch (item.getItemId()) {         case R.id.menuNext:             NonSwipeableViewPager pages = (NonSwipeableViewPager) getActivity().findViewById(R.id.pager);             pages.setCurrentItem(pages.getCurrentItem()+1, true);             break;         default:             return super.onOptionsItemSelected(item);     }     return super.onOptionsItemSelected(item); } 
like image 768
Tonespy Avatar asked Mar 10 '15 16:03

Tonespy


Video Answer


2 Answers

viewPager.setCurrentItem(idx); 

where idx is 0 based integer.

like image 113
Alp Altunel Avatar answered Sep 23 '22 16:09

Alp Altunel


In your PagerActivity use

    viewPager = (ViewPager) findViewById(R.id.view_pager);     viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {         @Override         public void onPageSelected(int position) {             int pagei = position + 1;             pages=pagei + "";              Toast.makeText(PagerActivity.this, getString(R.string.changeinfopage) + " " + pages, Toast.LENGTH_SHORT).show();         }          @Override         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {         }          @Override         public void onPageScrollStateChanged(int state) {         }     }); 

and

    @Override     public boolean onOptionsItemSelected(MenuItem item) {           switch (item.getItemId()) {              case R.id.action_previous:                  viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);                 return true;              case R.id.action_next:                  viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);                 return true;         }          return super.onOptionsItemSelected(item);     } 
like image 23
eurosecom Avatar answered Sep 26 '22 16:09

eurosecom