DevAhamed / MultiViewAdapter

Easily create complex recyclerview adapters in android
https://devahamed.github.io/MultiViewAdapter
Apache License 2.0
818 stars 148 forks source link

Can I add multiple binder with same DataModel? #100

Closed kawtikwar closed 5 years ago

kawtikwar commented 5 years ago

I want to add multiple binders with the same DataModel as shown below

Binder1 class

public class Binder1 extends ItemBinder<DataModel, Binder1.ViewHolder> {

    @Override
    public Binder1 createViewHolder(ViewGroup parent) {
        return new ViewHolder(inflate(parent,R.layout.list_item));
    }

    @Override
    public void bindViewHolder(ViewHolder holder, DataModel dataModel) {

    }

    @Override
    public boolean canBindData(Object item) {
        return item instanceof DataModel;
    }

    static class ViewHolder extends ItemViewHolder<DataModel> {

        public NotificationViewHolder(View itemView) {
            super(itemView);

        }
    }
}

Binder2 class

public class Binder2 extends ItemBinder<DataModel, Binder2.ViewHolder> {

    @Override
    public Binder2 createViewHolder(ViewGroup parent) {
        return new ViewHolder(inflate(parent,R.layout.list_item_one));
    }

    @Override
    public void bindViewHolder(ViewHolder holder, DataModel dataModel) {

    }

    @Override
    public boolean canBindData(Object item) {
        return item instanceof DataModel;
    }

    static class ViewHolder extends ItemViewHolder<DataModel> {

        public NotificationViewHolder(View itemView) {
            super(itemView);

        }
    }
}
 MultiViewAdapter adapter = new MultiViewAdapter();

            recyclerView.setAdapter(adapter);

            // Register Binder
            adapter.registerItemBinders(new Binder1(), new Binder2());

            // Create Section and add items
            ListSection<DataModel> listSection = new ListSection<>();
  List<DataModel> list = new ArrayList<>();
  list.add(new DataModel());
  list.add(new DataModel());
  listSection.addAll(list);

            // Add Section to the adapter
            adapter.addSection(listSection);

Can I do like the above? If not then how to achieve this using MultiViewAdapter.

I have the same model class but need to show different View Type on the basis of some parameter̥

DevAhamed commented 5 years ago

Yes, you can do that.

For example, inside Binder1,

    @Override
    public boolean canBindData(Object item) {
        return item instanceof DataModel && ((DataModel) item).getSomeParameter().equals(MY_VALUE_HERE);
    }

Similarly you can do that for other binders as well.

kawtikwar commented 5 years ago

Thank You @DevAhamed