可横向滚动的Android芯片

6
我需要使用Android chips在一个水平滚动的视图中实现一个解决方案。我发现的所有库都是在有许多芯片需要过滤时使用多行解决方案。然而,我希望我的芯片在单行中,并通过其容器滚动。
我知道像Pinterest这样的应用程序已经使用了这个概念,但我不知道如何解决它。 enter image description here enter image description here
5个回答

11

尝试将您的ChipGroup放在一个HorizontalScrollView布局中。

<HorizontalScrollView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <android.support.design.chip.ChipGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <android.support.design.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:chipText="This" />

    <android.support.design.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:chipText="is" />

    // more chips...

  </android.support.design.chip.ChipGroup>
</HorizontalScrollView>

感谢您的回答!幸运的是,自从这个问题被发布以来,Android在他们的设计库中创建了芯片,并且我能够通过它来实现。 - Michele La Ferla

4

使用此代码可以隐藏滚动条

                <HorizontalScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="none">

                    <com.google.android.material.chip.ChipGroup
                        android:id="@+id/chipsPrograms"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:layout_marginBottom="8dp"
                        android:paddingStart="@dimen/text_margin"
                        android:paddingEnd="@dimen/text_margin"
                        app:chipSpacing="8dp"
                        app:singleSelection="true"/>
                </HorizontalScrollView>

2

2
尝试将ChipGroup放置在HorizontalScrollView中。
<HorizontalScrollView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <android.support.design.chip.ChipGroup
    android:id="@+id/chipGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</HorizontalScrollView>

然后动态地将芯片添加到ChipGroup中

for(String string:set){
        Chip chip = new Chip(binding.chipGroup.getContext());
        LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(5,5,5,5);
        chip.setLayoutParams(layoutParams);
        chip.setText(string);
        chip.setCloseIconEnabled(true);
        chip.setChipBackgroundColor(getResources().getColorStateList(R.color.colorChipIconTint));
        chip.setTextColor(getResources().getColorStateList(R.color.colorChipText));
        chip.setCloseIconTint(getResources().getColorStateList(R.color.colorChipCloseIcon));
        chip.setClickable(true);
        chip.setCheckable(false);
        binding.chipGroup.addView(chip );
        chip.setOnCloseIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //binding.chipGroup.removeView(v);
                setDataContainer.remove((String)((Chip)v).getText());
            }
        });

        chip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SMSHomeActivity.this,PhoneNumberActivity.class);
                ArrayList<GroupMobileNumberModel> groupMobileNumberModelList = (ArrayList<GroupMobileNumberModel>) PhoneNumberActivity.groupMobileNumberModelList();
                intent.putParcelableArrayListExtra("groupMobileNumberModelList",groupMobileNumberModelList);
                startActivity(intent);
            }
        });
    }

1
你需要将ChipGroup添加到HorizontalScrollView中,然后在ChipGroup属性中使用app:singleLine="true"
<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
  <com.google.android.material.chip.ChipGroup
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:singleLine="true">

    <!-- Chips can be declared here, or added dynamically. -->

  </com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接