亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android中如何將ArrayList的所有元素顯示為Chips

Android中如何將ArrayList的所有元素顯示為Chips

元芳怎么了 2023-09-20 16:29:14
我有一個ArrayList<String>這樣的[Angularjs, JavaScript, Css]在我的適配器中,我ArrayList從另一個意圖獲取標簽。我嘗試使用以下方法顯示ArrayListas Chips的所有元素,但我只得到 ArrayList 的最后一個元素。我只看到一個帶有 ArrayList 最后一個元素的 Chip。for (String tag: card.getTags()) {   Log.d(TAG, "tag - " + tag);   holder.tags.setText(tag); }誰能指導一下,我如何更改代碼以將 ArrayList 的所有元素顯示為Chips.
查看完整描述

2 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

如果你說的是芯片。

在 xml 布局中添加ChipGroup您想要顯示籌碼的位置

<com.google.android.material.chip.ChipGroup

?android:id="@+id/chipGroup"

?android:layout_width="match_parent"

?app:chipSpacingVertical="2dp"

?android:layout_height="wrap_content"/>

現在您可以使用此方法添加 ArrayList,如下Chip所示ChipGroup:


private void addChip(String pItem, ChipGroup pChipGroup) {

? ? Chip lChip = new Chip(this);

? ? lChip.setText(pItem);

? ? lChip.setTextColor(getResources().getColor(R.color.primary_text));

? ? lChip.setChipBackgroundColor(getResources().getColorStateList(R.color.chip_bg));


? ? pChipGroup.addView(lChip, pChipGroup.getChildCount() - 1);

? }


查看完整回答
反對 回復 2023-09-20
?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

目前,您正在更換支架的環路tagsforEach你只有一個持有者并不斷更新直到循環結束;這就是你最后的原因chip/tag

如果你指的是MaterialComponents的ChipGroup。

ChipGroup chipGroup = new ChipGroup(parentView.getContext());


String[] genres = {"Thriller", "Comedy", "Adventure"};

for(String genre : genres) {

? ?Chip chip = new Chip(parentView.getContext());

? ?chip.setText(genre);

? ?chipGroup.addView(chip);

}

我認為您一定正在使用,RecyclerView因為我在您的代碼片段中看到了持有者。

以下是視圖層次結構;我假設你正在努力實現;如果沒有,那么您可以使用相同的邏輯來移動這些部分

  • 回收視圖

    • 標簽 - 多個標簽使用FlowLayout- 在視圖中添加多個標簽

    • 每個項目/視圖

您需要bindRecyclerView 中的每個項目。

@Override

public void onBindViewHolder(final YourViewHolder holder, final int position) {

? ? ...

? ? // assuming you have FlowLayout in recyclerview view item

? ? // do add holder pattern to flowlayout as well

? ? fillAutoSpacingLayout(card.getTags(), holder.flowLayout);

}

在這里,我使用FlowLayout在 RecyclerView 的每個視圖中添加芯片或標簽。


在 onBindViewHolder 中綁定每個項目/視圖:


private void fillAutoSpacingLayout(ArrayList<String> tags, FlowLayout flowLayout) {


? ? for (String tag : tags) {

? ? ? ? TextView textView = buildLabel(tag);

? ? ? ? flowLayout.addView(textView);

? ? }

}


private TextView buildLabel(String text) {

? ? TextView textView = new TextView(this);

? ? textView.setText(text);

? ? textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);

? ? textView.setPadding((int)dpToPx(16), (int)dpToPx(8), (int)dpToPx(16), (int)dpToPx(8));

? ? textView.setBackgroundResource(R.drawable.label_bg);


? ? return textView;

}


private float dpToPx(float dp){

? ? return TypedValue.applyDimension(

? ? ? ? ? ? TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());

}


查看完整回答
反對 回復 2023-09-20
  • 2 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號