2017-10-12 81 views
0

我有同樣的問題,在這個問題here。 問題在於,在發生反彈後,RecyclerView的孩子不會得到觸摸事件。只有當recyclerview達到頂部或底部時纔會顯示。更好的方法來解決「觸摸後一觸即發」

這裏的問題是,在達到頂部或底部位置後,回收站視圖滾動狀態仍然爲SCROLL_STATE_SETTLING(1或2秒)。這個狀態意味着recyclerview還沒有完成一個動畫。在這種狀態下,簡單的點擊事件只會停止「SETTLING」過程。點擊次數正常處理。

它似乎越野車...因爲「SETTLING」過程應該在到達頂部時立即結束。從RecycleView類

代碼:

if (mScrollState == SCROLL_STATE_SETTLING) { 
       getParent().requestDisallowInterceptTouchEvent(true); 
       setScrollState(SCROLL_STATE_DRAGGING); 
      } 

我已成功地使用此代碼

this.addOnScrollListener(object : RecyclerView.OnScrollListener() { 

     override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) { 
      val canScrollUp = recyclerView?.canScrollVertically(-1)!! 
      val canScrollDown = recyclerView.canScrollVertically(1) 
      if (!canScrollUp || !canScrollDown) { 
       recyclerView.stopScroll() 
      } 

     } 
    }) 

我的問題是 1)修復這是它支持庫問題? 2)什麼是解決這個問題的更正確的方法?自定義偵聽器對我來說似乎不太好。

PS:我的視圖層次結構不是RecyclerView裏面的NestedScrollView。它是AppBar和ViewPager下的RelativeLayout片段。

<RelativeLayout 
    android:id="@+id/fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 
</RelativeLayout> 


<FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#fafafa"> 
    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
      /> 
</FrameLayout> 

回答

0

這裏另一個解決方法@Revaz修復,因爲如果你的孩子意見不填滿所有可用的空間,或使用PagerSnapHelper與單頁就會失敗。在這種情況下,滾動將永遠不會被調用!

覆蓋您的LayoutManager的startSmoothScroll常規:

private final LinearLayoutManager mLayoutManager = new LinearLayoutManager(
     getContext(), 
     LinearLayoutManager.HORIZONTAL, false) { 

    @Override 
    public void startSmoothScroll(SmoothScroller smoothScroller) { 
     int[] out = mPagerSnapHelper.calculateDistanceToFinalSnap(
       mLayoutManager, mPagerSnapHelper.findSnapView(mLayoutManager)); 
     if (out[0] == 0 && out[1] == 0) { 
      // workarround "Touch Intercepted after fling」 
      // no scroll needed 
      MyRecyclerView.this.stopScroll(); 
      return; 
     } 
     super.startSmoothScroll(smoothScroller); 
    } 
}; 

setLayoutManager(mLayoutManager); 

需要在支持庫中修復我的意見!