Skip to content

4. Advanced Usage

boybeak edited this page Sep 13, 2017 · 3 revisions

There are many magic functions in DelegateAdapter. Here are some most important functions for you.

Add

Add a data collection or array.

mAdapter.addAll(users, new DelegateParser<User>() {
            @Override
            public LayoutImpl parse(DelegateAdapter adapter, User data) {
                return new UserDelegate(data);
            }
        }).autoNotify();

the users parameter can be a collection or an array.

Convert one object to collection and add them

mAdapter.add("DelegateAdapter", new DelegateListParser<String>() {
            @Override
            public List<LayoutImpl> parse(DelegateAdapter adapter, String data) {
                List<LayoutImpl> delegateList = new ArrayList<LayoutImpl>();
                for (int i = 0; i < data.length(); i++) {
                    delegateList.add(new CharDelegate(data.charAt(i)));
                }
                return delegateList;
            }
        }).autoNotify();

Call autoNotify method after adding data, can perform RecyclerView's animation

Query

DelegateAdapter has a very powerful query function. All these powerful functions are provided by my another library Selector. You can query data like a database. Query all UserDelegate items where user's name length greater than 4:

List<UserDelegate> userDelegates = mAdapter.selector(UserDelegate.class).where(
                Path.with(UserDelegate.class, Integer.class).methodWith("getSource").fieldWith("name").methodWith("length"),
                Operator.OPERATOR_GT,
                4
        ).findAll();

Extract all users where it's name length greater than 4:

List<User> users = mAdapter.selector(UserDelegate.class).where(
                Path.with(UserDelegate.class, Integer.class).methodWith("getSource").fieldWith("name").methodWith("length"),
                Operator.OPERATOR_GT,
                4
        ).extractAll(
                Path.with(UserDelegate.class, User.class).methodWith("getSource")
        );

Map item where user's name length greater than 4:

mAdapter.selector(UserDelegate.class).where(
                Path.with(UserDelegate.class, Integer.class).methodWith("getSource").fieldWith("name").methodWith("length"),
                Operator.OPERATOR_GT,
                4
        ).map(new Action<UserDelegate>() {
            @Override
            public void action(int i, UserDelegate userDelegate) {
                //TODO action this
            }
        });

Remove items where user's name length greater than 4:

mAdapter.selector(UserDelegate.class).where(
                Path.with(UserDelegate.class, Integer.class).methodWith("getSource").fieldWith("name").methodWith("length"),
                Operator.OPERATOR_GT,
                4
        ).remove();
        mAdapter.notifyDataSetChanged();

Clone this wiki locally