Android Rewrite Viewpager modify sliding sensitivity

Source: Internet
Author: User

Use Viewpager as a page to switch, it can store a lot of view, but sometimes in the operation of the view is not careful to swipe a bit may jump to the next page, this is not what we want, here we need to rewrite Viewpager modify its sliding conditions


Program directory Structure


Btviewpager.java

Package Com.example.viewpagerdemo;import Android.content.context;import Android.support.v4.view.viewpager;import Android.util.attributeset;import Android.util.log;import Android.view.motionevent;import Android.widget.Scroller /** * Solve Viewpager sliding is too sensitive, only sliding distance greater than 100 to slide to another page * * @author Administrator * */public class Btviewpager extends Viewpager {pri vate static final String TAG = "Dzt_pager";p rivate static final int move_limitation = 100;//trigger moved pixels away from private float mlastm Otionx; Finger touch the last x-coordinate of the screen private int mcurscreen;private Scroller Mscroller; Slide Control Public Btviewpager (context context) {super (context);//TODO auto-generated Constructor Stubinit (context);} Public Btviewpager (context context, AttributeSet Attrs) {Super (context, attrs);//TODO Auto-generated constructor Stubinit (context);} private void init (context context) {Mscroller = new Scroller (context); Mcurscreen = 0;//default setting displays the first view} @Overridepublic Boo Lean Ontouchevent (Motionevent event) {//TODO auto-generated method stubfinal int Action = Event.getaction (); final float x = event.getx (); switch (action) {case MOTIONEVENT.ACTION_DOWN:LOG.D (TAG, "[ Btviewpager->]ontouchevent Action_down "); Mlastmotionx = X;break;case motionevent.action_move:log.d (TAG," [ Btviewpager->]ontouchevent action_move "); break;case motionevent.action_up:log.d (TAG," Item = "+ getCurrentItem () + "Count =" + Getchildcount ()); if (Math.Abs (X-mlastmotionx) < move_limitation) {//snaptodestination (); Jumps to the specified page Snaptoscreen (Getcurrentitem ()); return true;} Break;default:break;} LOG.D (TAG, "[btviewpager->]ontouchevent--end"); return super.ontouchevent (event);} @Overridepublic void Computescroll () {//TODO auto-generated method Stublog.d (TAG, "[Btviewpager->]computescroll"); Super.computescroll (), if (Mscroller.computescrolloffset ()) {LOG.D (TAG, "[Btviewpager->]computescroll x =" + Mscroller.getcurrx ()); ScrollTo (Mscroller.getcurrx (), Mscroller.getcurry ()); invalidate ();}} /** * Move to the number of views based on sliding distance */public void Snaptodestination () {final int Screenwidth = getwidth (); final int destscreen = (GETSCROLLX () + SCREENWIDTH/2)/screenwidth; LOG.D (TAG, "[Btviewpager->]snaptodestination screenwidth =" + ScreenWidth + "Destscreen =" + Destscreen); SnapToScreen (Destscreen);} /** * Scroll to the developed view * * @param whichscreen * view subscript */public void Snaptoscreen (int whichscreen) {//Whichscreen = Mat H.max (0, Math.min (Whichscreen, Getchildcount ()-//1)); if (GETSCROLLX ()! = (Whichscreen * getwidth ())) {Final int delta = Whichscreen * getwidth ()-GETSCROLLX (); LOG.D (TAG, "[Btviewpager->]snaptoscreen-whichscreen =" + Whichscreen + "Delta =" + Delta + "SCROLLX =" + GETSCROLLX ( ); Mscroller.startscroll (Getscrollx (), 0, Delta, 0,math.abs (Delta) * 2); mcurscreen = Whichscreen;invalidate ();}} /** * For intercepting gesture events, each gesture event will call this method first. The default return value of Onintercepttouchevent in Layout is false, * so that the touch event is passed to the Childview control, and if the return is false the child control can respond, otherwise the control does not respond, here is the response to intercept the child control primarily, * For ViewGroup no matter what the return value is Ontouchevent */@Overridepublic Boolean onintercepttouchevent (motionevent arg0) {//TODO auto-gEnerated method Stublog.d (TAG, "[btviewpager->]onintercepttouchevent"); final int action = Arg0.getaction (); final float x = arg0.getx (); switch (action) {case MOTIONEVENT.ACTION_DOWN:LOG.D (TAG, "Onintercepttouchevent---action_down") ; Mlastmotionx = X;break;case motionevent.action_move:log.d (TAG, "Onintercepttouchevent---action_move"); break;case MOTIONEVENT.ACTION_UP:LOG.D (TAG, "Onintercepttouchevent---action_up"); break;default:break;} Return super.onintercepttouchevent (arg0);}}
As long as the sliding condition is handled in the ontouchevent up

if (Math.Abs (X-mlastmotionx) < move_limitation) {//Snaptodestination ();//jump to the specified page Snaptoscreen (Getcurrentitem ()); return true;}
Only the sliding distance is greater than 100 for the next page processing, otherwise it will stop at the current page, the current page using Getcurrentitem () to get,one thing to note in Viewpager Getchildcount () Gets the value is wrong, not clear what is the reason, in ViewGroup is valid, this may want to see Viewpager source code to find out what is the reason, have to know friends can tell me!
Full demo:http://download.csdn.net/detail/deng0zhaotai/7384637

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.