android - Android:无法更新 RecyclerView
问题描述
我正在 Kotlin 中开发一个 Android 应用程序,并实现了一个 RecyclerView。
添加到的两个参数parametersList
都显示出来了,但是当我按下TEST
按钮时,RecyclerView没有更新!我不明白我的错误,你能帮我吗?
这是我的适配器:
class CustomAdapter(private val parameterList: List<Parameter>, private val onClick: ((selectedParameter: Parameter) -> Unit)? = null) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun showItem(parameter: Parameter, onClick: ((selectedParameter: Parameter) -> Unit)? = null) {
itemView.findViewById<TextView>(R.id.parameterName).text = parameter.parameterName
itemView.findViewById<TextView>(R.id.parameterValue).text = parameter.parameterValue as CharSequence?
if(onClick != null) {
itemView.setOnClickListener {
onClick(parameter)
}
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.parameter_text, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.showItem(parameterList[position], onClick)
}
override fun getItemCount(): Int {
return parameterList.size
}
}
我的活动:
class NFCActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nfc)
buttonTest.setOnClickListener() {
parametersList = parametersList + ParameterText("Temperature", "24°C")
rv_parameters2.adapter?.notifyDataSetChanged()
}
rv_parameters2.adapter = CustomAdapter(parametersList) { item ->
println("------------------OnClick------------------")
}
rv_parameters2.layoutManager = LinearLayoutManager(this)
}
private var parametersList : List<Parameter> = listOf<Parameter> (
ParameterText("Temperature", "24°C"),
ParameterText("Temperature", "24°C")
)
companion object {
fun getStartIntent(context: Context): Intent {
return Intent(context, NFCActivity::class.java)
}
}
}
参数文本.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="10dp">
<TextView
android:id="@+id/parameterName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/parameter_name"
android:textColor="@color/black"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/parameterValue"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/parameterValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/parameter_value"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="@id/parameterName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/parameterName"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
活动_nfc.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.ble.NFCActivity">
<Button
android:id="@+id/buttonTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_parameters2"
tools:listitem="@layout/parameter_text"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/buttonTest"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
编辑 1:(按照 SlothCoding 的回答)
我的新活动:
class NFCActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nfc)
buttonTest.setOnClickListener() {
parametersList = parametersList + ParameterText("Temperature", "24°C")
rv_parameters2.adapter?.notifyDataSetChanged()
}
rv_parameters2.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
rv_parameters2.adapter = CustomAdapter(parametersList) { item ->
println("------------------OnClick------------------")
}
}
private var parametersList : List<Parameter> = listOf<Parameter> (
ParameterText("Temperature", "24°C"),
ParameterText("Temperature", "24°C")
)
companion object {
fun getStartIntent(context: Context): Intent {
return Intent(context, NFCActivity::class.java)
}
}
}
编辑 2:(按照 Sekiro 的回答)
自定义适配器
class CustomAdapter(private val parameterList: List<Parameter>, private val onClick: ((selectedParameter: Parameter) -> Unit)? = null) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
private var parameterListLocal: List<Parameter> = parameterList
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun showItem(parameter: Parameter, onClick: ((selectedParameter: Parameter) -> Unit)? = null) {
itemView.findViewById<TextView>(R.id.parameterName).text = parameter.parameterName
itemView.findViewById<TextView>(R.id.parameterValue).text = parameter.parameterValue as CharSequence?
if(onClick != null) {
itemView.setOnClickListener {
onClick(parameter)
}
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.parameter_text, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.showItem(parameterListLocal[position], onClick)
}
override fun getItemCount(): Int {
return parameterListLocal.size
}
fun addNewList(newList: List<Parameter>){
parameterListLocal = newList;
notifyDataSetChanged();
}
}
NFC活动
class NFCActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nfc)
buttonTest.setOnClickListener() {
parametersList = parametersList + ParameterText("Temperature", "24°C")
(rv_parameters2.adapter as CustomAdapter).addNewList(parametersList)
}
rv_parameters2.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
rv_parameters2.adapter = CustomAdapter(parametersList) { item ->
println("------------------OnClick------------------")
}
}
private var parametersList : List<Parameter> = listOf<Parameter> (
ParameterText("Temperature", "24°C"),
ParameterText("Temperature", "24°C")
)
companion object {
fun getStartIntent(context: Context): Intent {
return Intent(context, NFCActivity::class.java)
}
}
}
解决方案
问题在于您buttonTest.setOnClickListener()
在哪里更新列表,但适配器对数组列表的更新一无所知,而是将新的数组列表传递给适配器并调用notifyDataSetChanged()
尝试以下
自定义适配器
// add this new function
fun addNewList(newList: List<Parameter>){
parameterList = newList;
notifyDataSetChanged();
}
addNewList
通过触发buttonTest.setOnClickListener
buttonTest.setOnClickListener() {
parametersList = parametersList + ParameterText("Temperature", "24°C")
(rv_parameters2.adapter as CustomAdapter).addNewList(parametersList)
}
编辑:
notifyDataSetChanged()
is indeed a costly operation and should be replaced with either notifyItemInserted
or a better choice would be to use ListAdapter
推荐阅读
- android - Android Studio:如何从搜索和其他功能中排除文档文件夹?
- javascript - 如何在文本字段中显示基本验证消息
- asp.net - 更改后如何放弃会话
- 在 ASP.Net 中?
- react-native - 从服务器获取所有数据后停止活动指示器
- electron - 以编程方式控制 Chromium 实例的最可靠方法是什么?
- android - 如何在android studio中读取pdf文件
- c# - 如何使用 onvif ver20 ptz.wsdl 创建和设置 ONVIF PresetTour
- r - 订购 PCA 导致主成分高于 5
- quickfix - Quickfix 浮动字段
- c# - async Func 的 3 次调用有什么区别
> 在 C# 中?