-
Notifications
You must be signed in to change notification settings - Fork 183
Quick start with Composite Items
Vitaly Vivchar edited this page Mar 6, 2020
·
17 revisions
NOTE: start from v3.0.0 ViewBinder
was renamed to ViewRenderer
, CompositeViewBinder
to CompositeViewRenderer
Step 1: Create your child_item_layout.xml
<LinearLayout ... >
<TextView android:id = "@+id/title" ... />
<ImageView android:id = "@+id/image" ... />
<Button android:id = "@+id/button" ... />
<CustomView android:id = "@+id/custom" ... />
</LinearLayout>
Step 2: Add ViewModel
interface to your ChildItem
class
public class ChildItem implements ViewModel {
String getTitle() {...} /* your method */
int getTitleColor() {...} /* your method */
Bitmap getImage() {...} /* your method */
}
Step 3: Create your ViewBinder
private ViewRenderer getChildItemViewBinder() {
return new ViewBinder<>(
R.layout.child_item_layout,
ChildItem.class,
(model, finder, payloads) -> finder
.setText(R.id.title, model.getTitle())
.setTextColor(R.id.text, model.getTitleColor())
.setImageBitmap(R.id.image, model.getImageBitmap())
.setOnClickListener(R.id.button, new OnClickListener() {...})
.find(R.id.custom, new ViewProvider<CustomView>() {...})
);
}
Step 4: Create your parent_item_layout.xml
<LinearLayout ... >
<RecyclerView android:id = "@+id/recycler_view" ... />
</LinearLayout>
Step 5: Create the CompositeViewBinder
private ViewRenderer getParentItemViewBinder() {
return new CompositeViewBinder<>(
R.layout.parent_item_layout,
R.id.recycler_view,
DefaultCompositeViewModel.class /* imported from library */
//new ViewBinder.Binder(){...} /* if you need bind something else */
);
}
NOTE: If you need Custom LayoutManager
then you should extend CompositeViewBinder
and override createLayoutManager
method. See the Customization page.
Step 6: Generate your items
private List<DefaultCompositeViewModel> getParentItems() {
ArrayList<DefaultCompositeViewModel> parents = new ArrayList();
for (...) {
ArrayList<ChildItem> children = new ArrayList();
for () {
children.add(new ChildItem(...));
}
parents.add(new DefaultCompositeViewModel(children));
}
return parents;
}
Step 7: Initialize RendererRecyclerViewAdapter
and register your renderer
RendererRecyclerViewAdapter adapter = new RendererRecyclerViewAdapter();
adapter.registerRenderer(getParentItemViewBinder().registerRenderer(getChildItemViewBinder));
adapter.setItems(getParentItems());