edwardharks / Multi-Type-RecyclerView-Adapter

A simple adapter for creating a RecyclerView with an unlimited number of view types

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multi Type RecyclerView Adapter

A simple adapter for creating a RecyclerView with an unlimited number of view types.

Usage

Declare your view types

public enum DemoViewTypes implements ViewType {

    VIEW_TYPE_ONE, VIEW_TYPE_TWO, FOOTER;

    @Override
    public int getType() {
        return ordinal();
    }
}

An Enum is a good choice for this

Create a class to hold your view type

public class ViewTypeOne {
    
}

Add a MultiTypeCreator, MultiTypeBinder, ViewHolder as static inner classes

public class ViewTypeOne {

    public static class Creator implements MultiTypeCreator {

        @NonNull
        @Override
        public ViewType getViewType() {
            return DemoViewTypes.VIEW_TYPE_TWO;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_type_two, parent, false);
            return new ViewHolder(view);
        }
    }

    public static class Binder implements MultiTypeBinder {

        private final String mData;

        public Binder(String data) {
            this.mData = data;
        }

        @NonNull
        @Override
        public ViewType getViewType() {
            return DemoViewTypes.VIEW_TYPE_TWO;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder) {
            final ViewHolder typeOneHolder = (ViewHolder) holder;
            typeOneHolder.mText.setText(mData);
        }
    }

    public static class ViewHolder extends SimpleMultiTypeViewHolder {

        private final TextView mText;

        public ViewHolder(View itemView) {
            super(itemView);
            mText = (TextView) itemView.findViewById(R.id.data);
        }
    }
}

The MultiTypeCreator is responsible for inflating the view used for this view type and is the equivilant of createViewHolder(ViewGroup parent, int viewType) that you would normally override. The MultiTypeBinder is responsible for binding data to the view and is the equivalent of bindViewHolder(VH holder, int position). The ViewHolder is just a regular view holder. All you need to do is implement MultiTypeViewHolder or extend SimpleMultiTypeViewHolder

Use the MultiTypeAdapter

mAdapter = new MultiTypeAdapter.Builder()
    .addCreator(new ViewTypeOne.Creator())
    .addCreator(new ViewTypeTwo.Creator())
    .addCreator(new FooterViewType.Creator())
    .build();
    
mRecyclerView.setAdapter(mAdapter);

You need to add all of the MultiTypeCreator you want to use with the adapter

MultiTypeBinder binder = ViewTypeOne.Binder("View type one");
mAdapter.add(binder)

Add your binders to the adapter

About

A simple adapter for creating a RecyclerView with an unlimited number of view types


Languages

Language:Java 100.0%