6

Does anyone know how to do flow layout using RecyclerView?

How to change span count dynamically?

Answer :

Like this

Bincy Baby
  • 3,941
  • 5
  • 36
  • 62

3 Answers3

5

Best solution is to use a RecyclerView with google FlexLayoutManager

// Set layout manager
    val layoutManager = FlexboxLayoutManager(context)
    recyclerview.layoutManager = layoutManager

// Now you can add your normal recyclerview adapter

recyclerview.adapter = MyListAdapter(list)

Add below dependency in your build.gradle file

 implementation 'com.google.android:flexbox:3.0.0'

This will work like a charm.

Rizwan
  • 1,461
  • 12
  • 26
3

Here is the full example of using custom Library which acts like List GitHubLibrary TagLayout

  • Sample Code:-

mFlowLayout.setAdapter(new TagAdapter<String>(mVals) { @Override public View getView(FlowLayout parent, int position, String s) { TextView tv = (TextView) mInflater.inflate(R.layout.tv, mFlowLayout, false); tv.setText(s); return tv; } });

Using below code you can pre set selection you wanted:-

mAdapter.setSelectedList(1,3,5,7,8,9);

Will show result like below:-

enter image description here

Hardy
  • 2,576
  • 1
  • 23
  • 45
2

You can use FlowLayout and put it as a child of ScrollView. Samples for flow layout are available in repository.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.wefika.flowlayout.FlowLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="start|top"
            android:minHeight="50dp">

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button" />

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button" />
        </com.wefika.flowlayout.FlowLayout>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello world" />

    </LinearLayout>

</ScrollView>

Also you can add or remove views programatically using following methods given in sample.

 public void addItem(View view) {

        int color = getResources().getColor(R.color.holo_blue_dark);

        View newView = new View(this);
        newView.setBackgroundColor(color);

        FlowLayout.LayoutParams params = new FlowLayout.LayoutParams(100, 100);
        params.rightMargin = 10;
        newView.setLayoutParams(params);

        mFlowLayout.addView(newView);
    }

    public void removeItem(View view) {

        mFlowLayout.removeView(getLastView());

    }

    public void toggleItem(View view) {

        View last = getLastView();

        if(last.getVisibility() == View.VISIBLE) {
            last.setVisibility(View.GONE);
        } else {
            last.setVisibility(View.VISIBLE);
        }

    }

    private View getLastView() {
        return mFlowLayout.getChildAt(mFlowLayout.getChildCount() - 1);
    }
Arth Tilva
  • 2,496
  • 22
  • 40