自定义可拖拽View(工具类ViewDragHelper的用法)
来源:互联网 发布:mac 照片 文件夹 分类 编辑:程序博客网 时间:2024/06/05 17:36
android提供了GestureDetector
这个类用于常用手势识别, 如单击、双击、滑动等; 对于拖拽手势的分析识别并不理性,
因此support-v4包提供了一个工具类ViewDragHelper, 使用此工具类可以轻松实现可拖拽View(准确的说应该是支持拖拽的View容器),
闲话少说, 请看代码:
DragContainerLayout.java
/** * ViewDragHelper的用法 * 1. ViewDragHelper.create()创建实例 * 2. 初始化callback * 3. 将当前ViewGroup的onInterceptTouchEvent和onTouchEvent事件委托给ViewDragHelper处理 * 4. 处理拖拽view的边界(在callback中的clampViewPositionHorizontal和clampViewPositionVertical方法中处理) */public class DragContainerLayout extends LinearLayout{ private ViewDragHelper mViewDragHelper; public DragContainerLayout(Context context) { super(context); initViewDragHelper(); GestureDetector } public DragContainerLayout(Context context, AttributeSet attrs) { super(context, attrs); initViewDragHelper(); } public DragContainerLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initViewDragHelper(); } public DragContainerLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); initViewDragHelper(); } private void initViewDragHelper() { //ViewDragHelper.Callback是一个抽象类, 里面有更多的处理, 将需要处理的方法重写就可以了 mViewDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() { @Override public boolean tryCaptureView(View child, int pointerId) { Log.e("abc", "pointerId=" + pointerId); //第3个view不能截取事件 (也就是不能拖拽) if(getChildCount() >= 3) { if(child == getChildAt(2)) { return false; } } return true; //true表示child可以拖拽, false标示child不能拖拽 } @Override public int clampViewPositionHorizontal(View child, int left, int dx) { final int leftBound = getPaddingLeft(); final int rightBound = getWidth() - child.getWidth() - getPaddingRight();// int newLeft = left;// if(left < leftBound) newLeft = leftBound;// if(left > rightBound) newLeft = rightBound; //词句等价于上面的三句 int newLefth = Math.min(Math.max(left, leftBound), rightBound); return newLefth; } @Override public int clampViewPositionVertical(View child, int top, int dy) { final int topBound = getPaddingTop(); final int bottomBound = getHeight() - child.getHeight() - getPaddingBottom(); int newTop = Math.min(bottomBound, Math.max(topBound, top)); return newTop; } }); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = MotionEventCompat.getActionMasked(ev); if(action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { mViewDragHelper.cancel(); return false; } return mViewDragHelper.shouldInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { mViewDragHelper.processTouchEvent(event); return true; }}
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.stone.android_demo2.DragContainerLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:paddingTop="60dp" android:paddingLeft="0dp" android:paddingBottom="0dp" android:paddingRight="30dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" android:padding="10dp" android:background="#4400ccaa" android:layout_marginTop="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="哈哈哈哈哈" android:padding="10dp" android:background="#4400ccaa" android:layout_marginTop="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="wakaka" android:padding="10dp" android:background="#4400ccaa" android:layout_marginTop="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hi, hi, you you !" android:padding="10dp" android:background="#4400ccaa" android:layout_marginTop="10dp"/> </com.stone.android_demo2.DragContainerLayout></LinearLayout>
创建一个Activity, 使用上述布局, 运行, 试试拖拽TextView
0 0
- 自定义可拖拽View(工具类ViewDragHelper的用法)
- ViewDragHelper自定义view保存view的位置
- Android自定义ViewGroup工具类ViewDragHelper分析
- ViewDragHelper的用法
- 自定义View笔记(三) --- ViewDragHelper
- 自定义View笔记(三) --- ViewDragHelper
- ViewDragHelper实现自定义view侧滑菜单
- Android自定义View之图形图像工具类Path的特殊用法
- Android自定义View之图形图像工具类Path的特殊用法
- 自定义View(三) switch开关按钮 ViewDragHelper的使用初级
- android自定义View的用法
- 自定义view中自定义属性的用法.
- ViewDragHelper详解- 可拖动的view
- ViewDragHelper详解- 可拖动的view
- View滚动帮助类ViewDragHelper解析
- 自定义View(仿QQ侧滑删除实现,ViewDragHelper)
- Android 中 View移动总结:ViewDragHelper学习及用法详解
- Android 中 View移动总结:ViewDragHelper学习及用法详解
- Erwin 生成 mysql 带注释(comment )的脚本
- 程序笔试题(涂鸦移动)
- 耳机插入笔记本还是有外音解决办法
- tomcat 容器生命周期lifecycle
- 分享的方法(个人工作遇到的问题,有第三方,也有官方的方法)
- 自定义可拖拽View(工具类ViewDragHelper的用法)
- Evaluate Reverse Polish Notation
- Android 网络交互
- Unity3D-射线Ray
- Unity3D核心类型
- tomact如何处理一个http请求?
- Unable to locate appropriate constructor on class原因分析
- iOS 导出 ipa 包时 三个选项的意义
- Listview属性大全