我正在使用适配器将列表+静态按钮显示为footerView。
按钮以及列表都是通过以下方式加载的版式资源文件:
LayoutInflater.from(parent.context).inflate(R.layout.recycler_button, parent, false)
在某些情况下,我现在需要以编程方式更改按钮的背景颜色以及文本。我该如何处理?
在这里您可以找到我的适配器代码:
package com.etask.etaskemployeeapp.Controller.Model
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.etask.etaskemployeeapp.R
class VacationTimeAdapter(val rowJSONList: ArrayList<JSONListRow>) : RecyclerView.Adapter<VacationTimeAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
if (viewType == R.layout.list_layout) {
val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
return ViewHolder(v)
} else {
val v = LayoutInflater.from(parent.context).inflate(R.layout.recycler_button, parent, false)
return ViewHolder(v)
}
}
override fun getItemCount(): Int {
return rowJSONList.size + 1
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
if (position == rowJSONList.size){
}else {
val row = rowJSONList.get(position)
holder.identifierName.text = row.identifier
// Check which datatype the value for a row is
if (row.value is String) {
holder.valueName.text = row.value as String
} else if (row.value is Double) {
val value = row.value as Double
holder.valueName.text = "$value"
} else if (row.value is Int) {
val value = row.value as Int
holder.valueName.text = "$value"
}
}
}
override fun getItemViewType(position: Int): Int {
return if (position === rowJSONList.size) R.layout.recycler_button else R.layout.list_layout
}
// Clean all elements of the recycler
fun clear() {
rowJSONList.clear()
notifyDataSetChanged()
}
// Add a list of items -- change to type used
fun addAll(newList: ArrayList<JSONListRow>) {
rowJSONList.addAll(newList)
notifyDataSetChanged()
}
class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
var identifierName = itemView.findViewById<TextView>(R.id.identifierLabel)
var valueName = itemView.findViewById<TextView>(R.id.valueLabel)
}
}
答案 0 :(得分:0)
是什么情况?您是否希望通过操作或加载它们时更改它们?我在Android的机器人中有这样的实现方式