[英]Scroll not working properly for RecyclerView inside CoordinatorLayout
其實我有2個問題
1號 滾動無法正常工作,有時當我們在特定方向上滾動很小的距離並保持觸摸時,滾動速度會非常快,直到該特定方向的末端(即上下)
2號 我希望僅在折疊和展開時顯示自定義工具欄的標題,標題應該隱藏
這是XML代碼
<android.support.design.widget.CoordinatorLayout
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:id="@+id/appBarLayout"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/imageone"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7"
android:scaleType="fitCenter"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar22"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_collapseMode="pin"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerview1"
android:layout_centerHorizontal="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_marginTop="-30dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/appBarLayout"
app:layout_anchorGravity="bottom|right|end"
android:src="@drawable/ic_favourite"
android:layout_marginBottom="@dimen/fab_margin_bottom"
android:layout_marginRight="@dimen/fab_margin_right"
app:fabSize="normal" />
除了庫爾迪普(Kuldeep)給出的答案外,以下是第一個問題的解決方案:
要消除RecyclerView的不良滾動行為,只需在RecyclerView上設置以下屬性:
android:nestedScrollingEnabled="false"
或動態地執行此操作,您可以使用:
recyclerView.setNestedScrollingEnabled(false);
注意:如果在CoordinatorLayout中使用FloatActionButton,則當您的recyclerview滾動時,它不會執行隱藏動畫
//工具欄展開和折疊時隱藏並顯示標題
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
//set your title when you scroll up
collapsingToolbar.setTitle(title);
isShow = true;
} else if (isShow) {
//title will disappear when you scroll down
collapsingToolbar.setTitle(" ");
isShow = false;
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.