不幸的是,NestedScrollView
不支持調度滾動狀態的實現,因爲它是完全不同的一種滾動視圖。簡單地說就是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;
}
}
}
你有幫助在SO http://stackoverflow.com/questions/36143802/how-to-detect-the-position-of-the-scroll-nestedscrollview-android-at-the-bottom – Webserveis