android - 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
}
}
谢谢!如果有任何提示/良好的编码实践,请告诉我!
解决方案
看来您在理解如何RecyclerView
工作方面遇到了困难。
我应该在这里使用“字符串”还是“按钮”?
我们通常向适配器类传递一个数据列表,这个列表将用于在 RecyclerView 视图中填充数据(所以我们永远不会传递按钮或类似的东西),RecyclerView 负责根据数据列表为我们创建视图我们在CatAdapter
类中传递给 RecyclerView 的大小:
override fun getItemCount( ): Int {
return catList.size
}
现在对于每个位置,RecyclerView 将创建一个 ViewHolder,它将我们的 Layout 作为 View 保存,这就是发生onCreateViewHolder
在CatAdapter
:
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
.
推荐阅读
- android - 停止前台服务后通知棒(无法关闭)
- prestashop - 模块不会出现在 prestashop 的员工权限中
- redis - redis 使用缓存吗?
- php - 为什么 sort() 在 PHP 7 中以相反的顺序使用 SORT_NUMERIC?
- java - ItemizedOverlayWithFocus 聚焦 osmdroid 时显示另一个叠加层
- python - 将“Content-Encoding”和“Accept-Encoding”标头设置为 None
- php - 更改履行 php 响应中的日期语言
- php - 您好,我正在尝试打印自 mysql 时间戳发布以来经过的时间
- angular - 如何通过重置按钮清除表单的错误消息/取消表单后
- azure - 如何通过 Git VM 管理 azure 本地 Web 应用程序文件