【问题标题】:implementing a ChipGroup with adapter pattern使用适配器模式实现 ChipGroup
【发布时间】:2021-01-10 09:42:13
【问题描述】:

我的片段包含一个ChipGroup,其中chips 是根据用户的选择动态添加的。我希望完成的是一个适配器模式,其中list 暴露给正在创建chips 的适配器,用户可以在列表中添加/删除元素。

我找不到任何方法来做到这一点,而现在,每当用户与列表交互时,都会重新创建所有芯片。

为简洁起见,代码已被剪辑

XML 文件

// capturing the user's choice
      <RadioGroup
          android:id="@+id/vehicles_radio_group"
          android:onCheckedChanged="@{vehicleViewModel::radioCheckedChanged}">

视图模型

fun radioCheckedChanged(radioGroup: RadioGroup, checkedId: Int) {

        //validating the user's choice
            if (condition) {
                //code for adding the choice to a List
                vehicleDispatched(selectedVehicle, checkedId)   
            } else {
                selectionError()
            }
    }

    // adding the user's choice to the list
    // _dispatchingUnits is a LiveData

    private fun vehicleDispatched(selectedVehicle: DomainVehicle, checkedId: Int) {
  
     // appending the selected choice to the list(Type : List<Pair<Vehicle,Planet>>)
        _dispatchingUnits.value =
            _dispatchingUnits.value?.plus(selectedVehicle to _currentPlanet.value!!)
                ?: listOf(selectedVehicle to _currentPlanet.value!!)
    }

片段

// register an observer and take action

        myViewModel.dispatchingUnits.observe(viewLifecycleOwner) { allPairs ->
            allPairs?.apply {

                with(binding) {
                   // remove the existing chips if any
                    if (selectedPairChipGroup.isNotEmpty()) {
                        selectedPairChipGroup.removeAllViews()
                    }
                  // create new chips
                    allPairs.forEach { chipPair ->
                        createChips(chipPair)
                    }
                }
            }
        }

期待类似,

// In fragment class
val adapter = ChipAdapter()
binding.chipGroup.setAdapter(adapter)


// In bindingUtils file
@BindingAdapter("fill_data")
fun RecyclerChipGroup.setData(list : MyListType){
val adapter = this.adapter as ChipAdapter
adapter.submitChipList(list)
}

//XML File
<RecyclerChipGroup
....
app:fill_data = "@{viewModel.dispatchingUnits}"
....
/>

如何继续?

【问题讨论】:

  • 对于多个动态变化的芯片,可以使用材料芯片(material.io/components/chips)。要为芯片创建适配器,您可以访问douevencode.com/articles/2017-06/…
  • 我确实用材料芯片来显示用户的选择,后面的链接好像坏了
  • 哦,好吧。该链接提到了github.com/BelooS/ChipsLayoutManager 这个使用回收器视图布局管理器的存储库。如果您不想在项目中使用库,那么这样做的概念是创建一个回收器视图及其带有芯片的适配器,而不是使用chipGroup,因为它将提供回收器视图的所有功能并且比chipGroup 更灵活。
  • 感谢链接,非常有用,想法相同,只是使用recyclerview 而不是chipgroup 但这给我留下了一个未回答的问题,为什么在第一个有chipgroup如果我们可以和RecyclerView一起去吗?
  • 是的,recycler view 和 chipGroup 有不同的用例,正如您在问题中提到的,您需要一个基于适配器的芯片列表。而且由于chipGroup没有适配器,所以RecyclerView是实现相同目标的最佳方式。

标签: android adapter material-components-android android-chips


【解决方案1】:

如果您想实现基于适配器的芯片列表和比 ChipGroup 更多的控件,那么您可以使用 RecyclerView 将芯片作为一个项目并以从左到右或从右到左的顺序排列芯片,您可以创建一个自定义布局管理器。

更多详情,您可以访问https://github.com/BelooS/ChipsLayoutManager。该库提供对 ChipsLayoutManager 的支持。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多