回收器视图滚动不顺畅 - Cardview

问题描述

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

这是我的列表项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()
        })
    }

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

你的建议会很有帮助

提前致谢 R

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)