ViewDragHelper通用模板代码及应用
来源:互联网 发布:三菱plc模拟量编程实例 编辑:程序博客网 时间:2024/06/05 13:32
ViewDragHelper的通用代码模板如下:
public class DragLayout extends FrameLayout { private ViewDragHelper viewDragHelper; public DragLayout(Context context){ super(context); initHelper(); } public DragLayout(Context context, AttributeSet attrs){ super(context,attrs); initHelper(); } public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initHelper(); } //初始化ViewDragHelper private void initHelper(){ viewDragHelper=ViewDragHelper.create(this,callback); } //处理callback ViewDragHelper.Callback callback=new ViewDragHelper.Callback() { //何时开始触摸 @Override public boolean tryCaptureView(View child, int pointerId) { return 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; } //拖动结束后调用 @Override public void onViewReleased(View releaseChild,float xvel,float yvel){ super.onViewReleased(releaseChild,xvel,yvel); } //触摸到View时调用 @Override public void onViewCaptured(View capturedChild,int activePointerId){ super.onViewCaptured(capturedChild,activePointerId); } //触摸状态改变时调用,比如STATE_IDLE(没有拖动)、STATE_DRAGGING(正在拖动)、 //STATE_SETTLING(拖动结束) @Override public void onViewDragStateChanged(int state){ super.onViewDragStateChanged(state); } //触摸位置改变时调用,常用于滑动时更改scale进行缩放 @Override public void onViewPositionChanged(View changeView,int left,int top,int dx,int dy){ super.onViewPositionChanged(changeView,left,top,dx,dy); } }; //重写拦截事件 @Override public boolean onInterceptTouchEvent(MotionEvent event){ return viewDragHelper.shouldInterceptTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event){ //将触摸事件传递给ViewDragHelper viewDragHelper.processTouchEvent(event); return true; } //重写computeScroll @Override public void computeScroll(){ super.computeScroll(); if(viewDragHelper.continueSettling(true)){//如果需要继续滑动,就刷新界面 ViewCompat.postInvalidateOnAnimation(this); } }}
下面使用ViewDragHelper实现类似QQ的滑动侧边栏,具体代码如下:
public class DragLayout extends FrameLayout { private ViewDragHelper viewDragHelper; private View mainView,menuView; private int menuViewWidth; public DragLayout(Context context){ super(context); initHelper(); } public DragLayout(Context context, AttributeSet attrs){ super(context,attrs); initHelper(); } public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initHelper(); } //初始化ViewDragHelper private void initHelper(){ viewDragHelper=ViewDragHelper.create(this,callback); } //处理callback ViewDragHelper.Callback callback=new ViewDragHelper.Callback() { //何时开始触摸 @Override public boolean tryCaptureView(View child, int pointerId) { return mainView==child; } //处理水平滑动 @Override public int clampViewPositionHorizontal(View child,int left,int dx){ return left; } //处理垂直滑动 @Override public int clampViewPositionVertical(View child,int top,int dy){ return 0; } //拖动结束后调用 @Override public void onViewReleased(View releaseChild,float xvel,float yvel){ super.onViewReleased(releaseChild,xvel,yvel); if(mainView.getLeft()<menuViewWidth/2){//关闭菜单 viewDragHelper.smoothSlideViewTo(mainView,0,0); }else {//打开菜单 viewDragHelper.smoothSlideViewTo(mainView,menuViewWidth,0); } ViewCompat.postInvalidateOnAnimation(DragLayout.this); } }; //重写拦截事件 @Override public boolean onInterceptTouchEvent(MotionEvent event){ return viewDragHelper.shouldInterceptTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event){ //将触摸事件传递给ViewDragHelper viewDragHelper.processTouchEvent(event); return true; } //重写computeScroll @Override public void computeScroll(){ super.computeScroll(); if(viewDragHelper.continueSettling(true)){ ViewCompat.postInvalidateOnAnimation(this); } } @Override public void onFinishInflate(){ super.onFinishInflate(); menuView=getChildAt(0); mainView=getChildAt(1); } @Override public void onSizeChanged(int w,int h,int oldW,int oldH){ super.onSizeChanged(w,h,oldW,oldH); menuViewWidth=menuView.getMeasuredWidth(); }}
activity_main布局如下:
<?xml version="1.0" encoding="utf-8"?><com.example.draghelperdemo.DragLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="400dp" android:layout_height="match_parent" android:gravity="center" android:background="@color/colorAccent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="菜单布局"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@color/colorPrimary"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="主页布局"/> </LinearLayout></com.example.draghelperdemo.DragLayout>
阅读全文
0 0
- ViewDragHelper通用模板代码及应用
- ViewDragHelper的简单分析及应用(二)
- 模板方法模式--介绍、应用及代码
- Android应用ViewDragHelper详解及部分源码浅析
- ViewDragHelper使用模板
- 基于模板的通用代码生成器LKGenerator(二)-使用时的关键步骤,注意事项及实例
- 通用对话框应用代码片断
- 通用Makefile原理及应用
- java数据库连接模板代码通用收集
- tarjan模板及应用
- 基于模板的通用代码生成器LKGenerator(三)-模板示例
- ViewDragHelper打造简单通用的PullToRefresh
- DragLayout测试代码(ViewDragHelper)
- 通用模板
- 通用模板
- 通用模板
- C++函数模板及应用
- C++函数模板及应用
- 数据库相关的资料汇总
- php版本间区别
- 基于labVIEW 的excle报表生成
- 带有哨兵的双向循环链表
- 简单的json数据分页操作
- ViewDragHelper通用模板代码及应用
- CvMat、Mat、IplImage之间的转换 ***** **
- 谈谈inline-block的几个神奇的用法
- linux的dns被劫持(解决方案)
- 网络知识
- asp:TreeView UpdatePanel学习笔记
- 【前端知识点】关于跨域的理解及解决方法
- 关于 ie 下使用 iframe 加载子页面时,子页面ajax数据能进入success方法,但是数据为空的问题
- webpack打包