Skip to content

Instantly share code, notes, and snippets.

@vivchar
Last active March 2, 2018 06:42
Show Gist options
  • Save vivchar/d96e750c26bdbb200e7cdb40c80e3e0a to your computer and use it in GitHub Desktop.
Save vivchar/d96e750c26bdbb200e7cdb40c80e3e0a to your computer and use it in GitHub Desktop.
RecyclerView with multiple view types https://github.com/vivchar/RendererRecyclerViewAdapter
dependencies {
compile 'com.github.vivchar:RendererRecyclerViewAdapter:2.5.0'
}
mRecyclerViewAdapter = new RendererRecyclerViewAdapter(getContext());
mRecyclerViewAdapter.enableDiffUtils();
mRecyclerViewAdapter.registerRenderer(new YourViewBinder());
//mRecyclerViewAdapter.registerRenderer(new OtherYourViewBinder());
mRecyclerViewAdapter.setItems(getYourItems());
<LinearLayout ... >
<TextView android:id = "@+id/title" ... />
<ImageView android:id = "@+id/image" ... />
<Button android:id = "@+id/button" ... />
<CheckBox android:id = "@+id/check" ... />
<CustomView android:id = "@+id/custom" ... />
</LinearLayout>
public class YourModel implements ViewModel {
String getTitle() {...} //your method
int getTitleColor() {...} //your method
Bitmap getImage() {...} //your method
boolean isImageVisible() {...} //your method
boolean isChecked() {...} //your method
}
public class YourViewBinder extends ViewBinder<YourModel> {
public YourViewBinder() {
super(
R.layout.your_model_layout, //your item layout
YourModel.class, //your model class
(model, finder, payloads) -> finder
.setText(R.id.title, model.getTitle())
.setTextColor(R.id.text, model.getTitleColor())
.setImageBitmap(R.id.image, model.getImage())
.setVisible(R.id.image, model.isImageVisible())
.setOnClickListener(R.id.button, new OnClickListener() {
//...
})
.setChecked(R.id.check, model.isChecked())
.find(R.id.custom, new ViewProvider<CustomView>() {
//...
})
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment