【问题标题】:How to programatically select the default Chip in a ChipGroup in Android?如何以编程方式选择 Android 芯片组中的默认芯片?
【发布时间】:2020-05-13 02:07:30
【问题描述】:

TL;DR - 以编程方式选择默认选择 Chip(它是 Android 中 ChipGroup 的子级)的正确方法是什么?

--

在我的 Android 应用程序中,我使用 com.google.android.material.chip.Chip 样式为 @style/Widget.MaterialComponents.Chip.Choice 组件来表示用户可以为给定路线选择的活动选择(想想步行、骑自行车等)

因为路线可以有不同类型的活动,我以编程方式将每种类型作为不同的芯片插入com.google.android.material.chip.ChipGroup。我还在片段的 onViewCreated() 期间使用以下代码选择默认芯片作为插入列表中的第一个芯片

 private fun setupTypeSelection(types: List<Type>) {
    types.forEach { type ->
        val chip = layoutInflater.inflate(R.layout.chip_type, viewBinding.typeChipGroup, false) as Chip

        chip.tag = type
        /* Init chip text and icon */

        chip.setOnClickListener {
            /* Update selected type */
        }

        if (currentType == null) {
            chip.isSelected = true
            currentType = type
        }

        viewBinding.typeChipGroup.addView(chip)
    }
}

这是 ChipGroup 的布局定义,其中我设置了单选等

chip_group_layout.xml

<com.google.android.material.chip.ChipGroup
      android:id="@+id/type_container"

      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="@dimen/margin_medium"

      app:chipSpacingHorizontal="@dimen/margin_medium"
      app:selectionRequired="true"
      app:singleLine="true"
      app:singleSelection="true" />

这是芯片布局

chip_type.xml

<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:app="http://schemas.android.com/apk/res-auto"

     style="@style/Widget.MaterialComponents.Chip.Choice"

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"

     app:chipIconEnabled="true" />

我面临的问题是,即使在用户通过 UI 交互选择了不同的芯片后,以编程方式设置为选中 chip.isSelected = true 的芯片仍保持选中状态。

在 Android 中以编程方式选择作为 ChipGroup 子级的默认选择 Chip 的正确方法是什么?

【问题讨论】:

    标签: android android-layout material-design android-chips


    【解决方案1】:

    找到我的答案。

    • 使用 View.generateViewId() 并将新 ID 分配给 新创建的芯片。然后,添加
    • 将芯片添加到其父芯片组
    • 使用viewBinding.typeChipGroup.check(id)检查芯片视图ChipGroup

    这是最终代码:

    private fun setupTypeSelection(types: List<Trail.Type>) {
    
        types.forEach { type ->
            val chip = layoutInflater.inflate(R.layout.chip_trail_type, viewBinding.typeContainer, false) as Chip
    
            chip.id = View.generateViewId()
            /* Set chip details as usual */
    
            viewBinding.typeContainer.addView(chip)
    
            if (currentType == null) viewBinding.typeChipGroup.check(chip.id)
        }
    }
    

    【讨论】:

      【解决方案2】:

      currentType == null 条件将在下一次选择时为假。您是否在其他任何地方都将其设为 null?

      使用 ChipGroup 的 setOnCheckedChangeListener 代替 chip.setOnClickListener 来检查芯片选择的状态

      【讨论】:

      • currentType 被初始化为 null,所以当第一个芯片被创建时,currentType 被设置为它的类型。
      • 感谢您指出 setOnCheckedChangeListener。不幸的是,没有回答我原来的问题
      猜你喜欢
      • 2021-01-25
      • 2019-12-19
      • 2020-11-01
      • 2019-09-11
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      相关资源
      最近更新 更多