【问题标题】:Scroll not working properly for RecyclerView inside CoordinatorLayoutCoordinatorLayout 内的 RecyclerView 滚动无法正常工作
【发布时间】:2015-09-14 13:44:50
【问题描述】:

其实我有两个问题

第一。滚动无法正常工作,有时当我们在特定方向滚动很短的距离并离开触摸时,它会非常快速地滚动到该特定方向的末尾,即向上或向下

第二。我希望自定义工具栏的标题仅在其折叠时显示,而当其展开时,标题应隐藏

这是 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" />

【问题讨论】:

  • 活动(片段)代码在哪里?
  • 您是否设法找到了解决方案。我面临着类似的问题。
  • @Gaurav 不。当我遇到这个问题时,我已经停止使用带有 recyclerview 的协调器布局。今天我再次尝试使用更新的 SDK,仍然是同样的问题。相反,这些天我正在使用ObservableScrollView
  • 谢谢。我会尝试实现这一点。

标签: android android-recyclerview android-coordinatorlayout android-collapsingtoolbarlayout


【解决方案1】:

除了Kuldeep给出的答案之外,第一个问题的解决方案如下:

要消除 RecyclerView 的不良滚动行为,只需在 RecyclerView 上设置此属性:

android:nestedScrollingEnabled="false"

或者要动态地执行此操作,您可以使用:

recyclerView.setNestedScrollingEnabled(false);

注意:如果你在 CoordinatorLayout 中使用 FloatActionButton,当你的 recyclerview 滚动时它不会执行隐藏动画

【讨论】:

    【解决方案2】:

    // 工具栏展开和折叠时隐藏和显示标题

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      相关资源
      最近更新 更多