调用notifyDataSetChanged时,仅更新RecyclerView中的最后一项

问题描述 投票:0回答:1
我想建立一个

分段明智列表选择功能。 为此,我的布局文件中有多个 RecyclerView(例如 5 个),并且每个 RecyclerView 的数据列表都是动态的。As you can see in this image

现在,recyclerview 中的每个项目都有一个

checkbox。我需要做的是,当用户选择任何项目时,仅应启用该特定列表中的那些项目,而应禁用其他分段的列表项目。

我面临的问题是,当我尝试使用

notifyDataSetChanged 更新每个 recyclerview 时,只有列表中的最后一项被禁用,其余项被启用。我调试了该应用程序,并且流程方面一切正常,但应用程序没有反映相同的情况。As you can see here

我的数据模型 -

data class SelectionModel( var channelType: String, var channelName: String, var devices: ArrayList<SelectiveDevice>? ) { data class SelectiveDevice( var deviceName: String, var deviceType: String, var selected: Boolean = false ) }
我的主要活动 - 

class MainActivity : AppCompatActivity(), FiveChannelDeviceAdapter.FiveChannelDeviceCallback, FourChannelDeviceAdapter.FourChannelDeviceCallback, ThreeChannelDeviceAdapter.ThreeChannelDeviceCallback, TwoChannelDeviceAdapter.TwoChannelDeviceCallback, OneChannelDeviceAdapter.OneChannelDeviceCallback { lateinit var mBinding: ActivityMainBinding private var segments = ArrayList<SelectionModel>() private lateinit var fiveChannelDeviceAdapter: FiveChannelDeviceAdapter private lateinit var fourChannelDeviceAdapter: FourChannelDeviceAdapter private lateinit var threeChannelDeviceAdapter: ThreeChannelDeviceAdapter private lateinit var twoChannelDeviceAdapter: TwoChannelDeviceAdapter private lateinit var oneChannelDeviceAdapter: OneChannelDeviceAdapter var selectedType = "" var selectedGroup = "" var countChecked = 0 var devices5ch = ArrayList<SelectionModel.SelectiveDevice>() var devices3ch = ArrayList<SelectionModel.SelectiveDevice>() var devices1ch = ArrayList<SelectionModel.SelectiveDevice>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) fiveChannelDeviceAdapter = FiveChannelDeviceAdapter(this@MainActivity, this@MainActivity) fourChannelDeviceAdapter = FourChannelDeviceAdapter(this@MainActivity, this@MainActivity) threeChannelDeviceAdapter = ThreeChannelDeviceAdapter(this@MainActivity, this@MainActivity) twoChannelDeviceAdapter = TwoChannelDeviceAdapter(this@MainActivity, this@MainActivity) oneChannelDeviceAdapter = OneChannelDeviceAdapter(this@MainActivity, this@MainActivity) mBinding.rvFiveCh.adapter = fiveChannelDeviceAdapter mBinding.rvFourCh.adapter = fourChannelDeviceAdapter mBinding.rvThreeCh.adapter = threeChannelDeviceAdapter mBinding.rvTwoCh.adapter = twoChannelDeviceAdapter mBinding.rvOneCh.adapter = oneChannelDeviceAdapter segments.clear() devices5ch.clear() devices3ch.clear() devices1ch.clear() val device1 = SelectionModel.SelectiveDevice("First", "5ch", false) val device2 = SelectionModel.SelectiveDevice("Second", "5ch", false) val device3 = SelectionModel.SelectiveDevice("Third", "5ch", false) val device4 = SelectionModel.SelectiveDevice("Forth", "3ch", false) val device5 = SelectionModel.SelectiveDevice("Fifth", "3ch", false) val device6 = SelectionModel.SelectiveDevice("Sixth", "1ch", false) devices5ch.add(device1) devices5ch.add(device2) devices5ch.add(device3) devices3ch.add(device4) devices3ch.add(device5) devices1ch.add(device6) val segment5ch = SelectionModel("5ch", "Color & CCT Lights", devices5ch) val segment3ch = SelectionModel("3ch", "Color Lights", devices3ch) val segment1ch = SelectionModel("1ch", "White Lights", devices1ch) segments.add(segment5ch) segments.add(segment3ch) segments.add(segment1ch) fiveChannelDeviceAdapter.setAdapterData(segments[0].devices!!, selectedType, selectedGroup, countChecked) threeChannelDeviceAdapter.setAdapterData(segments[1].devices!!, selectedType, selectedGroup, countChecked) oneChannelDeviceAdapter.setAdapterData(segments[2].devices!!, selectedType, selectedGroup, countChecked) } override fun resetSelection() { selectedType = "" selectedGroup = "" countChecked = 0 fiveChannelDeviceAdapter.setAdapterData(segments[0].devices!!, selectedType, selectedGroup, countChecked) threeChannelDeviceAdapter.setAdapterData(segments[1].devices!!, selectedType, selectedGroup, countChecked) oneChannelDeviceAdapter.setAdapterData(segments[2].devices!!, selectedType, selectedGroup, countChecked) } override fun setReverseData( selectedType: String, selectedGroup: String, countChecked: Int, deviceList: ArrayList<SelectionModel.SelectiveDevice> ) { this.selectedType = selectedType this.selectedGroup = selectedGroup this.countChecked = countChecked val alteredList = java.util.ArrayList<SelectionModel>() alteredList.addAll(segments) for (group in alteredList) { if (group.channelType.equals(this.selectedType, true)) { group.devices = deviceList } } fiveChannelDeviceAdapter.setAdapterData(alteredList[0].devices!!, selectedType, selectedGroup, countChecked) threeChannelDeviceAdapter.setAdapterData(alteredList[1].devices!!, selectedType, selectedGroup, countChecked) oneChannelDeviceAdapter.setAdapterData(alteredList[2].devices!!, selectedType, selectedGroup, countChecked) } }
其中一个适配器 - 

class FiveChannelDeviceAdapter( private val context: Context, private val callback: FiveChannelDeviceCallback ) : RecyclerView.Adapter<FiveChannelDeviceAdapter.FiveChannelDeviceViewHolder>() { private var mDeviceLists = ArrayList<SelectionModel.SelectiveDevice>() private lateinit var channelDevices: LayoutChannelDeviceBinding var selectedTypes = "" var selectedGroups = "" var countCheckeds = 0 inner class FiveChannelDeviceViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) { fun setData(position: Int) { if (countCheckeds > 0) { if (mDeviceLists[position].deviceType == selectedTypes) { Log.d("SegmentSelected", position.toString()) channelDevices.selectDeviceCheck.setOnCheckedChangeListener(null) channelDevices.selectDeviceCheck.isEnabled = true channelDevices.selectDeviceCheck.isClickable = true channelDevices.selectDeviceCheck.isChecked = mDeviceLists[position].selected channelDevices.tvDeviceName.text = "Enabled" } else { Log.d("SegmentNotSelected", position.toString()) channelDevices.selectDeviceCheck.isEnabled = false channelDevices.selectDeviceCheck.isClickable = false channelDevices.tvDeviceName.text = "Disabled" } } else { Log.d("SegmentReset", position.toString()) channelDevices.selectDeviceCheck.isEnabled = true channelDevices.selectDeviceCheck.isClickable = true channelDevices.tvDeviceName.text = "Enabled" } channelDevices.selectDeviceCheck.setOnCheckedChangeListener { compoundButton, checked -> if (checked) { countCheckeds += 1 mDeviceLists[position].selected = checked selectedTypes = mDeviceLists[position].deviceType selectedGroups = selectedTypes Log.d("DeviceList", mDeviceLists.toString()) callback.setReverseData(selectedTypes, selectedGroups, countCheckeds, mDeviceLists) } else if (countCheckeds > 0 && !checked) { countCheckeds -= 1 mDeviceLists[position].selected = checked if (countCheckeds == 0) { countCheckeds = 0 selectedTypes = "" selectedGroups = "" callback.resetSelection() } else { callback.setReverseData(selectedTypes, selectedGroups, countCheckeds, mDeviceLists) } } else if (countCheckeds == 0 && !checked) { countCheckeds = 0 mDeviceLists[position].selected = checked selectedTypes = "" selectedGroups = "" callback.resetSelection() } } } } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FiveChannelDeviceViewHolder { channelDevices = LayoutChannelDeviceBinding.inflate( LayoutInflater.from(parent.context), parent, false ) return FiveChannelDeviceViewHolder(channelDevices.root) } override fun onBindViewHolder(holder: FiveChannelDeviceViewHolder, position: Int) { holder.setData(position) } override fun getItemCount(): Int { return mDeviceLists.size } fun setAdapterData(mDeviceList: ArrayList<SelectionModel.SelectiveDevice>, selectedType: String, selectedGroup: String, countChecked: Int) { this.mDeviceLists.clear() this.mDeviceLists.addAll(mDeviceList) this.selectedTypes = selectedType this.selectedGroups = selectedGroup this.countCheckeds = countChecked notifyDataSetChanged() } interface FiveChannelDeviceCallback { fun resetSelection() fun setReverseData(selectedType: String, selectedGroup: String, countChecked: Int, deviceList: ArrayList<SelectionModel.SelectiveDevice>) } }
    
android kotlin checkbox android-recyclerview multi-select
1个回答
0
投票
您是否尝试过在单个 RecyclerView 中完成所有这些操作? 使用 Adapter 的 getItemViewType() 方法。

就像,创建两个布局:

    标题布局[为章节提供标题]
  1. 项目布局[带有复选框的一个]
创建一个主列表,其中包含这样的密封类

sealed class SectionItemUIModel{ data class Heading(val heading:String):SectionItemUIModel() data class Item(val sectionItem:SectionItem):SectionItemUIModel() }
然后在recyclerView Adapter中,你将有一个名为 

getItemViewType 的函数

override fun getItemViewType(position:Int) = when(getItem(position)){ Heading->0 Item->1 }
现在在 

OnCreateViewHolder(){} 中使用它,您将在其中获得 viewType 的参数

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): when(viewType){ 0->HeadingHolder() 1->ItemHolder() }
这将帮助您为所有字段创建一个列表,并且可以轻松处理选择布尔值以打开和关闭。
也将采取更短的方法来完成。

© www.soinside.com 2019 - 2024. All rights reserved.