我需要在水平滚动视图中使用android芯片实现解决方案。我找到的所有库都使用多线解决方案,因为有许多芯片需要过滤。但是我希望我能在一行中滚动它的容器。
我知道像Pinterest这样的应用程序已经使用了这个和概念,但是我无法绕过它。
答案 0 :(得分:2)
尝试将您的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>
答案 1 :(得分: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);
}
});
}
答案 2 :(得分:1)
使用它隐藏滚动条
<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>
答案 3 :(得分:1)
签出 Nanochips for Android
chips layout example image link
Nanochips是适用于Android的库,提供了自定义TextView,允许用户在文本字段中输入文本并创建材料碎片。
dependencies {
implementation 'com.github.969rishi:nanochips:1.0.0'
}
答案 4 :(得分: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>