gpt4 book ai didi

android - Kotlin:如何使用 Spinner 将项目设置为 RecyclerViewAdapter?

转载 作者:行者123 更新时间:2023-12-02 13:32:35 25 4
gpt4 key购买 nike

我正在尝试使用两个微调器功能对 Recyclerview 进行编码。
主要目标是显示两个微调器过滤的结果。
例如,有 7 个列表,每个都有一定的学科,如“建筑”、“机械”、“电气”等,楼层数据如 1、2、3、4..

所以,我做了这样的代码

这是recyclerview的适配器。

class ProjectFilesModel(val fileName: String, val ctype: String, val floor: Int)

class FileDataViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer

class FileDataAdapter(val fileList: List<ProjectFilesModel>) :
RecyclerView.Adapter<FileDataViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FileDataViewHolder {

val view =
LayoutInflater.from(parent.context).inflate(R.layout.filtered_file, parent, false)

return FileDataViewHolder(view)
}

override fun getItemCount(): Int {
return fileList.count()
}

override fun onBindViewHolder(holder: FileDataViewHolder, position: Int) {
holder.containerView.fileNameLayout.text = fileList[position].fileName
holder.containerView.ctypeLayout.text = fileList[position].ctype
holder.containerView.floorLayout.text = "${fileList[position].floor}floor"
}
}

而且,这是主要 Activity ;
class MainActivity : AppCompatActivity() {

//ctype option array
var ctypeArray =
arrayOf("All", "Architectural", "Mechanical", "Electrical", "Structural", "Civil")
var ctypeOptionAdapter: ArrayAdapter<String>? = null

//floor option arry
var floorArray = arrayOf(1, 2, 3)
var floorOptionAdapter: ArrayAdapter<Int>? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

ctypeOptionAdapter =
ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, ctypeArray)
ctypeOption.adapter = ctypeOptionAdapter

floorOptionAdapter =
ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, floorArray)
floorOption.adapter = floorOptionAdapter

ctypeOption.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {}

override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
if (position >= 0 && position < floorArray.size) {
getSelectedListData(position)
} else {
Toast.makeText(
this@MainActivity,
"Selected Category Does not Exist!",
Toast.LENGTH_SHORT
).show()
}
}
}

val fileArrayList = arrayListOf(
ProjectFilesModel("A_file", "Architectural", 1),
ProjectFilesModel("B_file", "Mechanical", 1),
ProjectFilesModel("C_file", "Electrical", 2),
ProjectFilesModel("D_file", "Structural", 2),
ProjectFilesModel("E_file", "Architectural", 2),
ProjectFilesModel("F_file", "Mechanical", 3),
ProjectFilesModel("G_file", "Electrical", 3)
)

val fileArrayAdapter = FileDataAdapter(fileArrayList)
filteredListView.adapter = fileArrayAdapter
filteredListView.layoutManager = LinearLayoutManager(this)

}

fun getSelectedListData(position: Int) {
/* I'm stuck from here..*/
}
}

我不确定我会走正确的路..

请给我一些提示!

最佳答案

尝试如下。请引用评论。

适配器

class FileDataAdapter : RecyclerView.Adapter<FileDataViewHolder>() {
var items : List<ProjectFilesModel> = listOf()
set(value) {
// implements setter for notifying item changed
field = value
notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FileDataViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.filtered_file, parent, false)
return FileDataViewHolder(view)
}

override fun getItemCount(): Int = items.count()

override fun onBindViewHolder(holder: FileDataViewHolder, position: Int) {
holder.containerView.fileNameLayout.text = fileList[position].fileName
holder.containerView.ctypeLayout.text = fileList[position].ctype
holder.containerView.floorLayout.text = "${fileList[position].floor}floor"
}
}

Activity
class MainActivity : AppCompatActivity() {

//ctype option array
var ctypeArray =
arrayOf("All", "Architectural", "Mechanical", "Electrical", "Structural", "Civil")
var ctypeOptionAdapter: ArrayAdapter<String>? = null

//floor option arry
var floorArray = arrayOf(1, 2, 3)
var floorOptionAdapter: ArrayAdapter<Int>? = null

private val files = arrayListOf(
ProjectFilesModel("A_file", "Architectural", 1),
ProjectFilesModel("B_file", "Mechanical", 1),
ProjectFilesModel("C_file", "Electrical", 2),
ProjectFilesModel("D_file", "Structural", 2),
ProjectFilesModel("E_file", "Architectural", 2),
ProjectFilesModel("F_file", "Mechanical", 3),
ProjectFilesModel("G_file", "Electrical", 3)
)
private val adapter by lazy {
FileDataAdapter().apply {
items = files
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

ctypeOptionAdapter =
ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, ctypeArray)
ctypeOption.adapter = ctypeOptionAdapter

floorOptionAdapter =
ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, floorArray)
floorOption.adapter = floorOptionAdapter

ctypeOption.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {}

override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
//replace items in adapter to filtered list by selected CType
//TODO : stored selected ctype & floor and update filtered list logic
adapter.items = if(position == 0) files else files.filter {
it.ctype == ctypeArray[position]
}
}
}

//TODO : you should implement selected listener as below to filter files by floor
floorOption.onItemSelectedListener = ...


filteredListView.adapter = adapter
filteredListView.layoutManager = LinearLayoutManager(this)
}
}

附加 - 组合两个过滤器

设置监听器
ctypeOption.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {}

override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
invalidateItems()
}
}

floorOption.onItemSelectedListener = ... // same as ctypeOption

使用过滤器使函数无效
private fun invalidateItems() {
//position == 0 isn't filter items by selected option
val cTypeFilter: (ProjectFilesModel) -> (Boolean) = { item ->
if (ctypeOption.selectedItemPosition != 0) item.ctype == ctypeArray[spinner.selectedItemPosition]
else true
}
val floorFilter: (ProjectFilesModel) -> (Boolean) = { item ->
if (floorOption.selectedItemPosition != 0) item.floor == floorArray[spinner.selectedItemPosition]
else true
}

adapter.items = files.filter { cTypeFilter(it) && floorFilter(it) }
}

关于android - Kotlin:如何使用 Spinner 将项目设置为 RecyclerViewAdapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60538467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com