【问题标题】:How to display all the elements of ArrayList as Chips in Android如何在Android中将ArrayList的所有元素显示为芯片
【发布时间】:2019-10-03 03:01:44
【问题描述】:

我有一个ArrayList<String> 这样的[Angularjs, JavaScript, Css]

在我的适配器中,我从另一个 Intent 获取标签的 ArrayList。我尝试使用以下方法将ArrayList 的所有元素显示为 Chips,但我只得到了 ArrayList 的最后一个元素。我只看到 one 带有 ArrayList 的最后一个元素的芯片。

for (String tag: card.getTags()) {
  Log.d(TAG, "tag - " + tag);
  holder.tags.setText(tag);
}

谁能指导一下,如何更改我的代码以将 ArrayList 的所有元素显示为Chips

【问题讨论】:

    标签: java android arraylist android-chips


    【解决方案1】:

    如果你在谈论Chips

    在要显示筹码的 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 in 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);
      }
    

    【讨论】:

    • 太好了,就是这样!有效。我刚刚做了holder.chipgroup.add(chip)
    【解决方案2】:

    目前,您正在替换持有人的forEach 循环中的tags。您只有一个持有者并不断更新,直到循环结束;这就是你最后的原因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,因为我在您的 sn-p 中看到了支架。

    以下是视图层次结构;我假设您正在努力实现;如果没有,那么您可以使用相同的逻辑线来移动这些部分

    • 回收站查看
      • 每个项目/视图
        • 标签 - 使用 FlowLayout 的多个标签 - 在视图中添加多个标签

    您需要bind RecyclerView 中的每个项目。

    @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中绑定每个item/View:

    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());
    }
    

    此外,你可以参考这个SO question,因为它在同一行。

    【讨论】:

    • 感谢您的详细回答,但我使用的是ChipGroup,在其中我想设置来自 ArrayList 的芯片。我应该在我的问题本身中提到这一点。您能否指导如何实现这一目标?
    • @Pavan 啊!如果我能感觉到你指的是 MaterialComponent 的ChipGroup;我本可以在几分钟内发布答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2020-10-09
    • 2015-12-09
    相关资源
    最近更新 更多