首页 > 解决方案 > RecyclerView 与仅按钮适配器

问题描述

所以我的杂货项目需要我(拥有 3 周的 android 知识)为包含类别按钮的页面制作一个 RecyclerView。这些按钮应该将用户带到带有相应产品列表的片段。

我学到的方法是,我需要为item创建一个xml,将recyclerView放入main.xml中,并编写一个适配器。

我使用基本的 TextView 示例做到了这一点,在该示例中,我可以轻松地将 .text 放入适配器中。但在这种情况下,我的项目完全由一个按钮组成。而且我不知道如何在适配器中调用它,因为对于TextView使用setText()更容易。

我不知道会有多少按钮,因为他们说他们会为项目提供API,其中也应该有类别按钮名称。

这就是我所拥有的:

cat_unit.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/cat_btn"
        android:background="#000000"
        android:textColor="#ffffff"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

这是我的适配器:

猫适配器.kt

package org.mp.chiproject01.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.recyclerview.widget.RecyclerView
import org.mp.chiproject01.R

class CatAdapter(var catList: ArrayList<String>, var context: Context): RecyclerView.Adapter<ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var v: View = LayoutInflater.from(context).inflate(R.layout.cat_unit, parent, false)
        return ViewHolder(v)
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // I don't know what to put in the bind
        // Button will have different names, and I don't know how to 
        // Set the text for buttons
    }


}

class ViewHolder(view: View): RecyclerView.ViewHolder(view){
    var catBtn = view.findViewById<Button>(R.id.cat_btn)
}

这是带有 RecyclerView 的片段:

class FragmentCategory : Fragment() {


    var catList: ArrayList<String>()
    //Should I use 'String' or 'Button' here?

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        //View inflater for Fragment
        var view = inflater.inflate(R.layout.cat_unit, container, false)

        // Layout Manager and Adapter for RecyclerView
        view.recyclerViewmeat.layoutManager = LinearLayoutManager(context)
        view.recyclerViewmeat.adapter = CatAdapter(catList, this.context!!)

        //I don't know how if I can add them as 'String' or 'Button'
        //Can I do the setOnClickListener on the button the Button way
        //or do I have to write my own onClick to handle this? 

        return view

    }
}

谢谢!如果有任何提示/良好的编码实践,请告诉我!

标签: androidandroid-recyclerviewandroid-adapterandroid-button

解决方案


看来您在理解如何RecyclerView工作方面遇到了困难。

我应该在这里使用“字符串”还是“按钮”?

我们通常向适配器类传递一个数据列表,这个列表将用于在 RecyclerView 视图中填充数据(所以我们永远不会传递按钮或类似的东西),RecyclerView 负责根据数据列表为我们创建视图我们在CatAdapter类中传递给 RecyclerView 的大小:

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

现在对于每个位置,RecyclerView 将创建一个 ViewHolder,它将我们的 Layout 作为 View 保存,这就是发生onCreateViewHolderCatAdapter

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
//Inflating our layout (inflaing simple words, it's just a process to convert xml layout to View.
var v: View = LayoutInflater.from(context).inflate(R.layout.cat_unit, parent, false)
//Creating a ViewHolder that holds the view inside it.
return ViewHolder(v)
}

最后一件事,onBindViewHolder每当 ViewHolder 附加到 ReyclerView 时调用,这是绑定数据(用数据填充我们的视图)并在视图上设置单击侦听器(在您的情况下为按钮)的合适位置。那么如何在那个Button上绑定数据和setOnClickListener呢?首先,看看onBindViewHolder()

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

}

在参数中:

  • holder: ViewHolder保存我们的视图,我们可以使用它来绑定数据和点击监听器。
  • position: Int在当前视图中,我们将使用此位置从列表中获取数据(在您的情况下catList)。

现在你知道了,如何使用onBindViewHolder让我们编写代码(CatAdapter类):

package org.mp.chiproject01.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.recyclerview.widget.RecyclerView
import org.mp.chiproject01.R

class CatAdapter(var catList: ArrayList<String>, var context: Context): RecyclerView.Adapter<ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var v: View = LayoutInflater.from(context).inflate(R.layout.cat_unit, parent, false)
        return ViewHolder(v)
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
      //First, get data from catList by position.
      val data = catList[position]
      //Then, bind this data to the views (the button).
      holder.catBtn.text = data
      holder.catBtn.setOnClickListener {
      //Do something when button clicked
      }
    }


}

class ViewHolder(view: View): RecyclerView.ViewHolder(view){
    //catBtn should be val since it'll never change.
    val catBtn = view.findViewById<Button>(R.id.cat_btn)
}

注意:确保您将您的活动附加FragmentCategory到您的活动中,并且您将数据传递到catList您的FragmentCategory.


推荐阅读