|
| 1 | +package com.yanbober.viewdraghelper_demo.view; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.support.v4.view.ViewCompat; |
| 5 | +import android.support.v4.widget.ViewDragHelper; |
| 6 | +import android.util.AttributeSet; |
| 7 | +import android.view.MotionEvent; |
| 8 | +import android.view.View; |
| 9 | +import android.view.ViewConfiguration; |
| 10 | +import android.view.ViewGroup; |
| 11 | +import android.widget.LinearLayout; |
| 12 | +/** |
| 13 | + * 头部可伸缩ViewGroup |
| 14 | + * |
| 15 | + * 特别注意,这里result直接返回true,混合式使用时手势自己处理一下,这里主要介绍ViewDragHelper用法而已。 |
| 16 | + */ |
| 17 | +public class WanDouJiaLayout extends LinearLayout { |
| 18 | + private static final String TAG = "WanDouJiaLayout"; |
| 19 | + |
| 20 | + private View mTopView; |
| 21 | + private View mBottomView; |
| 22 | + |
| 23 | + private ViewDragHelper mHelper; |
| 24 | + |
| 25 | + private int mBottomViewTop = 0; |
| 26 | + |
| 27 | + private float mCurX, mCurY; |
| 28 | + |
| 29 | + private boolean mIsFirst = true; |
| 30 | + |
| 31 | + public WanDouJiaLayout(Context context) { |
| 32 | + this(context, null); |
| 33 | + } |
| 34 | + |
| 35 | + public WanDouJiaLayout(Context context, AttributeSet attrs) { |
| 36 | + this(context, attrs, 0); |
| 37 | + } |
| 38 | + |
| 39 | + public WanDouJiaLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
| 40 | + super(context, attrs, defStyleAttr); |
| 41 | + initView(); |
| 42 | + } |
| 43 | + |
| 44 | + private void initView() { |
| 45 | + setOrientation(LinearLayout.VERTICAL); |
| 46 | + |
| 47 | + mHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelperCallBack()); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void onFinishInflate() { |
| 52 | + super.onFinishInflate(); |
| 53 | + |
| 54 | + if (getChildCount() != 2) { |
| 55 | + throw new RuntimeException("WanDouJiaLayout only need two View for child!"); |
| 56 | + } |
| 57 | + |
| 58 | + mTopView = getChildAt(0); |
| 59 | + mBottomView = getChildAt(1); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 64 | + //关于与ListView等混合时手势这里不给出了,自己处理一下即可!!!!!!!!!!! |
| 65 | + //特别注意,这里result直接返回true,混合式使用时手势自己处理一下,这里主要介绍ViewDragHelper用法而已。 |
| 66 | + boolean result = true; |
| 67 | + return mHelper.shouldInterceptTouchEvent(ev) & result; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean onTouchEvent(MotionEvent event) { |
| 72 | + mHelper.processTouchEvent(event); |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void computeScroll() { |
| 78 | + super.computeScroll(); |
| 79 | + if (mHelper.continueSettling(true)) { |
| 80 | + ViewCompat.postInvalidateOnAnimation(this); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 86 | + super.onLayout(changed, l, t, r, b); |
| 87 | + |
| 88 | + if (mIsFirst) { |
| 89 | + mBottomViewTop = mBottomView.getTop(); |
| 90 | + mIsFirst = false; |
| 91 | + } |
| 92 | + |
| 93 | + int offset = mTopView.getHeight() - mBottomViewTop; |
| 94 | + mTopView.layout(mTopView.getLeft(), -offset, |
| 95 | + mTopView.getRight(), mBottomViewTop); |
| 96 | + |
| 97 | + ViewGroup.LayoutParams params = mBottomView.getLayoutParams(); |
| 98 | + params.height = params.height + offset; |
| 99 | + mBottomView.setLayoutParams(params); |
| 100 | + |
| 101 | + mBottomView.layout(mBottomView.getLeft(), mBottomViewTop, |
| 102 | + mBottomView.getRight(), mBottomViewTop + mBottomView.getHeight()); |
| 103 | + } |
| 104 | + |
| 105 | + private class ViewDragHelperCallBack extends ViewDragHelper.Callback { |
| 106 | + @Override |
| 107 | + public boolean tryCaptureView(View child, int pointerId) { |
| 108 | + if (child == mTopView) { |
| 109 | + mHelper.captureChildView(mBottomView, pointerId); |
| 110 | + } |
| 111 | + return mBottomView == child; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public int clampViewPositionVertical(View child, int top, int dy) { |
| 116 | + //实时变化,最大挪动位置限制处理,如果想要回弹效果这个区域可以大一点 |
| 117 | + return Math.min(Math.max(0, top), mTopView.getMeasuredHeight()); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public int getViewVerticalDragRange(View child) { |
| 122 | + //灵敏度计算区域 |
| 123 | + return getMeasuredHeight(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void onViewReleased(View releasedChild, float xvel, float yvel) { |
| 128 | + int top = (yvel < 0 && mBottomViewTop < mTopView.getHeight()/2) ? 0 : mTopView.getMeasuredHeight(); |
| 129 | + mHelper.settleCapturedViewAt(mBottomView.getLeft(), top); |
| 130 | + postInvalidate(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { |
| 135 | + super.onViewPositionChanged(changedView, left, top, dx, dy); |
| 136 | + mBottomViewTop = top; |
| 137 | + //重新进行绘制,因为这样就可以让上下两部分完全重新调运onLayout布局铺满屏幕 |
| 138 | + requestLayout(); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments