我想在单行中显示多个芯片,可能有两个或多个,并在后续行中显示。请参阅附图,我想要与图像中显示的相同的。在此处输入图像描述
将 FlexboxLayoutManager 与 RecyclerView 结合使用。
val layoutManager = FlexboxLayoutManager(this)
layoutManager.flexDirection = FlexDirection.ROW
layoutManager.flexWrap = FlexWrap.WRAP
binding.rvFilterItem.layoutManager = layoutManager
binding.rvFilterItem.adapter = tagAdapter
要在单行中显示两个或更多芯片并在后续行中显示多个芯片,请使用 FlexboxLayoutManager 和 Recyclerview。
public class YourActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<String> arrayList = new ArrayList<>();
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
favArray();
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setJustifyContent(JustifyContent.CENTER);
layoutManager.setAlignItems(AlignItems.CENTER);
recyclerView.setLayoutManager(layoutManager);
adapter = new CustomAdapter(YourActivity.this, arrayList);
recyclerView.setAdapter(adapter);
}
private void favArray() {
arrayList.add("The dark knight");
arrayList.add("The god father");
arrayList.add("12 Angry Man");
arrayList.add("Fight club");
arrayList.add("Star Wars");
arrayList.add("The god father : Part 2");
arrayList.add("The dark knight");
arrayList.add("The god father");
arrayList.add("The god father : Part 2");
}
}