recyclerView不会第一次平滑滚动

时间:2020-07-30 07:23:32

标签: android kotlin android-recyclerview smooth-scrolling

我在片段中有一个recyclerView,该片段位于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的项目并不多,大约10个项目。

2 个答案:

答案 0 :(得分:2)

删除

 recyclerView.setHasFixedSize(true)

实际上,它不会造成任何伤害并确实提高了性能,因为recyclerView不会在onBindViewHolder内计算viewHolders evrytime的大小。但是您所有TextViews的高度都为wrap_content,这肯定会在将来导致UI损坏。


删除

recyclerView.setItemViewCacheSize(20)

此函数强制RecyclerView过早充气并绑定20个额外的ViewHolders,以便它可以立即使用它们而无需调用OncreateViewHolderOnBindViewHolder。您不应该玩这个游戏,而应该使用默认设置。尽管 20 额外的观看者应该没什么大不了的,但我想RecyclerView可能会感到困惑,因为您强迫它在现有的顶部创建了额外的 20 > 10 ,而您的列表仅包含 10 个项目。创建那些多余的viewHolders阻止ui线程需要花费一些时间,但是在创建之后,它们似乎很平滑。

答案 1 :(得分:0)

您不需要添加这些行

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

请删除这些行