可隨意定製的RecyclerView 介面卡
1.支援多型別
3.支援新增狀態如:獲取資料失敗、網路沒有連線等(可控制隱藏與顯示)
關於載入更多
由於載入更多的實現方法很多,這裡並沒有引入這個功能,例子中也給了一個來自度孃的簡單實現,有需要可以參考一下
include lib
Gradle: compile 'com.yan.adapter:adapter:+'複製程式碼
how to use
adapter = initAdapter(dataList);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
recyclerView.setAdapter(adapter);複製程式碼
1.新增普通的資料型別
.addAdapterItem(new CustomAdapterItem<HolderTest1, Integer>() {
@Override
public Class dataType() {
return Integer.class;
}
@Override
public HolderTest1 viewHolder(ViewGroup parent) {
return new HolderTest1(
LayoutInflater.from(MainActivity.this).inflate(R.layout.item_type_1, parent, false)
);
}
@Override
public void bindData(HolderTest1 holder, Integer item, int position) {
holder.textView.setText(item + "");
}
})複製程式碼
2.新增狀態型別
.addAdapterItem(new StateAdapterItem<HolderTest2>("NO_WIFI", false) {
@Override
public HolderTest2 viewHolder(ViewGroup parent) {
HolderTest2 holderTest2 = new HolderTest2(
LayoutInflater.from(MainActivity.this).inflate(R.layout.state_view, parent, false)
);
holderTest2.textView.setText("確保網路連線正常");
return holderTest2;
}
})
.addAdapterItem(new StateAdapterItem<HolderTest2>("DataError", false,
getHolder()) {
@Override
public HolderTest2 viewHolder(ViewGroup parent) {
return null;
}
})
private HolderTest2 getHolder() {
RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
RecyclerView.LayoutParams.MATCH_PARENT);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.state_view, null);
view.setLayoutParams(layoutParams);
HolderTest2 holderTest2 = new HolderTest2(view);
holderTest2.textView.setText("獲取資料出錯");
return holderTest2;
}
複製程式碼
.addAdapterItem(new StateAdapterItem<HolderTest2>(StateAdapterItem.HEADER)
.addAdapterItem(new StateAdapterItem<HolderTest2>(StateAdapterItem.FOOTER)複製程式碼
4.動態更改資料
StateAdapterItem dataError = adapter.findStateItem("DataError");
((HolderTest2) dataError.getHolder()).textView.append("!");
((HolderTest2) adapter.getStateFooter().getHolder())
.textView.setText("footer" + times);
StateAdapterItem noWifi = adapter.findStateItem("NO_WIFI");
if (noWifi.getHolder() != null)
((HolderTest2) noWifi.getHolder()).textView.append(".");
StateAdapterItem noData = adapter.findStateItem("NO_DATA");
if (noData.getHolder() != null)
((HolderTest2) noData.getHolder()).textView.append("-");複製程式碼
5.控制顯示與隱藏
adapter.findStateItem("FIXED").hide();
adapter.show("DataError")
.hide("NO_WIFI")
.hide("NO_DATA")
.hideHeader(false)
.hideFooter(false)
.commit();複製程式碼
6.點選事件
adapter.setOnItemClickListener(...
adapter.setOnDataItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(RecyclerView.ViewHolder holder, int position) {
((HolderTest) holder).textView.append(position + "");
toast.setText("onItemClick: position " + position);
toast.show();
}
});複製程式碼