Android RecyclerView 中除第一个项目外的所有项目都是空的

Android all items except the first in RecyclerView are empty

我在 Fragment 中将数据加载到简单的 RecyclerView 中,但除第一个项目外的所有项目都保持空白,默认文本为:Here is a screenshot

我的片段:

class CustomerTestFragment : Fragment() {

    private val customers: MutableList<Customer> by lazy { mutableListOf() }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_customer_test, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        customers.add(Customer(id = 0, name = "oooo", loyalty = 3, phone = "88888"))
        customers.add(Customer(id = 0, name = "tttt", loyalty = 3, phone = "88888"))
        customers.add(Customer(id = 0, name = "rrrr", loyalty = 3, phone = "88888"))
        customers.add(Customer(id = 0, name = "eeee", loyalty = 3, phone = "88888"))
        customers.add(Customer(id = 0, name = "wwww", loyalty = 3, phone = "88888"))

        val adapter = CustomerAdapterTest(customers)
        rv_list_customers.adapter = adapter
        rv_list_customers?.layoutManager = LinearLayoutManager(context)
        adapter.notifyDataSetChanged()
    }

    inner class CustomerAdapterTest(private var items: List<Customer>) : RecyclerView.Adapter<CustomerAdapterTest.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
            ViewHolder(parent.inflate(R.layout.item_customer_test))

        override fun onBindViewHolder(holder: ViewHolder, position: Int) =
            holder.bind(items[position])

        override fun getItemCount() = items.size

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            fun bind(item: Customer) = with(itemView) {
                name_customer?.text = item.name
            }
        }
    }

}

我的片段xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_list_customers"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:scrollbars="none"
        android:orientation="vertical"
        android:overScrollMode="never"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我的商品xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="20dp"
    android:layout_marginStart="24dp"
    android:layout_marginEnd="24dp" >

    <TextView
        android:id="@+id/name_customer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

我不知道为什么,但是当我这样重写我的 ViewHolder 时:

inner class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
            fun bind(item: Customer) {
                view.name_customer?.text = item.name
                view.phone_customer?.text = item.phone
                view.code_customer?.text = item.id.toString()
                view.loyalty_customer?.text = item.loyalty.toString()
            }
        }

开始工作

试试这个:

class TestFragment : Fragment() {
    private val customers: MutableList<Customer> by lazy { mutableListOf<Customer>() }

    companion object {
        fun newInstance() = TestFragment()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_test, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        customers.add(Customer(id = 0, name = "oooo", loyalty = 3, phone = "88888"))
        customers.add(Customer(id = 1, name = "tttt", loyalty = 4, phone = "88888"))
        customers.add(Customer(id = 2, name = "rrrr", loyalty = 5, phone = "88888"))
        customers.add(Customer(id = 3, name = "eeee", loyalty = 6, phone = "88888"))
        customers.add(Customer(id = 4, name = "wwww", loyalty = 7, phone = "88888"))

        val adapter = CustomerAdapterTest(customers)
        rv_list_customers.adapter = adapter
    }

    inner class CustomerAdapterTest(private var items: List<Customer>) :
        RecyclerView.Adapter<CustomerAdapterTest.ViewHolder>() {

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

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val item = items[position]
            holder.name.text  = item.name
        }

        override fun getItemCount() :Int{
            Log.d("TAG", "getItemCount: ${items.size}")
            return items.size
        }

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val name: TextView = itemView.findViewById(R.id.name_customer)
        }
    }
}

P.S: 在 <RecyclerView.../> 中声明布局管理器 i,e app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"