好久没回归我原生大Android开发啦!甚是怀念啊!终于,公司最近来了一个原生项目,可以好好爽一把了!实不相瞒,我现在写列表还是用的ListView - -! 想想以前用的那些东西,是该换换了!早就想用RecyclerView了,一直没机会!哎~~~网络框架就看了一下OkGo。然后!!大换血!!!
这回闲下来了,好好学一学RecyclerView的基本使用了,先能实现个普通列表开发就行! 哈哈~~~
使用RecyclerView的好处,想必你们这些大佬早就一清二楚了!我也查了相关帖子,也就不赘述了!
初始化
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
recyclerView.addItemDecoration(new ItemDecoration(this, R.drawable.divider_line, 1));
adapter = new MyAdapter(this);
adapter.openLoadAnimation();
// adapter.isFirstOnly(false);
recyclerView.setAdapter(adapter);
这里的adapter,我用了一个库,叫BRVAH,GitHub上一搜就能找到,start也很高~~ 从此,妈妈再也不用担心我写adapter了!
public class MyAdapter extends BaseQuickAdapter<ClassListBean.DataBean, BaseViewHolder> {
private Context context;
public MyAdapter(Context context) {
super(R.layout.item);
this.context = context;
}
@Override
protected void convert(BaseViewHolder helper, ClassListBean.DataBean item) {
// helper.setGone(R.id.tv_mirror, helper.getLayoutPosition() - getHeaderLayoutCount() == 0);
Picasso.with(context).load(item.getPicBig()).into((ImageView) helper.getView(R.id.iv_avatar));
helper.setText(R.id.tv_title, item.getName());
helper.setText(R.id.tv_desc, item.getDescription());
helper.setText(R.id.tv_num, "当前学员数:" + item.getLearner());
helper.addOnClickListener(R.id.iv_avatar).addOnClickListener(R.id.tv_title);
}
}
用了这个库,写个adapter,简直不要太粗暴!几行就搞定!!而且这个库还有更高级的用法,大佬们自行去看吧!
用了RecyclerView,唯独的一点弱地方,就是分割线和item的点击事件了!
先说点击事件,上面这个库已经完美解决了!
//普通点击事件
adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
showToast(position + "");
}
});
//子view点击事件
adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
ClassListBean.DataBean b = (ClassListBean.DataBean) adapter.getItem(position);
switch (view.getId()) {
case R.id.iv_avatar:
showToast(b.getPicBig());
break;
case R.id.tv_title:
showToast(b.getName());
break;
}
}
});
再说分割线!话不多说,放类!
/**
* RecyclerView分割线(适用于纵向列表和网格布局,横向列表忽略)
*/
public class ItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
private int style;
/**
* resId自定义,style 1:垂直列表分割线 2:网格布局分割线
*/
public ItemDecoration(Context context, int resId, int style) {
mDivider = context.getResources().getDrawable(resId);
this.style = style;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (style == 1) {
drawListVertical(c, parent);
}
if (style == 2) {
drawHorizontal(c, parent);
drawGridVertical(c, parent);
}
}
private int getSpanCount(RecyclerView parent) {
int spanCount = -1;// 列数
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
}
return spanCount;
}
public void drawListVertical(Canvas c, RecyclerView parent) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
// if (i == childCount - 1) {//最后一项底部是否设置线,显示这一块代码就不设置
// return;
// }
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = parent.getChildAt(i).getTop() - mDivider.getIntrinsicHeight();
int bottom = parent.getChildAt(i).getBottom();
int left = child.getRight() + params.leftMargin;
int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawGridVertical(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int left = parent.getChildAt(i).getLeft() + params.leftMargin;
int right = parent.getChildAt(i).getRight() + params.rightMargin;
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
if ((pos + 1) % spanCount == 0) {// 如果是最后一列,则不需要绘制右边
return true;
}
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
if ((pos + 1) % spanCount == 0) {// 如果是最后一列,则不需要绘制右边
return true;
}
} else {
childCount = childCount - childCount % spanCount;
if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
return true;
}
}
return false;
}
private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
childCount = childCount - childCount % spanCount;
if (pos >= childCount)// 如果是最后一行,则不需要绘制底部
return true;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
// StaggeredGridLayoutManager 且纵向滚动
if (orientation == StaggeredGridLayoutManager.VERTICAL) {
childCount = childCount - childCount % spanCount;
// 如果是最后一行,则不需要绘制底部
if (pos >= childCount)
return true;
} else {
// StaggeredGridLayoutManager 且横向滚动
// 如果是最后一行,则不需要绘制底部
if ((pos + 1) % spanCount == 0) {
return true;
}
}
}
return false;
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
int spanCount = getSpanCount(parent);
int childCount = parent.getAdapter().getItemCount();
if (isLastRaw(parent, itemPosition, spanCount, childCount)) {// 如果是最后一行,则不需要绘制底部
outRect.set(0, 0, mDivider.getIntrinsicHeight(), 0);
} else if (isLastColum(parent, itemPosition, spanCount, childCount)) {// 如果是最后一列,则不需要绘制右边
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicHeight(), mDivider.getIntrinsicHeight());
}
}
}
在初始化的地方,我已经设置了分割线,分割线的样式,可以自己定义:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ccc"/>
<size android:height="1px"/>
</shape>
OK!完事!睡觉!!!