Android FlexAdapter is a recycler view adapter that supports multiview holders, filters, and child view click events.

Need some career advice or prepping for an Android developer interview? Hit me up on Topmate.io, and let's chat!

android flexadapter
Android FlexAdapter
android flexadapter
Android FlexAdapter

How to use Android FlexAdapter

This is your model class. You only need to add @FlexAdapter.Item to your model class. So there is no constraint on the supper class.

@FlexAdapter.Item 
public class LabelItem {
//anything...
}

2. Layout (renderer_label.xml)

<TextView     
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txtLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Android FlexAdapter
Android FlexAdapter
Android FlexAdapter
Android FlexAdapter

3. Renderer (LabelRenderer.java)

This is ViewHolder. Make sure super class and onBind methods.

public class LabelRenderer extends ItemRenderer<LabelItem> {   
//If the resource name is the same, the view is automatically assigned

private TextView txtLabel;

public LabelRenderer(View view) {
super(view);
}

@Override
protected void onBind(final LabelItem item) {
txtLabel.setText(item.getLabel());
}
}

4. Activity side

 //rendererFactory 
RendererFactory rendererFactory = new RendererFactory();
rendererFactory.put(LabelRenderer.class, R.layout.renderer_label);

//adapter
FlexAdapter flexAdapter = new FlexAdapter(rendererFactory);

//recyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(flexAdapter);

//add items
for (int index = 0; index < 20; index++) {
flexAdapter.addItem(new LabelItem(String.valueOf(index)));
}

Download

repositories {     
jcenter()
}

dependencies {
compile 'com.scglab.common:list-adapter:1.2.3'
}

Link to github Library

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like