Android编程中ViewPage判断左右滑动方向的方法
程序员文章站
2023-12-10 21:54:58
本文实例讲述了android编程中viewpage判断左右滑动方向的方法。分享给大家供大家参考,具体如下:
package com.meityitian.app....
本文实例讲述了android编程中viewpage判断左右滑动方向的方法。分享给大家供大家参考,具体如下:
package com.meityitian.app.views; import android.content.context; import android.support.v4.view.viewpager; import android.util.attributeset; import android.util.log; import com.meityitian.app.utils.debug; /** * 重写,添加了判定滑动方向的方法 * @author zxy * */ public class meityitianviewpager extends viewpager { private boolean left = false; private boolean right = false; private boolean isscrolling = false; private int lastvalue = -1; private changeviewcallback changeviewcallback = null; public meityitianviewpager(context context, attributeset attrs) { super(context, attrs); init(); } public meityitianviewpager(context context) { super(context); init(); } /** * init method . */ private void init() { setonpagechangelistener(listener); } /** * listener ,to get move direction . */ public onpagechangelistener listener = new onpagechangelistener() { @override public void onpagescrollstatechanged(int arg0) { if (arg0 == 1) { isscrolling = true; } else { isscrolling = false; } debug.infobytag("meityitianviewpager", "meityitianviewpager onpagescrollstatechanged : arg0:" + arg0); if (arg0 == 2) { debug.infobytag("meityitianviewpager", "meityitianviewpager onpagescrollstatechanged direction left ? " + left); debug.infobytag("meityitianviewpager", "meityitianviewpager onpagescrollstatechanged direction right ? " + right); //notify .... if(changeviewcallback!=null){ changeviewcallback.changeview(left, right); } right = left = false; } } @override public void onpagescrolled(int arg0, float arg1, int arg2) { if (isscrolling) { if (lastvalue > arg2) { // 递减,向右侧滑动 right = true; left = false; } else if (lastvalue < arg2) { // 递减,向右侧滑动 right = false; left = true; } else if (lastvalue == arg2) { right = left = false; } } log.i("meityitianviewpager", "meityitianviewpager onpagescrolled last :arg2 ," + lastvalue + ":" + arg2); lastvalue = arg2; } @override public void onpageselected(int arg0) { if(changeviewcallback!=null){ changeviewcallback.getcurrentpageindex(arg0); } } }; /** * 得到是否向右侧滑动 * @return true 为右滑动 */ public boolean getmoveright(){ return right; } /** * 得到是否向左侧滑动 * @return true 为左做滑动 */ public boolean getmoveleft(){ return left; } /** * 滑动状态改变回调 * @author zxy * */ public interface changeviewcallback{ /** * 切换视图 ?决定于left和right 。 * @param left * @param right */ public void changeview(boolean left,boolean right); public void getcurrentpageindex(int index); } /** * set ... * @param callback */ public void setchangeviewcallback(changeviewcallback callback){ changeviewcallback = callback; } }
希望本文所述对大家android程序设计有所帮助。