5

我正在創建一個RecyclerView頭部,其中當您向上滾動RecyclerView時,頭部會摺疊。我可以通過下面的佈局非常仔細地實現這一點,透明的AppBarLayoutMyCoolView這是標題。視差效果很好。在AppBarLayout摺疊之前防止RecyclerView在AppBarLayout下滾動

但是,如果標題仍然可見,並且我打開RecyclerView,RV會慢慢滾動到頂部,並且其中一些項目位於工具欄下方,直到RV到達視圖的頂部。我一直在玩scrollFlags,但沒有取得理想的結果。有關如何改善投擲經驗以便物品不被剪裁的任何建議?

查看視頻和flinged當觀看--- https://www.dropbox.com/s/jppd6m7zo41k23z/20160609_151309.mp4?dl=0

<android.support.design.widget.CoordinatorLayout> 

    <android.support.design.widget.AppBarLayout 
     android:background="#00000000"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <com.android.myapp.MyCoolView 
       app:layout_collapseMode="parallax"/> 

     </android.support.design.widget.CollapsingToolbarLayout> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v7.widget.RecyclerView/> 

</android.support.design.widget.CoordinatorLayout> 

回答

5

可能的解決方法(未經測試)。將OnOffsetChangedListener添加到您的AppBarLayout,並記錄偏移值。首先,聲明該字段:

private boolean shouldScroll = false; 

然後的onCreate:

AppBarLayout appbar = findViewById(...); 
appbar.addOnOffsetChangedListener(new OnOffsetChangedListener() { 
    @Override 
    void onOffsetChanged(AppBarLayout appbar, int offset) { 
     // Allow recycler scrolling only if we started collapsing. 
     this.shouldScroll = offset != 0; 
    } 
}); 

現在,添加滾動收聽到您的RecyclerView。每當它試圖滾動,恢復滾動如果AppBarLayout仍在擴大:

RecyclerView recycler = findViewById(...); 
recycler.addOnScrollListener(new OnScrollListener() { 
    @Override 
    void onScrolled(RecyclerView recycler, int dx, int dy) { 
     // If AppBar is fully expanded, revert the scroll. 
     if (!shouldScroll) { 
      recycler.scrollTo(0,0); 
     } 
    } 
}); 

這可能需要一些調整,但。我看到兩個問題:

  • 如果scrollTo()調用onScrolled()返回,可能的堆棧溢出。可以通過布爾值或刪除/添加滾動監聽器來解決
  • 可能您希望不僅在AppBarLayout完全展開時防止滾動,而且通常在AppBarLayout未摺疊時防止滾動。這意味着您不必檢查offset != 0,而是檢查offset == appBarLayout.getTotalScrollRange()。我認爲。
+0

這工作得很好。偏移總是小於零,除非完全展開。所以'offset> 0'將不起作用。我做了一些像'shouldScroll = appbar.getTotalScrollRange()== Math.abs(offset)'。沒有得到stackoverflow,所以它似乎工作。 – ono

+0

是的,我忘了它是否定的。你做得對。 – natario

+0

這對我來說根本不起作用。有沒有人找到解決方案?它使appbarlayout邊界線不可用。 –

0

也許你可以像這樣向你的RecylerView添加layout_behavior =「@ string/appbar_scrolling_view_behavior」。

<android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
+0

我有。我忘了將它包含在上面的xml中 – ono

0

結束語中的FrameLayout的RecyclerView解決了這個問題。

您還需要將appbar_scrolling_view_behavior從RecyclerView移動到FrameLayout,以便將其正確放置在AppBarLayout下方。

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    > 

    <!--This RecyclerView MUST be wrapped in a FrameLayout--> 
    <!--This prevents the RecyclerView from going behind the AppBarLayout--> 
    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 

</FrameLayout> 
相關問題