ViewDragHelper 任意拖动

自定义view 拖动的边界需要严谨控制

view

 1 public class DragView extends LinearLayout{
 2 
 3     private View childView;
 4     private ViewDragHelper mDragHelper;
 5 
 6     public DragView(Context context) {
 7         this(context, null);
 8     }
 9 
10     public DragView(Context context, AttributeSet attrs) {
11         this(context, attrs, 0);
12     }
13 
14     public DragView(Context context, AttributeSet attrs, int defStyleAttr) {
15         super(context, attrs, defStyleAttr);
16         initializeView();
17     }
18 
19     private void initializeView() {
20         mDragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {
21             @Override
22             public boolean tryCaptureView(View child, int pointerId) {
23                 return true;
24             }
25 
26             @Override
27             public int clampViewPositionVertical(View child, int top, int dy) {
28                 return Math.min(Math.max(top, 0), getHeight()-child.getHeight());
29             }
30 
31             @Override
32             public int clampViewPositionHorizontal(View child, int left, int dx) {
33                 return Math.min(getWidth() - child.getWidth(), Math.max(left, 0));
34             }
35 
36             @Override
37             public int getViewVerticalDragRange(View child) {
38                 return child.getMeasuredHeight();
39             }
40 
41             @Override
42             public int getViewHorizontalDragRange(View child) {
43                 return child.getMeasuredWidth();
44             }
45 
46             @Override
47             public void onEdgeDragStarted(int edgeFlags, int pointerId) {
48                 mDragHelper.captureChildView(childView,pointerId);
49             }
50         });
51         mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
52     }
53 
54     @Override
55     public boolean onInterceptTouchEvent(MotionEvent ev) {
56         return mDragHelper.shouldInterceptTouchEvent(ev);
57     }
58 
59     @Override
60     public boolean onTouchEvent(MotionEvent event) {
61         mDragHelper.processTouchEvent(event);
62         return true;
63     }
64 
65     @Override
66     public void computeScroll() {
67         if(mDragHelper.continueSettling(true)) {
68             invalidate();
69         }
70     }
71 
72     @Override
73     protected void onFinishInflate() {
74         super.onFinishInflate();
75         if(getChildCount() < 1) {
76             throw new IllegalArgumentException("必须有一个child");
77         }
78         childView = getChildAt(0);
79     }
80 }

 

 

 

xml文件:

 1 <cn.haoju.mytest.view.DragView
 2         android:layout_width="match_parent"
 3         android:layout_height="match_parent"
 4         android:orientation="vertical"
 5         >
 6         <Button
 7         android:layout_margin="10dp"
 8         android:gravity="center"
 9         android:layout_gravity="center"
10         android:background="#aabbcc"
11         android:text="拖我"
12         android:layout_width="100dp"
13         android:layout_height="100dp"/>
14 
15     </cn.haoju.mytest.view.DragView>

几行代码就可实现任意拖动了

posted @ 2015-08-20 17:34  nihao1314520  阅读(405)  评论(0编辑  收藏  举报