Android disables ViewPager sliding and androidviewpager

Source: Internet
Author: User

Android disables ViewPager sliding and androidviewpager

In recent projects, we have a requirement to disable the ViewPager slide event. Let's take a look at the 360 mobile assistant interface. The style is similar to this.


If you have used the 360 mobile assistant, you will find that the middle content cannot be slide. Now, write a demo to explain how to disable the slide of ViewPager events.

Layout File

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>
<Com. example. viewpagears. ui. MyViewPager
Android: id = "@ + id/viewpager"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</RelativeLayout>

Custom ViewPager class

Public class MyViewPager extends ViewPager {
Private static final String TAG = "MyViewPager ";
Private boolean result = false;
Public MyViewPager (Context context, AttributeSet attrs ){
Super (context, attrs );
}
Public MyViewPager (Context context ){
Super (context );
}
@ Override
Public boolean onInterceptTouchEvent (MotionEvent arg0 ){
If (result)
Return super. onInterceptTouchEvent (arg0 );
Else
Return false;
}
@ Override
Public boolean onTouchEvent (MotionEvent arg0 ){
If (result)
Return super. onTouchEvent (arg0 );
Else
Return false;
}
}


MainActivity. java

Package com. example. viewpagears;


Import java. util. ArrayList;
Import java. util. List;


Import android. app. Activity;
Import android. OS. Bundle;
Import android. support. v4.view. PagerAdapter;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. ViewGroup;
Import android. widget. ImageView;
Import android. widget. Toast;


Import com. example. viewpagears. ui. MyViewPager;


Public class MainActivity extends Activity {
Private MyViewPager viewpager;
Private List <ImageView> views;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
InitData ();
Viewpager = (MyViewPager) findViewById (R. id. viewpager );
MyAdapter adapter = new MyAdapter ();
Viewpager. setAdapter (adapter );
}
Private void initData (){
Views = new ArrayList <ImageView> ();
For (int I = 0; I <6; I ++ ){
ImageView iv = new ImageView (this );
Iv. setImageResource (R. drawable. ic_launcher );
Iv. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
Toast. makeText (getApplicationContext (), "clicked", 0). show ();
}
});
Views. add (iv );
}
}
Class MyAdapter extends PagerAdapter {


@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return views. size ();
}


@ Override
Public boolean isViewFromObject (View arg0, Object arg1 ){
// TODO Auto-generated method stub
Return arg0 = arg1;
}


@ Override
Public void destroyItem (ViewGroup container, int position, Object object Object ){
Container. removeView (views. get (position ));
}


@ Override
Public Object instantiateItem (ViewGroup container, int position ){
// TODO Auto-generated method stub
Container. addView (views. get (position ));
Return views. get (position );
}

}
}

In fact, we have simply rewritten two methods in custom Viewpager: onInterceptTouchEvent () and onTouchEvent ()

OnInterceptTouchEvent () indicates intercepting touch events

OnTouchEvent () touch event

In android, The onTouchEvent is returned true by default, indicating that the event can be touched. You can check the source code. Here we will not talk about it. onInterceptTouchEvent () cannot return true, if the return true sub-view event is intercepted, Viewpager overwrites the method. You can check its source code,


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.