使用自动完成文本视图在 Android 中单行显示多个芯片/标签

问题描述 投票:0回答:2

我想在单行中显示多个芯片,可能有两个或多个,并在后续行中显示。请参阅附图,我想要与图像中显示的相同的。在此处输入图像描述

java android tags android-design-library android-chips
2个回答
1
投票

将 FlexboxLayoutManager 与 RecyclerView 结合使用。

        val layoutManager = FlexboxLayoutManager(this)
        layoutManager.flexDirection = FlexDirection.ROW
        layoutManager.flexWrap = FlexWrap.WRAP
        binding.rvFilterItem.layoutManager = layoutManager
        binding.rvFilterItem.adapter = tagAdapter

0
投票

要在单行中显示两个或更多芯片并在后续行中显示多个芯片,请使用 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");
     }
 }
© www.soinside.com 2019 - 2024. All rights reserved.