DelegateAdapter DelegateAdapter-Extension


This is an advanced RecyclerView's Adapter library. With this library, writing custom adapter class is not necessary in most conditions. Using DelegateAdapter can satisfy most of what you need.

The most amazing thing is binding multi types data and ViewHolder with Injections.





Grab via Gradle:

compile 'com.github.boybeak:adapter:3.1.5'
compile 'com.github.boybeak:adapter-extension:2.2.6' //Optional

What's new in version 3.x.x

  1. Reconstruct the library.
  2. Add extension library.

You can read a full release note.


Typical Usage

Advanced Usage


The most important class is DelegateAdapter. With this adapter class, there's no need to make a your own custom adapter class.

DelegateAdapter adapter = new DelegateAdapter (ActivityContext);
RecyclerView rv = ...;
//setLayoutManager etc;

Add data

adapter add data like below:

UserLayoutImpl userLayoutImpl = ...;
UserDelegateImpl userDelegateImple = ...;

User user = ...;
UserDelegate userDelegate = new UserDelegate (user);
UserAnnotationDeleagate annoDelegate = new UserAnnotationDelegate (user);
//create these User data or decode by gson from json
adapter.add (userLayoutImpl);
adapter.add (userDelegateImple);
adapter.add (userDelegate);
adapter.add (annoDelegate);
//Don't forget notifyDataSetChanged();

or you can add User model collection directly with a DelegateParser.

List<User> userList = ...;//make this data your self, generally from json array
mAdapter.addAll(userList, new DelegateParser<User>() {
    public LayoutImpl parse(DelegateAdapter adapter, User data) {
        return new UserDelegate(data); //return a LayoutImpl or its sub class

Query Data

Query data with conditions

//query count
int count = adapter.selector(UserDelegate.class)
                .where(Path.with(UserDelegate.class, Integer.class).methodWith("getSource").methodWith("getName").methodWith("length"), Operator.OPERATOR_GT, 4)

//query names of users
List<String> names = adapter.selector(UserDelegate.class)
                .where(Path.with(UserDelegate.class, Integer.class).methodWith("getSource").methodWith("getName").methodWith("length"), Operator.OPERATOR_GT, 4)
                .extractAll(Path.with(UserDelegate.class, String.class).methodWith("getSource").methodWith("getName"));


all your ViewHolder classes should extend AbsViewHolder class.

public class UserHolder extends AbsViewHolder<UserDelegate> {

    private ImageView avatar;
    private TextView nameTv, descTv;

    public UserHolder(View itemView) {
        avatar = (ImageView)findViewById(;
        nameTv = (TextView)findViewById(;
        descTv = (TextView)findViewById(;

    public void onBindView(Context context, UserDelegate userDelegate, int position, DelegateAdapter adapter) {
        User user = userDelegate.getSource();
        nameTv.setText(user.getName() + " - " + getClass().getSimpleName());

In this ViewHolder class you can bind data, bind event etc.

DelegateParser, DelegateListParser

A serious of code snippets show you usage.

User[] users = ...; //Make your data yourself.
adapter.addAll (users, new DelegateParser<User>() {
    public LayoutImpl parse(DelegateAdapter adapter, User data) {
        return new UserDelegate(data); //return a LayoutImpl or its sub class
User[] users = ...; //Make your data yourself.
adapter.addAll (users, new DelegateListParser () {
    public List<LayoutImpl> parse (DelegateAdapter adapter, User data) {
        List<LayoutImpl> list = new ArrayList<LayoutImpl>();
        list.add (new UserHeaderDelegate("I'm a good teacher"));
        list.add (new UserDelegate (data));
        if (data.isGood()) {
            list.add(new UserFooterDelegate ("I am really a good teacher!"));
        return list;


DelegateAdapter returns a DataChange instance after add, addAll and remove actions. You can call autoNotify directly.

mAdapter.add(new TextDelegate("The Beatles")).autoNotify();
mAdapter.addAll(Data.getTwitterList(v), new DelegateParser<Twitter>() {
                    public DelegateImpl parse(DelegateAdapter adapter, Twitter data) {
                        return new TwitterDelegate(data);

Item swap and delete

mRv = (RecyclerView)findViewById(;

mAdapter = new TouchableAdapter(this);

ItemTouchHelper helper = new ItemTouchHelper(new SimpleItemTouchHelperCallback(mAdapter,
                ItemTouchHelper.UP|ItemTouchHelper.DOWN, ItemTouchHelper.END));