很遗憾,NestedScrollView
不支持调度滚动状态的实现,因为它是完全不同类型的滚动视图。简单来说就是FrameLayout
with Scroller
.
通常,当达到滚动视图的末尾时ViewCompat.canScrollVertically (scrollView, -1)
返回假。对于滚动状态,您需要子类化NestedScrollView
并添加您自己的界面,类似于RecyclerView
。您的接口方法应该在以下重写方法中调用:
stopNestedScroll()
-> SCROLL_STATE_IDLE
startNestedScroll()
-> SCROLL_STATE_DRAGGING
dispatchNestedPreFling()
-> SCROLL_STATE_FLINGING
请不要忘记对这些方法进行超级调用。如果你不这样做,你将会破坏 NestedScrollView 的行为
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;
}
}
}