自定义Viw之使用ViewDragHelper做条目的侧滑菜单

来源:互联网 发布:python是面向对象语言 编辑:程序博客网 时间:2024/05/18 22:45

1.写一个类继承ViewGroup

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);        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>
0 0
原创粉丝点击