【问题标题】:recyclerView won't scroll smoothly for first timerecyclerView 第一次滚动不流畅
【发布时间】:2020-07-30 07:23:32
【问题描述】:

我在一个fragment中有一个recyclerView,这个fragment位于viewpager2中。

我从服务器读取数据并将它们转换为我的 ModelClass,它们创建一个 ArrayList 并将这个数组列表传递给 recyclerView。 这是我的 recyclerView 项目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@drawable/main_icon_background"
    android:elevation="5dp"
    android:layoutDirection="rtl"
    android:orientation="horizontal"
    android:padding="10dp">


    <ImageView
        android:id="@+id/image"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@color/black" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:textAppearance="@style/vazir_font"
            android:textColor="@color/black" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:ellipsize="end"
            android:maxLines="3"
            android:textAppearance="@style/vazir_font"
            android:textColor="@color/gray"
            android:textSize="13sp" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1.5dp"
            android:layout_marginStart="20dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="8dp"
            android:alpha="0.5"
            android:background="@color/gray" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/source"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:layout_weight="0.8"
                android:ellipsize="end"
                android:maxLines="1"
                android:textAppearance="@style/vazir_font"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="13sp" />

            <TextView
                android:id="@+id/date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:gravity="end"
                android:textAppearance="@style/vazir_font"
                android:textColor="@color/colorAccent"
                android:textSize="13sp" />

        </LinearLayout>


    </LinearLayout>


</LinearLayout>

这是我的 recyclerView 适配器:

class NewsRecyclerViewAdapter : RecyclerView.Adapter<NewsRecyclerViewAdapter.ViewHolder>() {

    private val items = ArrayList<NewsAdapterModel>()
    lateinit var onClick: NewsTutorialClickListener

    fun add(list: ArrayList<NewsAdapterModel>) {
        items.clear()
        items.addAll(list)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.news_recycler_model, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int = items.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding(items[position])
    }

    override fun getItemId(position: Int): Long {
        return items[position].id.hashCode().toLong()
    }


    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun binding(model: NewsAdapterModel) {


            itemView.setOnClickListener {
                onClick.onClick(model.id)
            }


            itemView.title.text = model.title
            itemView.description.text = model.description
            itemView.date.text = Arrange().persianConverter(model.date)
            itemView.source.text = model.source

            Glide.with(itemView.context)
                .load(model.image)
                .placeholder(R.mipmap.logo)
                .into(itemView.image)

        }
    }

}

我在我的片段中使用了这个回收器视图,就像这样:

 override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        adapter.onClick = this
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        binding.recyclerView.adapter = adapter
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.setItemViewCacheSize(20)


        scope.launch {
            withContext(Dispatchers.Default) {
                fetchNews()
            }
        }
    }

我的问题是:当我第一次滚动回收器视图时,它不流畅,似乎有些帧被跳转,但是当我到达 recyclerView 的末尾并向上滚动然后向下滚动时,它非常平滑并找到。我该如何解决这个问题? 还要提一下,recyclerView 的 Item 并不多,也就 10 个左右。

【问题讨论】:

  • 尝试同时删除 binding.recyclerView.setHasFixedSize(true) binding.recyclerView.setItemViewCacheSize(20)
  • @Rinat 事实上,它变得更好了!谢谢!我搜索了很多很多人都说使用这两种方法来提高 recyclerView 的性能。很有趣。
  • 它算作答案吗?它完成了你想要的吗?如果,是的,我可以添加答案来解释为什么会这样。
  • 是的!我会很感激知道为什么会发生这种情况@Rinat

标签: android kotlin android-recyclerview smooth-scrolling


【解决方案1】:

删除

 recyclerView.setHasFixedSize(true)

实际上它不会造成任何伤害并且确实提高了性能,因为 recyclerView 不会在 onBindViewHolder 内计算 viewHolders evrytime 的大小。但是你有你所有的TextViewswrap_content 高度,这肯定会在未来导致一些损坏的用户界面。


删除

recyclerView.setItemViewCacheSize(20)

此函数强制RecyclerView 过早地膨胀并绑定额外的 20 个ViewHolders,以便它可以立即使用它们而无需调用OncreateViewHolderOnBindViewHolder。你不应该玩弄这个而只是使用默认值。虽然 20 额外的查看器应该没什么大不了的,但我想 RecyclerView 可能会感到困惑,因为你强迫它在现有 10 之上创建额外的 20 ,而您列出的仅包含 10 项。创建那些阻塞 ui 线程的额外 viewHolders 需要时间,但创建后看起来很顺利。

【讨论】:

  • removing: recyclerView.setItemViewCacheSize() 为我完成了工作
【解决方案2】:

你不需要添加这些行

        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.setItemViewCacheSize(20)

请删除这些行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    相关资源
    最近更新 更多