ListView in BottomSheet

前端 未结 8 2331
梦毁少年i
梦毁少年i 2020-12-13 07:39

I ran into a problem where I had a simple ListView in a BottomSheet and ListView had enough items to fill the screen and scroll even m

8条回答
  •  离开以前
    2020-12-13 08:01

    I have tried Nested ListViews that was described here, but they do not allow expand /collapse bottom sheet when touching on ListView. Below is my solution, when you scroll up and ListView cannot scroll up, bottom sheet will be expanded, when you scroll down and ListView cannot do it, BottomSheet will be collapsed.

    public class NestedListView extends ListView {
    
        private boolean mIsBeingDragged = false;
        private float mLastMotionY;
    
        public NestedListView(final Context context, final AttributeSet attrs) {
            super(context, attrs);
        }
    
        public NestedListView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent event) {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN: {
                    if (canScrollDown() || canScrollUp()) {
                        final ViewParent parent = getParent();
                        if (parent != null) {
                            parent.requestDisallowInterceptTouchEvent(true);
                        }
                    }
                    break;
                }
            }
            return super.onInterceptTouchEvent(event);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            final float y = event.getRawY();
    
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_MOVE: {
                    if (!mIsBeingDragged) {
                        final float deltaY = mLastMotionY - y;
                        mIsBeingDragged = (deltaY > 0 && canScrollDown())
                            || (deltaY < 0 && canScrollUp());
    
                        if (mIsBeingDragged) {
                            final ViewParent parent = getParent();
                            if (parent != null) {
                                parent.requestDisallowInterceptTouchEvent(true);
                            }
                        } else {
                            final ViewParent parent = getParent();
                            if (parent != null) {
                                parent.requestDisallowInterceptTouchEvent(false);
                            }
                            return false;
                        }
                    }
                }
    
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    mIsBeingDragged = false;
                    break;
            }
    
            mLastMotionY = y;
    
            return super.onTouchEvent(event);
        }
    
        public boolean canScrollUp() {
            final int childCount = getChildCount();
            if (childCount == 0) {
                return false;
            }
    
            final int firstPosition = getFirstVisiblePosition();
            final int firstTop = getChildAt(0).getTop();
            return firstPosition > 0 || firstTop < getListPaddingTop();
        }
    
        public boolean canScrollDown() {
            final int childCount = getChildCount();
            if (childCount == 0) {
                return false;
            }
    
            final int firstPosition = getFirstVisiblePosition();
            final int lastBottom = getChildAt(childCount - 1).getBottom();
            final int lastPosition = firstPosition + childCount;
            return lastPosition < getCount() || lastBottom > getHeight() - getListPaddingBottom();
        }
    }
    

提交回复
热议问题