Android Disable Viewpager swipe

Source: Internet
Author: User

In recent projects, there is a need to prohibit viewpager sliding events, we look at the 360 mobile Phone assistant interface, style is similar to this


If you have used 360 mobile phone assistants will find the middle content is not to slide, now write a demo, how to prohibit the Viewpager event sliding

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 (), "was 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) {
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 are in the custom Viewpager is actually very simple rewrite 2 methods: Onintercepttouchevent (), Ontouchevent ()

Onintercepttouchevent () means intercept touch event

Ontouchevent () Touch Event

In Android ontouchevent default is return true means can touch, everyone can go through the source to see, in here do not say, onintercepttouchevent () can not return true, if return True child View what events have been intercepted, Viewpager this method has some rewriting, you can go to see his source code,


Android Disable Viewpager swipe

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.