类似ScrollView的写法,可实现子View的拖拽及复位到边界功能
效果图
使用方式
<com.fqxyi.utils.view.DragLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:background="#12345678"
android:onClick="testViewDrag"
android:paddingLeft="20dp"
android:gravity="center_horizontal"
android:layout_marginRight="10dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可拖拽按钮"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
</com.fqxyi.utils.view.DragLayout>
package com.fqxyi.utils.view;
import android.content.Context;
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;
public class DragLayout extends FrameLayout {
private ViewDragHelper mDragHelper;
private boolean leftOrRight;
private View child;
public DragLayout(Context context) {
super(context);
init();
}
public DragLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public DragLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragCallback());
}
private class ViewDragCallback extends ViewDragHelper.Callback {
@Override
public boolean tryCaptureView(View view, int pointerId) {
return view == child;
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) child.getLayoutParams();
if (params == null) {
if (getPaddingLeft() > left) {
return getPaddingLeft();
}
if (getWidth() - child.getWidth() - getPaddingRight() < left) {
return getWidth() - child.getWidth() - getPaddingRight();
}
} else {
if (getPaddingLeft() + params.leftMargin > left) {
return getPaddingLeft() + params.leftMargin;
}
if (getWidth() - child.getWidth() - getPaddingRight() - params.rightMargin < left) {
return getWidth() - child.getWidth() - getPaddingRight() - params.rightMargin;
}
}
return left;
}
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) child.getLayoutParams();
if (params == null) {
if (getPaddingTop() > top) {
return getPaddingTop();
}
if (getHeight() - child.getHeight() - getPaddingBottom() < top) {
return getHeight() - child.getHeight();
}
} else {
if (getPaddingTop() + params.topMargin > top) {
return getPaddingTop() + params.topMargin;
}
if (getHeight() - child.getHeight() - getPaddingBottom() - params.bottomMargin < top) {
return getHeight() - child.getHeight() - getPaddingBottom() - params.bottomMargin;
}
}
return top;
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) releasedChild.getLayoutParams();
if (params == null) {
if (leftOrRight) {
mDragHelper.settleCapturedViewAt(getPaddingLeft(), (int) releasedChild.getY());
} else {
mDragHelper.settleCapturedViewAt(getWidth() - releasedChild.getWidth() - getPaddingRight(), (int) releasedChild.getY());
}
} else {
if (leftOrRight) {
mDragHelper.settleCapturedViewAt(getPaddingLeft() + params.leftMargin, (int) releasedChild.getY());
} else {
mDragHelper.settleCapturedViewAt(getWidth() - releasedChild.getWidth() - getPaddingRight() - params.rightMargin, (int) releasedChild.getY());
}
}
invalidate();
}
@Override
public int getViewHorizontalDragRange(View child) {
return getMeasuredWidth() - child.getMeasuredWidth();
}
@Override
public int getViewVerticalDragRange(View child) {
return getMeasuredHeight() - child.getMeasuredHeight();
}
@Override
public void onViewDragStateChanged(int state) {
switch (state) {
case ViewDragHelper.STATE_DRAGGING:
break;
case ViewDragHelper.STATE_IDLE:
break;
case ViewDragHelper.STATE_SETTLING:
break;
}
super.onViewDragStateChanged(state);
}
}
@Override
public void computeScroll() {
if (mDragHelper.continueSettling(true)) {
invalidate();
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mDragHelper.shouldInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (checkNeedIntercept(event)) {
return true;
}
break;
case MotionEvent.ACTION_UP:
if (event.getX() >= getWidth() / 2) {
leftOrRight = false;
} else {
leftOrRight = true;
}
break;
}
mDragHelper.processTouchEvent(event);
return false;
}
private boolean checkNeedIntercept(MotionEvent event) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) child.getLayoutParams();
if (params == null) {
if (child.getX() - getPaddingLeft() != 0 && child.getX() + child.getWidth() + getPaddingRight() != getWidth()) {
mDragHelper.processTouchEvent(event);
return true;
}
} else {
if (child.getX() - getPaddingLeft() - params.leftMargin != 0 && child.getX() + child.getWidth() + getPaddingRight() + params.rightMargin != getWidth()) {
mDragHelper.processTouchEvent(event);
return true;
}
}
return false;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (getChildCount() != 1) {
throw new IllegalStateException("DragLayout can host only one direct child");
}
child = getChildAt(0);
child.setClickable(true);
}
}