此库已作废,请使用新库 - AnyAdapter
这是一个改进的RecyclerView框架下的适配Adapter库。使用这个库,在大多数情况下,你都不需要再写自定义的适配器Adapter类了。
通过Gradle:
compile 'com.github.boybeak:adapter:3.1.5'
compile 'com.github.boybeak:adapter-extension:2.2.6' //Optional
- 重构库结构。
- 增加了扩展库。
DelegateAdapter 这个类是该库中最为重要的一个类。使用这个适配器类,你基本就不用自定义适配器类了。
DelegateAdapter adapter = new DelegateAdapter (ActivityContext);
RecyclerView rv = ...;
//setLayoutManager etc;
rv.setAdapter(adapter);
如下方式添加数据:
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();
adapter.notifyDataSetChanged();
或者你可以在 DelegateParser 的配合下添加一组数据:
List<User> userList = ...;//make this data your self, generally from json array
mAdapter.addAll(userList, new DelegateParser<User>() {
@Override
public LayoutImpl parse(DelegateAdapter adapter, User data) {
return new UserDelegate(data); //return a LayoutImpl or its sub class
}
});
带条件的数据查询
//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)
.count();
//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"));
你所有的 ViewHolder 类都应该继承自 AbsViewHolder 类.
public class UserHolder extends AbsViewHolder<UserDelegate> {
private ImageView avatar;
private TextView nameTv, descTv;
public UserHolder(View itemView) {
super(itemView);
avatar = (ImageView)findViewById(R.id.avatar);
nameTv = (TextView)findViewById(R.id.name);
descTv = (TextView)findViewById(R.id.desc);
}
@Override
public void onBindView(Context context, UserDelegate userDelegate, int position, DelegateAdapter adapter) {
User user = userDelegate.getSource();
avatar.setImageResource(user.getAvatar());
nameTv.setText(user.getName() + " - " + getClass().getSimpleName());
descTv.setText(user.getDescription());
}
}
在这样一个ViewHolder类中,你可以进行绑定数据,绑定事件等操作。
接下来的几段代码展示一些较为便捷的操作。
/*DelegateParser*/
User[] users = ...; //Make your data yourself.
adapter.addAll (users, new DelegateParser<User>() {
@Override
public LayoutImpl parse(DelegateAdapter adapter, User data) {
return new UserDelegate(data); //return a LayoutImpl or its sub class
}
});
/*DelegateListParser*/
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;
}
});
/*DelegateFilter*/
List<LayoutImpl> subUserDelegateList = adapter.getSubList (new DelegateAdapter() {
public boolean accept (DelegateAdapter adapter, LayoutImpl impl) {
return impl != null && impl instanceof UserDelegate;
}
});
/*SimpleFilter*/
List<User> userList = adapter.getDataSourceArrayList (new SimpleFilter<User>());
DelegateAdapter 进行add, addAll, remove等操作后,返回了 DataChange 实例, 如此便可以直接调用autoNofify方法,通知UI更新.
mAdapter.add(new TextDelegate("The Beatles")).autoNotify();
mAdapter.remove(4).autoNotify();
mAdapter.addAll(Data.getTwitterList(v), new DelegateParser<Twitter>() {
@Override
public DelegateImpl parse(DelegateAdapter adapter, Twitter data) {
return new TwitterDelegate(data);
}
}).autoNotify();
mRv = (RecyclerView)findViewById(R.id.main_rv);
mAdapter = new TouchableAdapter(this);
mRv.setAdapter(mAdapter);
ItemTouchHelper helper = new ItemTouchHelper(new SimpleItemTouchHelperCallback(mAdapter,
ItemTouchHelper.UP|ItemTouchHelper.DOWN, ItemTouchHelper.END));
helper.attachToRecyclerView(mRv);