引言:记录自己项目中使用的库,非技术文章。
一、直接上代码
适配器
/**
* @param layoutId 布局Id
* @param ReceiveUser 实体类 、可以是任意类型
* @param BaseViewHolder 适配器内部holder
*/
class SimpleAdapter(@LayoutRes layoutId: Int) :
BaseQuickAdapter<ReceiveUser, BaseViewHolder>(layoutId) {
//只需要实现convert方法
override fun convert(holder: BaseViewHolder, item: ReceiveUser) {
//itemView
val itemRootView = holder.itemView
//获取view方法
val tvXxx = holder.getView<TextView>(0)
//支持设置text及img背景图片
//以下id均为测试 id=实际中的R.id.xxxx
val viewId = 0x11212
holder.setText(viewId, "设置的内容")
val imgResId = 0x1231
holder.setImageResource(viewId, imgResId)
}
}
使用
fun test(){
val adapter=SimpleAdapter(0)
//内部已完成notifyDataChange
//添加数据等方法、
val list= mutableListOf<ReceiveUser>()
adapter.addData(list)
//移除数据办法
adapter.removeAt(0)
}
二、补充说明
BaseViewHolder 为内部holder 由第三方维护
三、相关第三库信息、及注意事项;
github:GitHub - CymChad/BaseRecyclerViewAdapterHelper: BRVAH:Powerful and flexible RecyclerAdapter
普通且不复杂列表,可用此,复杂列表建议原生;
四 额外记录Grid类型 间距设置辅助类
自定义分割线类
/**
* 设置RecyclerView GridLayoutManager or StaggeredGridLayoutManager spacing
*/
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
}
使用
ConvertUtils.dp2px() 第三方、工具类...
includeEdge 解释:是否包含边缘、即上下左右是否填充间距。
over........................