我需要停止ListVIEw以响应用户手势,如果这些手势是在某个特定的ListVIEw行中进行的 – 如何做到这一点?目标ListVIEw的行视图具有ontouchListener设置,由于ListVIEw向上/向下滚动,我无法识别滑动/翻转.因此,如果我向上或向下移动手指 – ListVIEw会截取并向相应方向滚动.所以我需要以某种方式来统治它,如果Y-coord超过一些量 – 让ListVIEw滚动,如果不是 – 将手势识别为fling / swipe. OntouchListener是
private int SWIPE_MIN_disTANCE = 1;private int SWIPE_MAX_OFF_PATH = 300;final OntouchListener flingSwipeListener = new OntouchListener() { float touchX; float touchY; @OverrIDe public boolean ontouch(final VIEw vIEw, final MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { touchX = event.getX(); touchY = event.getY(); } else if (event.getAction() == MotionEvent.ACTION_UP) { if (Math.abs(touchY - event.getY()) > SWIPE_MAX_OFF_PATH) return false; // right to left swipe else if (touchX - event.getX() > SWIPE_MIN_disTANCE){ Log.i("flingSwipe","right to left swipe"); } // left to right swipe else if (event.getX() - touchX > SWIPE_MIN_disTANCE){ Log.i("flingSwipe","left to right swipe"); } } return true; }};
此ontouchListner设置为一个特定行.我需要冻结ListVIEw,而ontouchListener识别手势,但如果失败,我需要将MotionEvent发送到ListVIEw.
解决方法:
如果要在检测到任何水平滚动(滑动)时禁用ListVIEw的垂直滚动,请使用以下解决方案 – 覆盖自定义ListVIEw类中的ontouchEvent,并将动作MotionEvent.ACTION_MOVE替换为MotionEvent.ACTION_CANCEL:
public class ListVIEw2 extends ListVIEw{ private enum ScrollDirection { Horizontal, Vertical } private final int touchSlop; private float downX = 0; private float downY = 0; private ScrollDirection scrollDirection; public ListVIEw2(Context context, AttributeSet attrs) { super(context, attrs); touchSlop = VIEwConfiguration.get(context).getScaledtouchSlop(); } @OverrIDe public boolean ontouchEvent(MotionEvent ev) { switch (ev.getActionMasked()) { case MotionEvent.ACTION_MOVE: if (scrollDirection == null) { if (Math.abs(downX - ev.getX()) > touchSlop) scrollDirection = ScrollDirection.Horizontal; else if (Math.abs(downY - ev.getY()) > touchSlop) scrollDirection = ScrollDirection.Vertical; } if (scrollDirection == ScrollDirection.Horizontal) ev.setAction(MotionEvent.ACTION_CANCEL); break; case MotionEvent.ACTION_DOWN: scrollDirection = null; downX = ev.getX(); downY = ev.getY(); break; } return super.ontouchEvent(ev); }}
当然,您可以检查逻辑是否禁用垂直滚动更复杂.
总结以上是内存溢出为你收集整理的android – 如何在触摸/滑动/抛出其行时阻止ListView滚动?全部内容,希望文章能够帮你解决android – 如何在触摸/滑动/抛出其行时阻止ListView滚动?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)