How to determine if a NestedScrollView is scrolled to the end and is idle?

后端 未结 2 943
你的背包
你的背包 2021-01-24 15:33

Tried that :

NestedScrollView ns =(NestedScrollView) findViewById(R.id.nested_scroll);
        ns.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeLi         


        
2条回答
  •  感情败类
    2021-01-24 16:26

    Unfortunately, NestedScrollView does not support implementation that dispatches scroll state, because it is totally different kind of Scroll view. Simply it is FrameLayout with Scroller.

    Usually, end of a scroll view is reached when ViewCompat.canScrollVertically (scrollView, -1) returns false. For the scroll state you need to subclass NestedScrollView and add your own interface similar to the one of RecyclerView. Your interface method should be called in the following overriden methods:

    stopNestedScroll() -> SCROLL_STATE_IDLE

    startNestedScroll() -> SCROLL_STATE_DRAGGING

    dispatchNestedPreFling() -> SCROLL_STATE_FLINGING

    Please don't forget to make the super calls of these methods. If you don't you will break NestedScrollView behavior

    Edit:

    public class NestedScrollingView extends NestedScrollView {
        private int mState = RecyclerView.SCROLL_STATE_IDLE;
    
        public interface NestedScrollViewScrollStateListener {
            void onNestedScrollViewStateChanged(int state);
        }
    
    
        public void setScrollListener(NestedScrollViewScrollStateListener scrollListener) {
            this.mScrollListener = scrollListener;
        }
    
        private NestedScrollViewScrollStateListener mScrollListener;
    
        public NestedScrollingView(Context context) {
            super(context);
        }
    
        public NestedScrollingView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public NestedScrollingView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void stopNestedScroll() {
            super.stopNestedScroll();
            dispatchScrollState(RecyclerView.SCROLL_STATE_IDLE);
        }
    
        @Override
        public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
            dispatchScrollState(RecyclerView.SCROLL_STATE_DRAGGING);
            return super.onStartNestedScroll(child, target, nestedScrollAxes);
        }
    
    
        @Override
        public boolean startNestedScroll(int axes) {
            boolean superScroll = super.startNestedScroll(axes);
            dispatchScrollState(RecyclerView.SCROLL_STATE_DRAGGING);
            return superScroll;
        }
    
    
        private void dispatchScrollState(int state) {
            if (mScrollListener != null && mState != state) {
                mScrollListener.onNestedScrollViewStateChanged(state);
                mState = state;
            }
        }
    
    }
    

提交回复
热议问题