197_ViewDragHelper的使用

来源:互联网 发布:广州少儿编程培训机构 编辑:程序博客网 时间:2024/06/01 10:53

ViewDragHelper的使用

现在有好多好多应用都用到了抽屉面板

就是滑动的时候

从左侧或者右侧拉出来

然后主界面会变小一点

 

我们要用到一个API

 

ViewDragHelper,看了一下,父类直接是Object

源码1500

 

/**

 * ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number

 * of useful operations and state tracking for allowing a user to drag and reposition

 * views within their parent ViewGroup.

 */

 

来看看源码中的注释

 

ViewDragHelper 是一个写自定义ViewGroup的通用类.

提供了好多对用户拖拽的实用操作和状态追踪

在父类ViewGroup中复位view

 

 

 

开始

 

我们先来个类继承FrameLayout

 

为什么继承FrameLayout,因为FrameLayout简单简洁效率高

 

public class DragLayout extends FrameLayout{

 

}

 

 

 

然后依然是来3个构造函数

 

    public DragLayout(Context context) {

        super(context);

    }

 

    public DragLayout(Context context, AttributeSet attrs) {

        super(context, attrs);

    }

 

    public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

    }

 

 

 

然后我们把它放到布局里面去

 

<com.lich.customcomponent.widget.DragLayout

    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"

    android:background="@drawable/bg"

    tools:context=".activity.MainActivity">

 

    <LinearLayout

        android:layout_width="match_parent"

        android:background="#ff0000"

        android:orientation="vertical"

        android:id="@+id/ll_left"

        android:layout_height="match_parent"></LinearLayout>

 

    <LinearLayout

        android:layout_width="match_parent"

        android:background="#00ff00"

        android:orientation="vertical"

        android:id="@+id/ll_right"

        android:layout_height="match_parent"></LinearLayout>

</com.lich.customcomponent.widget.DragLayout>

 

 

 

ViewDragHelper初始化三个步骤

 

1.创建ViewDragHelper对象

 

ViewDragHelper的构造函数是私有的

所以我们不能new出来

我们要调用create方法

我们先看看create方法

 

public static ViewDragHelper create(ViewGroup forParent, float sensitivity, Callback cb)

 

3个参数

1.ViewGroup forParent 父控件

2.float sensitivity 敏感度,默认1.0,敏感度越大越敏感

3.Callback cb 回调

 

这里的sensitivity敏感度和源码中的一个值mTouchSlop嘻嘻相关

mTouchSlop就是最小敏感范围,敏感度越小,那么范围越大,

敏感度越大,那么范围越小

 

 

    public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

 

        ViewDragHelper viewDragHelper = ViewDragHelper.create(this, 1.0f, callback);

    }

 

    ViewDragHelper.Callback callback=new ViewDragHelper.Callback() {

        @Override

        public boolean tryCaptureView(View child, int pointerId) {

            return false;

        }

    };

 

 

 

 

2.把拦截判断,触摸事件转交给ViewDragHelper

 

 

    @Override

    public boolean onInterceptTouchEvent(MotionEvent ev) {

        return mViewDragHelper.shouldInterceptTouchEvent(ev);

    }

 

    @Override

    public boolean onTouchEvent(MotionEvent event) {

 

        try {

            mViewDragHelper.processTouchEvent(event);

        } catch (Exception e) {

            e.printStackTrace();

        }

 

        return true;

    }

 

 

 

 

3.重写回调事件

 

 

    ViewDragHelper.Callback callback=new ViewDragHelper.Callback() {

        @Override

        public boolean tryCaptureView(View child, int pointerId) {

            return true;

        }

 

        @Override

        public int clampViewPositionHorizontal(View child, int left, int dx) {

            return left;

        }

    };

 

 

 

现在已经可以拖动两个LinearLayout

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 0
原创粉丝点击