我需要在水平滚动视图中使用 android 芯片实现一个解决方案。当有许多芯片要过滤时,我发现的所有库都使用多行解决方案。但是我希望我的在一行中并滚动浏览它的容器。
我知道 Pinterest 等应用程序已经使用了这个和概念,但是我无法绕过它。
我需要在水平滚动视图中使用 android 芯片实现一个解决方案。当有许多芯片要过滤时,我发现的所有库都使用多行解决方案。但是我希望我的在一行中并滚动浏览它的容器。
我知道 Pinterest 等应用程序已经使用了这个和概念,但是我无法绕过它。
尝试将您的 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>
用它来隐藏滚动条
<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>
尝试将 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>
然后动态地将芯片添加到芯片组
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);
}
});
}
查看适用于 Android的Nanochips
Nanochips 是一个用于 Android 的库,它提供了一个自定义的 TextView,允许用户在文本字段中输入文本并创建材料芯片。
dependencies {
implementation 'com.github.969rishi:nanochips:1.0.0'
}
您需要在 HorizontalScrollView 中添加您的ChipGroup,然后在您的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>