Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create horizontal RecyclerView that auto new line when screen space is full?

I'm trying to use RecyclerView to display programmatically tags in my app. It would like this(sorry, i can't upload image to stackoverflow now)

http://anh.im/image/OUA

So, how can i create RecyclerView that show my contents left to right and auto new line at the right edge of RecyclerView look like the image and prevent it scroll horizontally? Thanks for any help!

like image 389
Anh Phạm Avatar asked Nov 01 '25 15:11

Anh Phạm


2 Answers

Use implementation 'com.google.android:flexbox:1.0.0'

Just add in your recyclerview

 val decoration = DividerItemDecoration(context, RecyclerView.HORIZONTAL)
    decoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider_10dp)!!)
    recyclerViewSelectionList.addItemDecoration(decoration)
    val layoutManager = FlexboxLayoutManager(this)
    layoutManager.flexDirection = FlexDirection.ROW
    layoutManager.justifyContent = JustifyContent.FLEX_START
    recyclerViewSelectionList.layoutManager = layoutManager
like image 128
Bhojaviya Sagar Avatar answered Nov 04 '25 05:11

Bhojaviya Sagar


Update 10/2022:

Use implementation 'com.google.android.flexbox:flexbox:3.0.0'. It's work for me!

FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getApplicationContext());
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setJustifyContent(JustifyContent.FLEX_START);
recyclerView.setLayoutManager(layoutManager);
like image 38
Trang Đỗ Avatar answered Nov 04 '25 05:11

Trang Đỗ