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!
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"/>
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'
}