万能适配器(RecyclerView、ListView、GridView)
compile 'com.lqr.adapter:library:1.0.2'
LQRAdapterForAbsListView<数据类型>(上下文,数据集合,item的布局引用)
private List<String> mData = new ArrayList<>();
for (int i = 0; i < 100; i++) {
mData.add("item " + i);
}
//ListView
mLv.setAdapter(new LQRAdapterForAbsListView<String>(this, mData, R.layout.item_tv_list) {
@Override
public void convert(LQRViewHolderForAbsListView helper, String item, int position) {
helper.setText(R.id.tv, item);
}
});
//GridView
mGv = (GridView) findViewById(R.id.gv);
mGv.setAdapter(new LQRAdapterForAbsListView<String>(this, mData, R.layout.item_tv_list) {
@Override
public void convert(LQRViewHolderForAbsListView helper, String item, int position) {
helper.setText(R.id.tv, item);
}
});
LQRViewHolderForAbsListView中提供了许多常规用的控件操作,如设置文字、文字颜色、背景、显隐等,同时每个方法都是返回this,这意味着可以链式操作,方便快速开发。
LQRAdapterForRecyclerView<数据类型>(上下文,数据集合,item的布局引用)
//RecyclerView
mRv.setAdapter(new LQRAdapterForRecyclerView<String>(this, mData, R.layout.item_tv_list) {
@Override
public void convert(LQRViewHolderForRecyclerView helper, String item, int position) {
helper.setText(R.id.tv, item);
}
});
LQRViewHolderForRecyclerView中提供了许多常规用的控件操作,如设置文字、文字颜色、背景、显隐等,同时每个方法都是返回this,这意味着可以链式操作,方便快速开发。
LQRAdapterForRecyclerView<String> mAdapter = new LQRAdapterForRecyclerView(...);
//必须使用HeaderAndFooterAdapter作为RecyclerView的适配器
mRv.setAdapter(mAdapter.getHeaderAndFooterAdapter());
//添加头部
private void testAddHeaderView() {
TextView tv = new TextView(this);
...
tv.setText("heaer");
mAdapter.addHeaderView(tv);
}
//添加尾部
private void testAddFooterView() {
TextView tv = new TextView(this);
...
tv.setText("footer");
mAdapter.addFooterView(tv);
}
若项目中需要用到多种条目视图类型,则需要重新getItemViewType方法,其返回值是不同类型对应的视图布局资源id,如:
private static final int NOTIFICATION = R.layout.item_notification;
private static final int SEND_TEXT = R.layout.item_text_send;
private static final int RECEIVE_TEXT = R.layout.item_text_receive;
private static final int SEND_STICKER = R.layout.item_sticker_send;
private static final int RECEIVE_STICKER = R.layout.item_sticker_receive;
private static final int SEND_IMAGE = R.layout.item_image_send;
private static final int RECEIVE_IMAGE = R.layout.item_image_receive;
private static final int SEND_VIDEO = R.layout.item_video_send;
private static final int RECEIVE_VIDEO = R.layout.item_video_receive;
private static final int SEND_LOCATION = R.layout.item_location_send;
private static final int RECEIVE_LOCATION = R.layout.item_location_receive;
@Override
public int getItemViewType(int position) {
IMMessage msg = getData().get(position);
MsgTypeEnum msgType = msg.getMsgType();
if (msgType == MsgTypeEnum.notification) {
return NOTIFICATION;
}
if (msgType == MsgTypeEnum.text) {
if (msg.getDirect() == MsgDirectionEnum.Out) {
return SEND_TEXT;
} else {
return RECEIVE_TEXT;
}
}
if (msgType == MsgTypeEnum.custom) {
if (msg.getDirect() == MsgDirectionEnum.Out) {
return SEND_STICKER;
} else {
return RECEIVE_STICKER;
}
}
if (msgType == MsgTypeEnum.image) {
if (msg.getDirect() == MsgDirectionEnum.Out) {
return SEND_IMAGE;
} else {
return RECEIVE_IMAGE;
}
}
if (msgType == MsgTypeEnum.video) {
if (msg.getDirect() == MsgDirectionEnum.Out) {
return SEND_VIDEO;
} else {
return RECEIVE_VIDEO;
}
}
if (msgType == MsgTypeEnum.location) {
if (msg.getDirect() == MsgDirectionEnum.Out) {
return SEND_LOCATION;
} else {
return RECEIVE_LOCATION;
}
}
return super.getItemViewType(position);
}
以上是本人项目中用到的代码,以此作为参考。
建议与LQRRecyclerView一起使用,不需要考虑LayoutManager和分割线的情况,开发效率大大提高。
不管是LQRAdapterForAbsListView还是LQRAdapterForRecyclerView,都可以通过使用适配器对item进行事件监听,代码如下:
mAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(LQRViewHolder helper, ViewGroup parent, View itemView, int position) {
helper.setText(R.id.tv, "我被点击了");
}
});
mAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(LQRViewHolder helper, ViewGroup parent, View itemView, int position) {
helper.setText(R.id.tv, "我被长按了");
return false;
}
});
mAdapter.setOnItemTouchListener(new OnItemTouchListener() {
@Override
public boolean onItemTouch(LQRViewHolder helper, View childView, MotionEvent event, int position) {
helper.setText(R.id.tv, "我被触摸了");
return false;
}
});