SimpleRecycler
GitHub傳送門:SimpleRecycle on Github
Demo apk下載:apk dowload
step:
步驟1.將JitPack存儲(chǔ)庫添加到構(gòu)建文件中
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
步驟2.添加依賴項(xiàng)
dependencies {
implementation 'com.github.xiaoqinghong:SimpleRecycler:last-version'
}
SimpleAdapter(只支持一種itemView)
public class TestAdapter extends SimpleRecyclerAdapter<String> {
public TestAdapter(List<String> list) {
super(R.layout.item_test, list);
}
@Override
protected void bind(SimpleViewHolder helper, String item) {
helper.setText(R.id.tv_test_item, item)
.addClickListener(R.id.tv_test_item) // 設(shè)置子項(xiàng)的點(diǎn)擊事件監(jiān)聽
.addLongClickListener(R.id.tv_test_item) // 設(shè)置子項(xiàng)的長按事件監(jiān)聽
.getView(R.id.tv_test_item);
}
}
MultipleAdapter(支持多種itemView)
/**
* bean類需要實(shí)現(xiàn)SimpleMultipleAdapter.TypeBind接口闲询。每個(gè)bean類都可能屬于不同的type
*/
public class MultipleBean implements SimpleMultipleAdapter.TypeBind {
@Override
public int type() {
return 0;
}
}
/**
* MultipleTestAdapter
*/
public class MultipleTestAdapter extends SimpleMultipleAdapter<MultipleBean> {
public MultipleTestAdapter(List<MultipleBean> list) {
super(list);
// 添加多種item對(duì)應(yīng)的layout年鸳。
addLayout(R.layout.multiple_item_a, 0);
addLayout(R.layout.multiple_item_b, 1);
addLayout(R.layout.multiple_item_c, 2);
}
@Override
protected void multipleBind(SimpleViewHolder helper, MultipleBean item, int itemType) {
switch (itemType) {
case 0:
// do something...
break;
case 1:
// do something...
break;
case 2:
// do something...
break;
default:
// do something...
break;
}
}
}
adapter暴露的接口
mAdapter.bindRecyclerView(recyclerView); // 內(nèi)部默認(rèn)使用LinearLayoutManager
mAdapter.addHeaderView(header);
mAdapter.addFooterView(footer);
// click
mAdapter.setOnItemClickListener(new SimpleRecyclerAdapter.OnItemClickListener() {
@Override
public void onClick(View v, int position) {
}
});
mAdapter.setOnSubViewClickListener(new SimpleRecyclerAdapter.OnSubViewClickListener() {
@Override
public void onClick(View v, int position) {
}
});
// long click
mAdapter.setOnItemLongClickListener(new SimpleRecyclerAdapter.OnItemLongClickListener() {
@Override
public void onLongClick(View v, int position) {
}
});
mAdapter.setOnSubViewLongClickListener(new SimpleRecyclerAdapter.OnSubViewLongClickListener() {
@Override
public void onLongClick(View v, int position) {
}
});
最后
方法不止一種,這樣地操作方式古瓤,僅僅這只是我的一種思考塘揣。