我有一個這樣的模型類:
。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來顯示圖片
。我怎樣才能根據我的模型類添加選擇芯片? 并在點擊任何芯片時顯示相應的圖片。
歡迎任何解決方案。
uj5u.com熱心網友回復:
你可以創建一個方法,像這樣命名或任何你想要的
你可以創建一個方法。
private void setChips(String type) {
Chip chip = new Chip(this)。
chip.setText(chipTitle)。
chip.setCheckable(true)。
//if you not create chipGroup in you xml yet then create it
chipGroup.addView(chip)。
}
然后你可以呼叫該方法,并傳遞你的型別,即--"食草動物 "等
。setChips("Herbivores"); //span>
之后你可以像這樣給你的芯片添加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)。
//這里你可以呼叫你的方法來加載影像。
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/307729.html
標籤:
下一篇:移除行動欄(安卓作業室

