ViewDragHelper is a View drag helper provided by the V4 package, which can be used to handle View drags easily, such as the probe card function.
Create a ViewGroup control:
public class SlideView extends FrameLayout { public SlideView(@NonNull Context context) { this(context, null); } public SlideView(@NonNull Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public SlideView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }}Copy the code
2. Create a ViewDragHelper object:
- ViewDragHelper structure:
static ViewDragHelper create(ViewGroup forParent, float sensitivity, ViewDragHelper.Callback cb)
static ViewDragHelper create(ViewGroup forParent, ViewDragHelper.Callback cb)
Copy the code
- Create an object using a static method:
private void initDragHelper() {
dragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {Override public Boolean tryCaptureView(View child, int pointerId) {// ReturntrueCan drag and dropreturn true; } / / manual Override method (horizontal slip need to rewrite this method) @ Override public int clampViewPositionHorizontal (View child, int left, int dx) {returnleft; } / / manual Override method (vertical sliding need to rewrite this method) @ Override public int clampViewPositionVertical (View child, int top, int dy) {returntop; }}); @override public Boolean onInterceptTouchEvent(MotionEvent ev) {returndragHelper.shouldInterceptTouchEvent(ev); Override public Boolean onTouchEvent(MotionEvent) { dragHelper.processTouchEvent(event);return true;
}
Copy the code
3. Create the layout file and introduce the SlideView we wrote:
<viewdrag.chao.com.viewdraghelper.SlideView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#50ff0000"
android:gravity="center"
android:text="I am the text"
android:textSize="30sp"
android:textStyle="bold" />
</viewdrag.chao.com.viewdraghelper.SlideView>
Copy the code
- Preview:
4. The restrictions:
- Restrict drag-and-drop to a View:
@Override
public boolean tryCaptureView(View child, int pointerId) {
returnchild==getChildAt(1); // Only the second View can be dragged}Copy the code
- Boundary restrictions:
@ Override public int clampViewPositionHorizontal (View child, int left, int dx) {/ / limit on the left border lowest is 0if (child.getLeft() + left <= 0) {
return 0;
}
return left;
}
Copy the code
- Gesture release callback (override the onViewReleased callback for ViewDragHelper) :
@Override
public void onViewReleased(View releasedChild, float xvel, floatyvel) { super.onViewReleased(releasedChild, xvel, yvel); dragHelper.settleCapturedViewAt(0, 0); Invalidate (); }Copy the code
Then override the smooth scrolling method of the View class:
@Override
public void computeScroll() {
super.computeScroll();
if(dragHelper ! = null && dragHelper.continueSettling(true)) { invalidate(); }}Copy the code
Preview:
Is it easy to drag and drop a View? Zoom in on the image, do some boundary control and some simple animation to achieve the card sliding effect of probing.
Because the code is not much, only wrote a test class, will not upload the warehouse, the following post source:
package viewdrag.chao.com.viewdraghelper;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
/**
* Created by Chao on 2018-02-03.
*/
public class SlideView extends FrameLayout {
private ViewDragHelper dragHelper;
public SlideView(@NonNull Context context) {
this(context, null);
}
public SlideView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public SlideView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initDragHelper();
}
private void initDragHelper() {
dragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return true;
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if (child.getLeft() + left <= 0) {
return 0;
}
return left;
}
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
return top;
}
@Override
public void onViewReleased(View releasedChild, float xvel, floatyvel) { super.onViewReleased(releasedChild, xvel, yvel); dragHelper.settleCapturedViewAt(0, 0); invalidate(); }}); } @Override public voidcomputeScroll() {
super.computeScroll();
if(dragHelper ! = null && dragHelper.continueSettling(true)) {
invalidate();
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return dragHelper.shouldInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
dragHelper.processTouchEvent(event);
return true; }}Copy the code