自定义DragFrameLayout实现拖动子控件的效果

DragFrameLayout实现拖动子控件的效果

效果图

这里写图片描述

介绍

DragFrameLayout继承自FrameLayout,可以对其内部的子View进行拖曳,子View的数量不限,一次只可以拖动一个view。
当然也可以继承RealtiveLayout

使用步骤

  1. 将这个类DragFrameLayout放入xml中,里面放入需要拖动的View,
  2. 获取DragFrameLayout和子View
  3. 将子View作为可拖动的view将入DragFrameLayout中(实际是放入内部集合中)

//注意:不是addView(tv),是addDragView(tv)

DragFrameLayout dragFrameLayout = (DragFrameLayout) findViewById(R.id.dragFrameLayout);
TextView tv = (TextView) findViewById(R.id.tv);
dragFrameLayout.addDragView(tv);

DragFrameLayout源码介绍

  1. 首先我们继承FrameLayout,需要重写4个构造方法,其中4个参数的构造方法是用于API>=21,所以,我们只需要重写其余3个构造方法即可。
  2. 创建接口用于拖动控件的处理
  3. 事件分发与拦截
  4. 在其构造方法里面创建ViewDragHelper对象并重写回调中的方法

第一步:创建拖动回调

下面有完整版代码,类和方法的名字可能不同,但是步骤逻辑处理是一样的。

public interface OnDragDropListener {
    void onDragDrop(boolean captured);
}

private OnDragDropListener onDragDropListener;

public void setOnDragDropListener(OnDragDropListener onDragDropListener) {
    this.onDragDropListener = onDragDropListener;
}

第二步:重写构造方法

注意:有一个参数和两个参数的构造方法中用的是this,不是super,这样会调用3个参数的构造方法。
在3个参数的构造方法中创建viewList,用于存放我们要拖动的childView;并对外公开添加的方法
创建ViewDragHelper对象,再起回调中重写5个方法

回调中重写的方法 说明
boolean tryCaptureView(View child, int pointerId) 是否捕获childView: 如果viewList包含child,那么捕获childView;如果不包含child,就不捕获childView
int clampViewPositionHorizontal(View child, int left, int dx) 到左边界的距离
int clampViewPositionVertical 到上边界的距离
onViewCaptured(View capturedChild, int activePointerId) 当捕获到child后的处理:获取child的监听
void onViewReleased(View releasedChild, float xvel, float yvel) 当释放child后的处理:取消监听,不再处理
void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) 不需要重写
这里写图片描述
public class DragFrameLayout extends FrameLayout {
    public DragFrameLayout(@NonNull Context context) {
        this(context,null);
    }

    public DragFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DragFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //第二步:创建存放View的集合
        viewList = new ArrayList<>();

        dragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {

            /**
             * 是否捕获childView:
             * 如果viewList包含child,那么捕获childView
             * 如果不包含child,就不捕获childView
             */
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                return viewList.contains(child);
            }

            @Override
            public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
                super.onViewPositionChanged(changedView, left, top, dx, dy);
            }

            /**
             * 当捕获到child后的处理:
             * 获取child的监听
             */
            @Override
            public void onViewCaptured(View capturedChild, int activePointerId) {
                super.onViewCaptured(capturedChild, activePointerId);
                if (onDragDropListener != null) {
                    onDragDropListener.onDragDrop(true);
                }
            }

            /**
             * 当释放child后的处理:
             * 取消监听,不再处理
             */
            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                super.onViewReleased(releasedChild, xvel, yvel);
                if (onDragDropListener != null) {
                    onDragDropListener.onDragDrop(false);
                }
            }

            /**
             * 到左边界的距离
             */
            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                return left;
            }

            /**
             * 到上边界的距离
             */
            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                return top;
            }
        });
    }
}

由于viewList是用private修饰的,所以需要对外公开的添加chiildView的方法

public void addDragChildView(View child) {
    viewList.add(child);
}

第三步:处理事件

需要重写2个方法:是否拦截事件和自身如何处理事件,分别是:onInterceptTouchEvent(MotionEvent ev)onTouchEvent(MotionEvent event)

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    //当手指抬起或事件取消的时候 就不拦截事件
    int actionMasked = ev.getActionMasked();
    if (actionMasked == MotionEvent.ACTION_CANCEL || actionMasked == MotionEvent.ACTION_UP) {
        return false;
    }
    return dragHelper.shouldInterceptTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    dragHelper.processTouchEvent(event);
    return true;
}

其它

demo:https://git.oschina.net/customView/CustomDragFrameLayout
demo是从google Android Sample中抽取出来的:https://developer.android.google.cn/samples/ElevationDrag/index.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,638评论 25 709
  • 内容是博主照着书敲出来的,博主码字挺辛苦的,转载请注明出处,后序内容陆续会码出。 当了解了Android坐标系和触...
    Blankj阅读 6,726评论 3 61
  • ViewDragHelper实例的创建 ViewDragHelper重载了两个create()静态方法public...
    傀儡世界阅读 681评论 0 3
  • 迦罗奇楠香, 月银白, 春夜三更无眠。 浅墨桃花笺, 七弦乱, 抚尽满怀相思。 孤影中堂, 酒阁只闻笼雀鸣。 红松...
    手握瓷杯阅读 223评论 1 4
  • 有一种感动叫做哭 为一首歌好听到哭 为女神美到哭 为偶像帅到哭 眼睛没哭 心却流下了泪痕 耳朵为了一种声音哭 眼睛...
    萌龙在天阅读 197评论 0 1