ListView in BottomSheet

前端 未结 8 2360
梦毁少年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 07:47

    This is the correct list view custom class with touch event hadnling

    public class BottomSheetListView extends ListView
    {
        public BottomSheetListView(Context context, AttributeSet p_attrs)
        {
            super(context, p_attrs);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev)
        {
            if (canScrollVertically(this))
            {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev)
        {
            if (canScrollVertically(this))
            {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onTouchEvent(ev);
        }
    
        public boolean canScrollVertically(AbsListView view)
        {
            boolean canScroll = false;
    
            if (view != null && view.getChildCount() > 0)
            {
                boolean isOnTop = view.getFirstVisiblePosition() != 0 || view.getChildAt(0).getTop() != 0;
                boolean isAllItemsVisible = isOnTop && view.getLastVisiblePosition() == view.getChildCount();
    
                if (isOnTop || isAllItemsVisible)
                {
                    canScroll = true;
                }
            }
    
            return canScroll;
        }
    }
    

提交回复
热议问题