如何确定NestedScrollView是否滚动到最后并且是空闲的?

时间:2016-06-06 21:28:25

标签: android scrollview android-scrollview nestedscrollview android-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) {

            }
        });

但是卡住了,有没有人有想法?

只是为了清楚我想要的东西 - 我希望能够观察滚动状态(如在RecyclerView的addOnScrollListener中)并且只检查一次,当滚动结束(空闲)时,如果用户滚动到结束NestedScrollView。

2 个答案:

答案 0 :(得分:13)

不幸的是,NestedScrollView不支持调度滚动状态的实现,因为它是完全不同类型的Scroll视图。只需FrameLayout Scroller

通常,当ViewCompat.canScrollVertically (scrollView, -1)返回false时,会到达滚动视图的结尾。对于滚动状态,您需要子类NestedScrollView并添加类似于RecyclerView的接口。应在以下覆盖方法中调用您的接口方法:

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

}

答案 1 :(得分:7)

我知道最简单的方法是使用NestedScrollView的setOnScrollChangeListener:

 NestedScrollView nestedSV = (NestedScrollView) findViewById(R.id.nested_sync_scrollview);

    if (nestedSV != null) {

        nestedSV.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                String TAG = "nested_sync";
                if (scrollY > oldScrollY) {
                    Log.i(TAG, "Scroll DOWN");
                }
                if (scrollY < oldScrollY) {
                    Log.i(TAG, "Scroll UP");
                }

                if (scrollY == 0) {
                    Log.i(TAG, "TOP SCROLL");
                }

                if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                    Log.i(TAG, "BOTTOM SCROLL");
                    if (!isRecyclerViewWaitingtoLaadData) //check for scroll down
                    {

                        if (!loadedAllItems) {
                            showUnSentData();
                        }
                    }
                }
            }
        });
    }