Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button in ViewPager scroll to specific page

One of my layouts inside of my ViewPager has a button. "R.layout.add_site". I would like the option of hitting the button and it scroll to the specific page for me. I already have the option to swipe to the specific page, but I would like to have both.

Now I'm sure there's a way to do that, but for some reason, I cannot figure it out.

You'll see that I've made an attempt , but just dont know what method to call in order to make it scroll to the desired page. Which is R.layout.main.

Here's my code.

public class fieldsActivity extends Activity {  Button addSiteButton; Button cancelButton; Button signInButton;  /**  * Called when the activity is first created.  */ @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     // to create a custom title bar for activity window     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);      setContentView(R.layout.fields);     // use custom layout title bar     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);      Pager adapter = new Pager();     ViewPager mPager = (ViewPager) findViewById(R.id.fieldspager);     mPager.setAdapter(adapter);     mPager.setCurrentItem(1);       addSiteButton = (Button) findViewById(R.id.addSiteButton);     addSiteButton.setOnClickListener(new View.OnClickListener() {           @Override         public void onClick(View v) {         // Don't know what method to call!?            }       });       cancelButton = (Button) findViewById(R.id.cancel_button);     signInButton = (Button) findViewById(R.id.sign_in_button);  }   private class Pager extends PagerAdapter {     public int getCount() {         return 3;      }      @Override     public Object instantiateItem(View collection, int position) {         LayoutInflater inflater = (LayoutInflater) collection.getContext()                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);         int resId = 0;         switch (position) {             case 0:                 resId = R.layout.field01;                 break;             case 1:                 resId = R.layout.add_site;                      break;             case 2:                 resId = R.layout.main;                 break;          }          View view = inflater.inflate(resId, null);         ((ViewPager) collection).addView(view, 0);         return view;     }      @Override     public void destroyItem(View arg0, int arg1, Object arg2) {         ((ViewPager) arg0).removeView((View) arg2);     }     @Override     public boolean isViewFromObject(View arg0, Object arg1) {         return arg0 == ((View) arg1);     }     @Override     public Parcelable saveState() {         return null;     }   }  } 

Any help is appreciated!

like image 716
PhDeOliveira Avatar asked Jan 18 '13 23:01

PhDeOliveira


People also ask

How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

To enable / disable the swiping, just overide two methods: onTouchEvent and onInterceptTouchEvent . Both will return "false" if the paging was disabled. You just need to call the setPagingEnabled method with false and users won't be able to swipe to paginate.

How do I swipe ViewPager on Android?

Implement Swipe Views You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .

How do you stop swipe in ViewPager?

You can easily do that by creating a custom class inherits from viewPager and override two methods: “onTouchEvent()” and “onInterceptTouchEvent()” and return true or false to disable and enable the swiping on certain touch events i.e say swiping.


1 Answers

call :

 mPager.setCurrentItem(2); 

in your onClick() method, or for a smoother scroll:

mPager.setCurrentItem(2, true);  
like image 134
Ercan Avatar answered Sep 21 '22 13:09

Ercan