如何确定NestedScrollView是否滚动到末尾并处于空闲状态? [英] How to determine if a NestedScrollView is scrolled to the end and is idle?

查看:1258
本文介绍了如何确定NestedScrollView是否滚动到末尾并处于空闲状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试过:

NestedScrollView ns =(NestedScrollView) findViewById(R.id.nested_scroll);
        ns.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            }
        });

但是被卡住了,有人有主意吗?

But got stuck, does anyone have an idea?

只是要弄清楚我想要的内容-我希望能够观察滚动状态(如RecyclerView的addOnScrollListener中),并且仅在滚动结束(空闲)时检查一次(如果用户滚动到滚动的末尾) NestedScrollView.

Just to clearify what i want - i want to be able to observe the scroll state (as in addOnScrollListener of RecyclerView) and check only once, when the scrolling has ended (idle), if the user scrolled to the end of the NestedScrollView.

推荐答案

不幸的是,NestedScrollView不支持调度滚动状态的实现,因为它是完全不同的Scroll视图.只是FrameLayoutScroller.

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.

通常,当ViewCompat.canScrollVertically (scrollView, -1)返回false时,将到达滚动视图的末尾.对于滚动状态,您需要继承NestedScrollView的子类,并添加与RecyclerView相似的自己的界面.您的接口方法应在以下替代方法中调用:

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

请不要忘记对这些方法进行超级调用.如果不这样做,您将破坏NestedScrollView的行为

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;
        }
    }

}

这篇关于如何确定NestedScrollView是否滚动到末尾并处于空闲状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆