Android RecyclerView与CollapsingToolbarLayout的平滑滚动问题

4
我想使用CollapsingToolbarLayoutRecyclerView,就像Whatsapp一样。
这是一个录制的视频:https://sendvid.com/0oi2lxx5 所以当我向上滚动时,一切正常,但是当我向下滚动时,不太流畅。我希望能够使用RecyclerView平稳地滚动。
这是我的profile_activity.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:id="@+id/profile_recyclerview"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_below="@+id/profileactivity_appbar" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/profileactivity_appbar"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme3.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/profileactivity_collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">

            <ImageView
                android:id="@+id/profileactivity_fullimage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />

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

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




    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/profileactivity_appbar"
        app:layout_anchorGravity="bottom|right|end"
        app:backgroundTint="@color/ColorPrimary"
        android:src="@drawable/ic_create_white_48dp"/>

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

我正在使用:

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:design:23.4.0'

我该如何修复这个问题?

删除 ImageView 的 app:layout_scrollFlags,看看是否解决了问题。我认为这些标志与 app:layout_collapseMode 相矛盾。 - Sevle
如果你尝试通过图片滚动,它非常流畅,但是我无法通过RecyclerView这样滚动。我想你明白我的意思。 - Yasin Kaçmaz
3个回答

11

好的,我将解释如何解决这个问题,现在我的布局非常流畅;

实际上我不知道它是怎么解决的,但是;

  • 我删除了所有的android:fitsSystemWindows="true"属性。
  • 然后将app:layout_scrollFlags="scroll|enterAlways"属性添加到CollapsingToolbarLayoutImageViewToolbar中。
  • 并将所有元素移到AppBarLayout之前。

我的activity_profile.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profileCoordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ProgressBar android:indeterminate="true"
    android:layout_width="50dp" android:layout_height="50dp"
    android:id="@+id/profile_activity_progress" 
    style="?android:attr/progressBarStyle"
    android:layout_gravity="center_vertical|center_horizontal"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:visibility="visible" />

<ProgressBar android:indeterminate="true"
    android:layout_width="50dp" android:layout_height="50dp"
    android:id="@+id/profile_activity_progress_edit" 
    style="?android:attr/progressBarStyle"
    android:layout_gravity="center_vertical|center_horizontal"
    android:visibility="gone" />


<include layout="@layout/content_profile_edit"/>


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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@+id/profileactivity_appbar"
    app:layout_anchorGravity="bottom|right|end"
    app:backgroundTint="@color/ColorPrimary"
    android:src="@drawable/ic_create_white_48dp"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/profileactivity_appbar"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:visibility="gone"
    android:theme="@style/AppTheme3.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/profileactivity_collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|enterAlways"
        app:statusBarScrim="@color/ColorPrimaryDark"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">

        <ImageView
            android:id="@+id/profileactivity_fullimage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            app:layout_scrollFlags="scroll|enterAlways"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />

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

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

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

并且像content_profile_edit.xml这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/profile_edit_layout"
    android:layout_alignParentBottom="true"
    android:visibility="gone"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/profile_fragment_nested"
    android:layout_height="match_parent"
    android:padding="10dp">

   <!-- other views -->

</android.support.v4.widget.NestedScrollView>

</RelativeLayout>

如何固定工具栏和选项卡布局 - Bincy Baby
@BincyBaby 我不太明白你的意思,但是你可以查看cheesesquare releases - Yasin Kaçmaz
这个方法可以正常工作,但如果我想固定工具栏和选项卡布局,它就不起作用了。 - Bincy Baby
@Bincy Baby,我很愿意帮助你,但你必须提供一些代码,让我看看你现在有什么。 - Yasin Kaçmaz
1
@YasinKaçmaz 一些警告:如果将FAB放在AppBarLayout之上,layout_anchor id将无法找到。 - Denis Makovsky

1

我也遇到了RecyclerView和CollapsingToolbarLayout的问题(根本没有折叠)。如果你还没有使用,试着使用当前版本23.4.+的recyclerview、appcompat和design。


我的问题不是折叠问题。如果您观看视频,当我通过触摸折叠图像滚动时,它非常流畅。但是当使用RecyclerView滚动时,它不如折叠图像视图的流畅。 - Yasin Kaçmaz
你尝试过最新版本的API吗?可能是已修复的错误。我尝试了你的布局,一切都正常运行。 - b00n12
我已将所有内容更新至23.4.0版本,但问题仍然存在。 - Yasin Kaçmaz
是的,我更新了RecyclerView并更改了滚动标志。然后我的问题就解决了。谢谢。 - Yasin Kaçmaz

0
/**
google solve this issue in 26 api level .I spent my whole day finally I found this answer. For Demo You create  one android studio Scrolling Activity and do following changes upgrade all repository to 26 version and you will see this scrolling smoothly effect.
**/

apply plugin: 'com.android.application'

android {
    **compileSdkVersion 26
    buildToolsVersion "26.0.1"**
    defaultConfig {
        applicationId "com.example.sandeep.quantauser"
        minSdkVersion 19
        **targetSdkVersion 26**
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile files('libs/ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar')
    compile files('libs/apache-jakarta-commons-net-1.4.1.jar')
    compile 'com.theartofdev.edmodo:android-image-cropper:2.4.+'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    testCompile 'junit:junit:4.12'
    compile files('libs/itextpdf-5.4.0.jar')
    **compile "com.android.support:support-core-utils:26.0.2"
    compile 'com.android.support:design:26.0.2'
    compile 'com.android.support:appcompat-v7:26.0.2'
    compile 'com.android.support:cardview-v7:26.0.2'**
}

**allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}**

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接