【问题标题】:Recycler view is not scrolling smoothly - Cardview回收站视图滚动不顺畅 - Cardview
【发布时间】:2021-04-03 14:50:53
【问题描述】:

您好,我正在使用带有卡片视图的回收站视图,我不知道为什么它滚动不顺畅。它停留了几秒钟并显示下一个内容。

这是我的列表项的 xml

<?xml version="1.0" encoding="utf-8"?>

    <androidx.cardview.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:clickable="true"
        android:focusable="true"
        app:cardBackgroundColor="@color/colorAccent"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp" >

        <LinearLayout
            android:id="@+id/product_list_item_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/product_name_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="name"
                android:textColor="#FFFFFF"
                android:textSize="30sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/product_catagory_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="catagory"
                android:textColor="#FFFFFF"
                android:textSize="24sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/product_end_date_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="End Date"
                android:textColor="#FFFFFF"
                android:textSize="24sp"
                android:textStyle="bold" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

这是我的 recyclerViewAdapter

    class ProductsRecyclerViewAdapter(private val clickListener: (Product) -> Unit): RecyclerView.Adapter<ProductsMyViewHolder>() {

    private val productsList = ArrayList<Product>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductsMyViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding: ProductsListItemBinding =
            DataBindingUtil.inflate(layoutInflater, R.layout.products_list_item, parent, false)

        return ProductsMyViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return productsList.size
    }

    override fun onBindViewHolder(holder: ProductsMyViewHolder, position: Int) {
        holder.bind(productsList[position], clickListener)
    }

    fun setList(products: List<Product>) {
        productsList.clear()
        productsList.addAll(products)
    }
}

class ProductsMyViewHolder(val binding: ProductsListItemBinding): RecyclerView.ViewHolder(binding.root) {

    fun bind(product: Product, clickListener: (Product) -> Unit) {
        binding.productNameTextView.text = product.name
        binding.productCatagoryTextView.text = product.catagory
        binding.productEndDateTextView.text = convertLongToTime(product.end_date)
        binding.productListItemLayout.setOnClickListener {
            clickListener(product)
        }
    }

    fun convertLongToTime(time: Long): String {
        val date = Date(time)
        val format = SimpleDateFormat("dd MMM yyyy")
        return format.format(date)
    }
}

这是我初始化 ProductsRecyclerViewAdapter 的片段

 private fun initRecyclerView() {
        binding.productsRecyclerView.layoutManager = LinearLayoutManager(context)
        adapter = ProductsRecyclerViewAdapter ({ selectedItem: Product -> listItemClicked(selectedItem)})
        binding.productsRecyclerView.adapter = adapter
        displayProductssList()
    }

  private fun displayProductssList() {
        productsViewModel.products.observe(viewLifecycleOwner, Observer {
            Log.i("MYTAG", it.toString())
            adapter.setList(it)
            adapter.notifyDataSetChanged()
        })
    }

任何想法我可能在这里做错了

你的建议会很有帮助

提前致谢 回复

【问题讨论】:

  • 你在模拟器上测试过这个吗?如果是,重新启动模拟器会有所帮助,因为有时我的模拟器也会有点滞后。
  • 我已经关闭重新打开它仍然发生
  • @MehranB 我刚刚在我的手机上尝试过,它运行时没有任何滚动问题
  • 为什么模拟器会出现这种情况?
  • 伙计,我正在查看您的代码并在想……!一切看起来都很好! :)) 我猜有时模拟器会耗尽内存或其他什么。我其实不知道。很高兴问题解决了。

标签: android kotlin android-recyclerview android-cardview


【解决方案1】:

我在您的代码中看到的唯一内容是在每次绑定时创建新的 SimpleDateFormat,它绝对不是免费的。在适配器中创建单个实例。 另外,您的产品观察员可能执行得太频繁了?

【讨论】:

  • 是的,我在onBindViewHolder 中看不到任何特别严重的事情发生,甚至SimpleDateFormat 也不应该是什么大问题。如果它真的冻结了几秒钟,这表明你的应用程序整体做的工作太多,也许滚动就是你注意到它的方式,因为它是一个持续的动画和交互。可能只是模拟器运行不佳,但您始终可以分析您的应用程序以查看占用了所有时间的内容:developer.android.com/studio/profile/android-profiler