自定义Viw之使用ViewDragHelper做条目的侧滑菜单
2017-01-06 18:58
393 查看
1.写一个类继承ViewGroup
2.使用自定义View
public class DragHelperView extends ViewGroup { private static final String TAG = "DragHelperView"; private View mChildLeft; private View mChildRight; private ViewDragHelper mHelper; public DragHelperView(Context context) { super(context); } public DragHelperView(Context context, AttributeSet attrs) { super(context, attrs); mHelper = ViewDragHelper.create(this , mCallback); } ViewDragHelper.Callback mCallback = new ViewDragHelper.Callback() { /** * @param child 点击到的孩子 * @param pointerId * @return false:不孩子允许拖动 true:允许孩子拖动 */ @Override public boolean tryCaptureView(View child, int pointerId) { return true; } /** * 在action_move的时候调用 * @param child 拖动的孩子 * @param left 拖动孩子左边位置的参考值 * @param dx * @return 孩子最终左边的位置 */ @Override public int clampViewPositionHorizontal(View child, int left, int dx) { int minLeft = 0 ; int maxLeft = 0; if (child == mChildLeft){ minLeft = -mChildRight.getWidth(); maxLeft = 0; }else if (child == mChildRight){ minLeft = mChildLeft.getWidth() - mChildRight.getWidth(); maxLeft = mChildLeft.getWidth(); } if (left < minLeft){ left = minLeft; }else if (left > maxLeft){ left = maxLeft; } return left; } /** * 在view位置发生变化的时候调用 * @param dx 孩子x轴移动的偏移量 * @param dy 孩子y轴移动的偏移量 */ @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { if (changedView == mChildLeft){ mChildRight.layout(mChildRight.getLeft() + dx , 0 , mChildRight.getRight() + dx , mChildRight.getHeight()); }else if (changedView == mChildRight){ mChildLeft.layout(mChildLeft.getLeft() + dx , 0 , mChildLeft.getRight() + dx , mChildLeft.getHeight()); } } /** * 在action_up是调用 * @param releasedChild * @param xvel * @param yvel */ @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { //临界点 int middle = mChildLeft.getWidth() - mChildRight.getWidth()/2; int finalLeft = 0; if (mChildRight.getLeft() > middle){ finalLeft = mChildLeft.getWidth(); // mHelper.smoothSlideViewTo(mChildLeft , 0 , 0); isOpen = false; }else { finalLeft = mChildLeft.getWidth() - mChildRight.getWidth(); // mHelper.smoothSlideViewTo(mChildLeft , -mChildRight.getWidth() , 0); isOpen = true; } mHelper.smoothSlideViewTo(mChildRight , finalLeft , 0); invalidate();//触发重新绘制 } }; boolean isOpen = false; public boolean isOpen(){ return isOpen; } @Override public void computeScroll() { super.computeScroll(); //continueSettling(true):返回值为true,表示滚动动画还没有结束 if (mHelper.continueSettling(true)){ //触发重新绘制 // invalidate(); postInvalidate(); //在虚拟机上选中了Use Host GPU // ViewCompat.postInvalidateOnAnimation(this);//兼容各大厂商 } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mChildLeft = getChildAt(0); mChildRight = getChildAt(1); LayoutParams leftParams = mChildLeft.getLayoutParams(); int leftHeight = MeasureSpec.makeMeasureSpec(leftParams.height , MeasureSpec.EXACTLY); mChildLeft.measure(widthMeasureSpec , leftHeight); LayoutParams rightParams = mChildRight.getLayoutParams(); int rightWidth = MeasureSpec.makeMeasureSpec(rightParams.width , MeasureSpec.EXACTLY); 4000 int rightHeight = MeasureSpec.makeMeasureSpec(rightParams.height , MeasureSpec.EXACTLY); mChildRight.measure(rightWidth , rightHeight); //拆分大小 int measureWidth = MeasureSpec.getSize(widthMeasureSpec); int measureHeight = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(measureWidth , measureHeight); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { mChildLeft.layout(0 , 0, mChildLeft.getMeasuredWidth() , mChildLeft.getMeasuredHeight()); mChildRight.layout(mChildLeft.getWidth() , 0 , mChildLeft.getWidth() + mChildRight.getMeasuredWidth() , mChildRight.getMeasuredHeight()); } @Override public boolean onTouchEvent(MotionEvent event) { //触摸事件交给ViewDragHelper mHelper.processTouchEvent(event); return true; } }
2.使用自定义View
<LinearLayout 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"> <com.myself.viewdraghelper.DragHelperView android:layout_width="match_parent" android:layout_height="80dp" android:id="@+id/dh_container"> <include layout="@layout/child_left"/> <include layout="@layout/child_right"/> </com.myself.viewdraghelper.DragHelperView> </LinearLayout>
相关文章推荐
- Android使用ViewDragHelper实现侧滑菜单(一)
- Android群英传——第五章实现滑动的7种方式(六七)ViewDragHelper自定义侧滑菜单
- ViewDragHelper实现自定义view侧滑菜单
- ViewDragHelper实现QQ条目侧滑效果
- 自定义ViewGroup+ViewDragHelper —— 侧滑菜单
- Android使用ViewDragHelper实现QQ6.X最新版本侧滑界面效果实例代码
- ViewDragHelper的妙用二 --QQ侧滑菜单的实现
- 使用 ViewDragHelper 实现左右侧滑
- ViewDragHelper之自定义侧滑菜单
- Android 使用ViewDragHelper实现向slidingMenu侧滑菜单的效果
- android: 侧滑菜单的实现(ViewDragHelper)
- ViewDragHelper实践之仿Android官方侧滑菜单NavigationDrawer效果
- android 使用ViewDragHelper轻松实现DrawerLayout和SlidMenu侧滑效果
- 仿QQ6.0侧滑之ViewDragHelper的使用(一)
- ViewDragHelper的使用
- ViewDragHelper实现微信侧滑
- ViewDragHelper 实现侧滑(二)
- ViewDragHelper使用详解
- Android ViewDragHelper实现QQ侧滑边栏
- ViewDragHelper做侧滑时与ListView的上下拉刷新冲突的解决办法