Does anyone know how to do flow layout using RecyclerView?
Asked Answered
U

3

7

Does anyone know how to do flow layout using RecyclerView?

How to change span count dynamically?

Answer :

Like this

Undeniable answered 1/12, 2015 at 4:13 Comment(5)
Can you please explain in detail ?Godunov
i want design like in above picture, i think this can be achieved by recycler-view but don't know how to do itUndeniable
github.com/hongyangAndroid/FlowLayout this may help you.Formfitting
Thankyou @DhinakaranThennarasuUndeniable
Have you managed to find a solution for this? All the libraries that are out there, are just adding views to the ViewGroup, no recycling is involved.Coagulant
V
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

Vocalic answered 18/12, 2015 at 8:38 Comment(1)
how can I add multiple sections of tags? I want to do it dynamicallySpindling
P
6

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.

Prang answered 25/2, 2021 at 13:7 Comment(2)
saved my day... easiest solutionVtol
this should be the accepted answer, kudos!Ruffner
V
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

Vocalic answered 18/12, 2015 at 8:38 Comment(1)
how can I add multiple sections of tags? I want to do it dynamicallySpindling
G
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);
    }
Godunov answered 1/12, 2015 at 4:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.