【问题标题】:RecyclerView jumps directly to top when scrolling up when its item's height is wrap_contentRecyclerView在其item的高度为wrap_content时向上滚动时直接跳到顶部
【发布时间】:2017-07-12 17:23:15
【问题描述】:

这里是xml文件

FragmentRecyclerview

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/list_photos"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.photos.PhotosFragment"
        tools:listItem="@layout/card_photo">

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

上面Recyclerview中每一项的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_photo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/photo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        tools:src="@drawable/placeholder_photo_item" />

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

它看起来像这样:

如您所见,当我滚动时,它不会快速流畅地加载。它会在某些项目上停止滚动。
另外,当我向上滚动时,它会直接跳转到顶部。

下面是RecyclerView 的完美平滑和快速滚动效果,我可以通过将Recyclerview 的项目(此处为CardView)的高度设置为"match_parent" 来实现。

但这不是我希望列表的样子。该列表应该包含可变长度的图像,它们之间没有空格。

【问题讨论】:

    标签: android android-layout android-recyclerview


    【解决方案1】:

    我正在使用Glide 加载图像,并通过Glide 添加占位符修复它:

    GlideApp.with(mContext)
                .load(url)
                .placeholder(R.color.placeholder)
                .into(holder.photo);
    

    现在RecyclerView 滚动流畅,不会跳转到向上滚动的第一个项目。

    虽然已经修复,但我真的不知道为什么RecyclerView 会这样。

    【讨论】:

    • 谢谢!我遇到了一个切线相关的问题,RecyclerView 不允许您滚动到列表的最底部——一旦您靠近项目的底部,视图就会跳转。添加带有Coil(而不是Glide)的占位符图像最终为我解决了这个问题。我怀疑这与 RecyclerView 在其生命周期的不同点重新计算其滚动高度有关。
    猜你喜欢
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 2019-06-15
    相关资源
    最近更新 更多