自定義控制元件之仿汽車之家
阿新 • • 發佈:2019-01-02
public class VerticalDragListView extends FrameLayout { // 可以認為這是系統給我們寫好的一個工具類 private ViewDragHelper mDragHelper; private View mDragListView; // 後面選單的高度 private int mMenuHeight; // 選單是否開啟 private boolean mMenuIsOpen = false; public VerticalDragListView(Context context) { this(context, null); } public VerticalDragListView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public VerticalDragListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mDragHelper = ViewDragHelper.create(this, mDragHelperCallback); } /*@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); View menuView = getChildAt(0); mMenuHeight = menuView.getMeasuredHeight(); }*/ /*@Override public void addView(View child) { super.addView(child); }*/ /*@Override public void setVisibility(int visibility) { super.setVisibility(visibility); }*/ @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (changed) { View menuView = getChildAt(0); mMenuHeight = menuView.getMeasuredHeight(); } } @Override protected void onFinishInflate() { super.onFinishInflate(); int childCount = getChildCount(); if (childCount != 2) { throw new RuntimeException("VerticalDragListView 只能包含兩個子佈局"); } mDragListView = getChildAt(1); /*View menuView = getChildAt(0); mMenuHeight = menuView.getMeasuredHeight();*/ } @Override public boolean onTouchEvent(MotionEvent event) { mDragHelper.processTouchEvent(event); // Log.e("TAG", "onTouchEvent -> " + event.getAction()); return true; } // 1.拖動我們的子View private ViewDragHelper.Callback mDragHelperCallback = new ViewDragHelper.Callback() { @Override public boolean tryCaptureView(View child, int pointerId) { // 指定該子View是否可以拖動,就是 child // 只能是列表可以拖動 // 2.1 後面不能拖動 return mDragListView == child; } @Override public int clampViewPositionVertical(View child, int top, int dy) { // 垂直拖動移動的位置 // 2.3 垂直拖動的範圍只能是後面選單 View 的高度 if (top <= 0) { top = 0; } if (top >= mMenuHeight) { top = mMenuHeight; } return top; } // 2.2 列表只能垂直拖動 /*@Override public int clampViewPositionHorizontal(View child, int left, int dx) { // 水平拖動移動的位置 return left; }*/ // 2.4 手指鬆開的時候兩者選其一,要麼開啟要麼關閉 @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { // Log.e("TAG", "yvel -> " + yvel + " mMenuHeight -> " + mMenuHeight); // Log.e("TAG", "top -> " + mDragListView.getTop()); if (releasedChild == mDragListView) { if (mDragListView.getTop() > mMenuHeight / 2) { // 滾動到選單的高度(開啟) mDragHelper.settleCapturedViewAt(0, mMenuHeight); mMenuIsOpen = true; } else { // 滾動到0的位置(關閉) mDragHelper.settleCapturedViewAt(0, 0); mMenuIsOpen = false; } invalidate(); } } }; // 現象就是ListView可以滑動,但是選單滑動沒有效果了 private float mDownY; // ecause ACTION_DOWN was not received for this pointer before ACTION_MOVE // VDLV.onInterceptTouchEvent().DOWN -> LV.onTouch() -> // VDLV.onInterceptTouchEvent().MOVE -> VDLV.onTouchEvent().MOVE @Override public boolean onInterceptTouchEvent(MotionEvent ev) { // 選單開啟要攔截 if (mMenuIsOpen) { return true; } // 向下滑動攔截,不要給ListView做處理 // 誰攔截誰 父View攔截子View ,但是子 View 可以調這個方法 // requestDisallowInterceptTouchEvent 請求父View不要攔截,改變的其實就是 mGroupFlags 的值 switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mDownY = ev.getY(); // 讓 DragHelper 拿一個完整的事件 mDragHelper.processTouchEvent(ev); break; case MotionEvent.ACTION_MOVE: float moveY = ev.getY(); if ((moveY - mDownY) > 0 && !canChildScrollUp()) { // 向下滑動 && 滾動到了頂部,攔截不讓ListView做處理 return true; } break; } return super.onInterceptTouchEvent(ev); } /** * @return Whether it is possible for the child view of this layout to * scroll up. Override this if the child view is a custom view. * 判斷View是否滾動到了最頂部,還能不能向上滾 */ public boolean canChildScrollUp() { if (android.os.Build.VERSION.SDK_INT < 14) { if (mDragListView instanceof AbsListView) { final AbsListView absListView = (AbsListView) mDragListView; return absListView.getChildCount() > 0 && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0) .getTop() < absListView.getPaddingTop()); } else { return ViewCompat.canScrollVertically(mDragListView, -1) || mDragListView.getScrollY() > 0; } } else { return ViewCompat.canScrollVertically(mDragListView, -1); } } /** * 響應滾動 */ @Override public void computeScroll() { if (mDragHelper.continueSettling(true)) { invalidate(); } } }