我在 parentAdapter 中有 recyclerview,它具有三种不同的布局多项选择(标题和单选按钮)、评论(标题和编辑文本)和布尔值(标题和单选按钮(Y/N))。这些布局基于 API 响应设置。现在 boolean 和 Multichoice 有 child-recyclerview 和单独的 child-adapter 包含 radioButton,期望单选但取消选中并且单选没有按预期发生。
这是 BindViewHolder 上的父适配器,
override fun onBindViewHolder(holder: InspectionAdapter.ViewHolder, position: Int) {
val model: QueryList = item.get(position)
if(model.QueryType.equals("YES / NO")){
holder.booleanQueryTitleLayout.visibility = View.VISIBLE
holder.booleanQueryBodyLayout.visibility = View.VISIBLE
holder.stringLayout.visibility = View.GONE
holder.mcLayout.visibility = View.GONE
holder.tvBooleanQueryName.text = model.QuestionName
val booleanArray: List<String> = model.QueryType.split("/")
var booleanList1 : List<String>?=null
booleanList1 = booleanArray
booleanList1 = booleanList1.toCollection(ArrayList())
if(booleanList1.size>0) {
holder.rvBooleanQuery.layoutManager = LinearLayoutManager(context)
booleanAdapter = context?.let { BooleanAdapter(it, booleanList1, position, item[position].QueryType) }!!
holder.rvBooleanQuery.adapter = booleanAdapter
holder.rvBooleanQuery.setHasFixedSize(true);
holder.rvBooleanQuery.setLayoutManager(holder.rvBooleanQuery.layoutManager);
booleanAdapter!!.setOnItemClickListener(this);
}
}
else if(model.QueryType.equals("string")){ //Comment Type
}
else { // Multi-choice Type
//multi-choice recyclerView
}
}
这是 childAdapter 的 onBindViewHolder()(布尔大小写)
override fun onBindViewHolder(holder: BooleanAdapter.ViewHolder, position: Int) {
Log.e(TAG,"booleanAdapterName"+ item.get(position))
holder.booleanQueryName.setChecked(position == mSelectedItem)
holder.booleanQueryName.text = item.get(position)
if (mSelectedItem == position) {
holder.booleanQueryName.setChecked(true);
} else {
holder.booleanQueryName.setChecked(false);
}
holder.booleanRadioGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener {
group, checkedId ->
this.mSelectedItem = holder.getAdapterPosition()
if (holder.booleanQueryName.isChecked) {
mSelectedItem = position
} else {
mSelectedItem = -1
}
})
holder.booleanQueryNameYes.setOnClickListener {
// if(selectedPosition == position){
hideSoftKeyboard(context, it)
mSelectedItem = holder.getAdapterPosition()
if(holder.booleanQueryNameYes.isChecked){
holder.booleanQueryNameYes.setChecked(true)
holder.booleanQueryNameYes.isChecked = true
Toast.makeText(context, "Yes", Toast.LENGTH_SHORT).show()
notifyDataSetChanged()
} else {
holder.booleanQueryNameYes.setChecked(false)
holder.booleanQueryNameYes.isChecked = false
Toast.makeText(context, "No", Toast.LENGTH_SHORT).show()
notifyDataSetChanged()
}
}
}
尝试了选择的最后一个适配器位置,并尝试在 if 条件下取消选中,但这不起作用。如何做到这一点?
而不是将
holder.booleanQueryName
设置为 false
使用 clearCheck()
方法,这样它会正常工作。