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 }

 

posted @ 2016-03-23 11:44  请输入...昵称  阅读(2238)  评论(0编辑  收藏  举报