【发布时间】:2018-07-25 09:48:28
【问题描述】:
我有一个回收器视图,它获取一个数组并为它的每个项目创建一个复选框。我希望在回收站视图的顶部有一个复选框,如果它被选中,那么回收站视图的所有复选框也会被选中。我怎样才能做到这一点?
回收站视图适配器:
class RecyclerViewAdapter(val context: Context, val myArray: Array<String>): RecyclerView.Adapter<RecyclerViewAdapter.Holder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val view = LayoutInflater.from(context).inflate(R.layout.recycler_view_pattern,parent,false)
return Holder(view)
}
override fun getItemCount(): Int {
return myArray.count()
}
override fun onBindViewHolder(holder: Holder, position: Int) {
return holder.bind(myArray[position])
}
inner class Holder(itemView: View?): RecyclerView.ViewHolder(itemView){
val checkBox = itemView?.findViewById<CheckBox>(R.id.checkBox)
fun bind(str: String){
checkBox?.text = str
checkBox?.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
if (checkBox.isChecked){
//do something
}
else{
//do something
}
}
})
}
}
}
回收站视图模式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="14sp" />
</LinearLayout>
【问题讨论】:
-
@NileshRathod Dude 我知道如何使用复选框创建回收站视图。我需要一个“检查所有”复选框来检查其他复选框
-
制作一个包装字符串的模型以及一个布尔值以指示是否选中。使用布尔值来初始化视图绑定中的复选框。选中主复选框后,将所有模型更新为 checked=true 并使用这些新模型更新 RV 适配器
-
@TimCastelijns 谢谢你的回答,但我是初学者,你能写下这个解决方案的代码吗?
-
我可以,但如果我这样做了,你什么也学不到
标签: android checkbox android-recyclerview kotlin