0

試過:如何確定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。

+0

你有幫助在SO http://stackoverflow.com/questions/36143802/how-to-detect-the-position-of-the-scroll-nestedscrollview-android-at-the-bottom – Webserveis

回答

10

不幸的是,NestedScrollView不支持調度滾動狀態的實現,因爲它是完全不同的一種滾動視圖。簡單地說就是FrameLayoutScroller

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

} 
+0

謝謝,好主意;) – gianguyen

+0

我怎麼能使用這與'if'語句,我想檢查滾動是空閒然後做一些事情,當它拖動做別的事情..!? –

+0

stopNestedScroll也在MotionEvent.ACTION_UP事件上調用 –

2

,因爲我知道要做到這一點最簡單的方法是使用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(); 
         } 
        } 
       } 
      } 
     }); 
    } 
+0

此解決方案適用!謝謝 –

相關問題