简体   繁体   English

Kotlin / Android自定义RecyclerView问题

[英]Kotlin / Android Custom RecyclerView issue

I was trying to make a custom RecyclerView with Adapter which takes a HashMap as parameter, I succeeded but now only 1 item is showing in recyclerView list and I have no idea why. 我试图使用带HashMap作为参数的适配器制作自定义RecyclerView ,我成功了,但现在recyclerView列表中仅显示1个项目,我不知道为什么。

Here my code snippet: 这是我的代码段:

Custom RecyclerAdapter: 自定义RecyclerAdapter:

class ProductsRecyclerAdapter(private val dataSet: HashMap<Int, ProductEntry>) : RecyclerView.Adapter<ProductsRecyclerAdapter.ViewHolder>() {

class ViewHolder(val linearLay: LinearLayout) : RecyclerView.ViewHolder(linearLay)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : ProductsRecyclerAdapter.ViewHolder {

    val linearLay = LayoutInflater.from(parent.context).inflate(R.layout.test_text_view, parent, false) as LinearLayout

    return ViewHolder(linearLay)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    holder.linearLay.textView.text = dataSet[position]?.pName

    holder.linearLay.textView2.text = dataSet[position]?.pExpiry

}

override fun getItemCount(): Int {

    return dataSet.size
}

}

Initialization: 初始化:

// Setup RecyclerView
    val prod1 = ProductEntry("Milk", "04/06/1996")
    val prod2 = ProductEntry("Bread", "04/01/2012")

    val testArray : HashMap<Int, ProductEntry> = hashMapOf(1 to prod1, 2 to prod2)

    viewManager = LinearLayoutManager(this)

    viewAdapter = ProductsRecyclerAdapter(testArray)

    recyclerView = findViewById<RecyclerView>(R.id.productsRecyclerView).apply {

        setHasFixedSize(true)
        layoutManager = viewManager
        adapter = viewAdapter
    }

And finally, custom XML for row: 最后,为行定制XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLay"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="40dp"
    android:layout_weight="1"
    android:text="TextView" />

Despite everything seems to be OK, I get only 1 row showing " milk ". 尽管一切似乎都很好,但我只得到1行显示“ milk ”。

Thanks for all help! 感谢所有帮助!

position in override fun onBindViewHolder(holder: ViewHolder, position: Int) starts at index 0. So in your case, it will be [0, 1] and your map keys are [1, 2]. override fun onBindViewHolder(holder: ViewHolder, position: Int) position override fun onBindViewHolder(holder: ViewHolder, position: Int)从索引0开始。因此,在您的情况下,它将为[0,1],并且地图关键字为[1、2]。 Only key "1" will be displayed correctly. 只有键“ 1”将正确显示。

try: 尝试:

val testArray : HashMap<Int, ProductEntry> = hashMapOf(0 to prod1, 1 to prod2)

But no need to use a map here! 但无需在这里使用地图! Just use a simple list of ProductEntry . 只需使用ProductEntry的简单列表即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM