【问题标题】:How to add choice chip dynamically in android studio in my case在我的情况下,如何在 android studio 中动态添加选择芯片
【发布时间】:2021-11-07 23:25:16
【问题描述】:

我有这样的模型类:

private String animalName, type;

public Model(){}

public Model(String animalName, String type){
 this.animalName = animalName;
 this.type = type;
}
//and getter and setters for all vars

ArrayList<Model>(); 根据我的模型类

所以我想检查我的ArrayList<>() 是否包含特定类型,即。 草食动物 然后根据它们的类型将这些动物添加到不同的 ArrayList 中,我想根据我的类型制作 CHOICE CHIPS。这是我的应用程序的一个示例:

注意我使用 RecyclerView 来显示图片

如何根据我的模型类添加选择芯片 并且 OnClick 在任何芯片负载上都会根据该显示图像。

感谢任何解决方案。

【问题讨论】:

标签: java android xml android-chips


【解决方案1】:

您可以像这样或任何您想要的名称创建一个方法

private void setChips(String type){
    Chip chip = new Chip(this);
    chip.setText(chipsTitle);
    chip.setCheckable(true);
    //if you not create chipGroup in you xml yet then create it 
    chipGroup.addView(chip);
}

然后您可以调用该方法并将您的类型传递给 ie - "Herbivores" 等。

setChips("Herbivores");

之后,您可以像这样将 ClickListeners 添加到您的芯片中:

for (int i = 0; i < chipGroup.getChildCount(); i++) {
     int finalI = i;
     chipGroup.getChildAt(i).setOnClickListener(v -> {
            Chip chip = chipGroup.findViewById(chipGroupFl.getChildAt(finalI).getId());
            chip.setChecked(true);

            //here you can call your method to load Images
            

        });
    }

【讨论】:

  • 是的!当然为什么不呢!
猜你喜欢
  • 1970-01-01
  • 2019-08-16
  • 2020-09-02
  • 2019-12-24
  • 2019-12-22
  • 1970-01-01
  • 2019-09-11
  • 2017-01-12
  • 1970-01-01
相关资源
最近更新 更多