DrawerLayout与ListView滑动冲突
Posted 小哇壁纸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DrawerLayout与ListView滑动冲突相关的知识,希望对你有一定的参考价值。
事件:当DrawerLayout的主内容是一个ListView时,滑动ListView经常会拉出菜单
解决方法:使用自定义控件替换DrawerLayout即可
1 import android.content.Context; 2 import android.support.v4.widget.DrawerLayout; 3 import android.util.AttributeSet; 4 import android.view.MotionEvent; 5 import android.view.ViewConfiguration; 6 7 /** 8 * Created by kwf on 2016/3/23 0023. 9 */ 10 public class CustomDrawerLayout extends DrawerLayout { 11 12 13 public CustomDrawerLayout(Context context) { 14 this(context, null); 15 } 16 17 public CustomDrawerLayout(Context context, AttributeSet attrs) { 18 this(context, attrs, 0); 19 } 20 21 public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle) { 22 super(context, attrs, defStyle); 23 final ViewConfiguration configuration = ViewConfiguration 24 .get(getContext()); 25 mTouchSlop = configuration.getScaledTouchSlop(); 26 } 27 28 private int mTouchSlop; 29 private float mLastMotionX; 30 private float mLastMotionY; 31 32 33 @Override 34 public boolean onInterceptTouchEvent(MotionEvent ev) { 35 try { 36 final float x = ev.getX(); 37 final float y = ev.getY(); 38 39 switch (ev.getAction()) { 40 case MotionEvent.ACTION_DOWN: 41 mLastMotionX = x; 42 mLastMotionY = y; 43 break; 44 45 case MotionEvent.ACTION_MOVE: 46 int xDiff = (int) Math.abs(x - mLastMotionX); 47 int yDiff = (int) Math.abs(y - mLastMotionY); 48 final int x_yDiff = xDiff * xDiff + yDiff * yDiff; 49 50 boolean xMoved = x_yDiff > mTouchSlop * mTouchSlop; 51 52 if (xMoved) { 53 if (xDiff > yDiff * 4) { 54 return true; 55 } else { 56 return false; 57 } 58 } 59 break; 60 default: 61 62 break; 63 } 64 return super.onInterceptTouchEvent(ev); 65 } catch (IllegalArgumentException ex) { 66 } 67 return false; 68 } 69 70 @Override 71 public boolean onTouchEvent(MotionEvent ev) { 72 try { 73 return super.onTouchEvent(ev); 74 } catch (IllegalArgumentException ex) { 75 } 76 return false; 77 } 78 }
以上是关于DrawerLayout与ListView滑动冲突的主要内容,如果未能解决你的问题,请参考以下文章
Android 中 DrawerLayout + ViewPager 怎么解决滑动冲突?
Scrollerview与listview或者gridview发生冲突
android viewpager滑动与slidingpanelayout冲突怎么解决