ViewDragHelper (three ways to drag controls), viewdraghelper drag
++ Method 1 ++ ++
Activity_main.xml layout File
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.example.draghelper.CustomView android:layout_width="match_parent" android:layout_height="match_parent" > <View android:layout_width="100dp" android:layout_height="100dp" android:background="#FFFF0000" /> </com.example.draghelper.CustomView></RelativeLayout>
The mview code comments are detailed.
Package com. example. draghelper; import android. content. context; import android. support. v4.widget. viewDragHelper; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; import android. widget. linearLayout; import android. widget. toast; public class CustomView extends LinearLayout {private ViewDragHelper mDragHelper; private Context context; /******************************** * ******************* Three constructors */public CustomView (Context context) {super (context ); this. context = context; init ();} public CustomView (Context context, AttributeSet attrs) {super (context, attrs); this. context = context; init ();} public CustomView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); this. context = context; init ();}/*** @ params ViewGroup forParent A ViewGroup That is, ViewDragHelper will be used to drag the sub-view under whom * @ params float sensitivity, usually 1.0f * @ params Callback cb a Callback, used to process the drag to the position */private void init () {mDragHelper = ViewDragHelper. create (this, 1.0f, new ViewDragCallback ();}/*** indicates to capture the subview. true must be returned here. true is returned to allow. * The child tries to capture the view * @ param int pointerId indicator id? You can determine which sub-view can be dragged */private class ViewDragCallback extends ViewDragHelper. callback {@ Overridepublic boolean tryCaptureView (View view, int pointerId) {return true ;} /*************************************** * ****************************** these two methods are used to process the x direction and y direction respectively. direction of the drag, returns the current location of the child. * @ Param View child is dragged to view * @ param int left the distance between the x axis that is reached * @ param int dx the suggested distance between x and * clampViewPositionHorizontal is the current drag the x coordinate to which the child view should arrive. * Therefore, the second parameter can be returned based on common sense. However, in order to prevent the dragged view from being dragged when it encounters a boundary, make more considerations for the returned value */@ Overridepublic int clampViewPositionHorizontal (View child, int left, int dx) {Toast. makeText (context, "left =" + left + ", dx =" + dx, 0 ). show (); // two if values are mainly used to make if (getPaddingLeft ()> left) {return getPaddingLeft ();} if (getWidth ()-child. getWidth () <left) {return getWidth ()-child. getWidth ();} return left;}/*** process the vertical side Drag ** @ param View ** child up to view * @ param int top to the y axis distance * @ param int dy suggested to move y distance */@ Overridepublic int clampViewPositionVertical (View child, int top, int dy) {// The two if statements are mainly used to make if (getPaddingTop ()> top) {return getPaddingTop ();} if (getHeight ()-child. getHeight () <top) {return getHeight ()-child. getHeight ();} return top;}/*** call back the ** @ params new state */@ Overridepublic v Oid onViewDragStateChanged (int state) {switch (state) {case ViewDragHelper. STATE_DRAGGING: // The break is being dragged; case ViewDragHelper. STATE_IDLE: // view is not dragged break; case ViewDragHelper. STATE_SETTLING: // After fling is completed, it is placed in a position break;} super. onViewDragStateChanged (state );}} /*************************************** * ************************* 1, but returns True (this indicates that the event will be processing) * // @ Overridepublic boolean onInterceptTouchEv Ent (MotionEvent ev) {switch (ev. getAction () {/*** for example, this event is triggered when you drag a listView or ScrollView on the screen rather than pressing the button above */case MotionEvent. ACTION_UP: case MotionEvent. ACTION_DOWN: mDragHelper. cancel (); // similar to the ACTION_UP event of onTouch, clear break;}/*** whether the MotionEvent should be interrupted */return mDragHelper. shouldInterceptTouchEvent (ev) ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {/*** handle intercepted events this method will distribute events before returning */mDragHelpe R. processTouchEvent (event);/*** return true indicates that the event is consumed. */Return true ;}}
++ Method 2 ++ ++
DragLayout
Package com. example. demo; import android. content. context; import android. support. v4.widget. viewDragHelper; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; import android. widget. relativeLayout; public class DragLayout extends RelativeLayout {private final ViewDragHelper mDragHelper; final ViewDragHelper. callback mDragCallBack = new ViewDragHelper. callback () {@ Overridepublic int clampViewPositionHorizontal (View child, int left, int dx) {return left ;}; @ Overridepublic int clampViewPositionVertical (View child, int top, int dy) {return top ;}; @ Overridepublic void onViewPositionChanged (View changedView, int left, int top, int dx, int dy) {invalidate ();} /*** the return value of the tryCaptureView method of DragHelperCallback can be used to determine which child view in a parentview can be dragged */@ Overridepublic boolean tryCaptureView (View arg0, int arg1) {return true ;}}; public DragLayout (Context context, AttributeSet attrs) {super (context, attrs); mDragHelper = ViewDragHelper. create (this, 1.0f, mDragCallBack);} @ Overridepublic boolean onInterceptTouchEvent (MotionEvent ev) {return mDragHelper. shouldInterceptTouchEvent (ev) ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {mDragHelper. processTouchEvent (event); return true ;}}
++ Method 3 ++ ++
Http://blog.csdn.net/u013210620/article/details/46378231
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.