AndroidRecyclerView⽹格布局(⽀持多种分割线)详解
(2)
上篇
记录了下RecyclerView的使⽤⽅法,并且讲述了线性布局列表的使⽤⽅法,在此基础上加上了万能分割线,⽀持颜⾊分割线和图⽚分割线,同时⽀持对分割线设置线宽。
这篇是总结⼀下⽹格布局的使⽤,同样也⽀持两种分割线和线宽的设置。
主要的相关类:
1. RecyclerView.Adapter
2. GridLayoutManager ⽹格布局管理器
3. RecycleView.ItemDecoration 分割线
下⾯就直接通过⼀个例⼦来展⽰:
先上效果图:
(1) 颜⾊分割线
看起来还不错吧,根据item的数量去显⽰格⼦,当然如果你需要的样式不是三列,这个很简单,只需要在设置GridLayoutManager的时候设置相应的列数即可,即:
mManagerLayout = new GridLayoutManager(getActivity(), 3);
(2) 图⽚分割线
可能有⼈会说你的列表四周都有分割线,其实在不做特殊处理时左边和上⾯默认是没有分割线的。后⾯我会加上四周没有分割线的,其实这两种形式在实际开发中都是常见的,先来看四周都有边线的。
由于RecycleView是⾼度解耦的控件,绘制分割线只和 RecycleView.ItemDecoration 有关,所以我们只需关⼼怎么去继承RecycleView.ItemDecoration 去实现我们所需的分割线,如下:
这⾥需要说明的是:颜⾊分割线和图⽚分割线原理是完全⼀样的,图⽚分割线只是将⼀张很细的图⽚传⼊即可。
public class GridDivider extends RecyclerView.ItemDecoration {
private Drawable mDividerDarwable;
private int mDividerHight = 1;
private Paint mColorPaint;
public final int[] ATRRS = new int[]{android.R.attr.listDivider};
public GridDivider(Context context) {
final TypedArray ta = context.obtainStyledAttributes(ATRRS);
this.mDividerDarwable = ta.getDrawable(0);
}
/*
int dividerHight 分割线的线宽
int dividerColor 分割线的颜⾊
*/
public GridDivider(Context context, int dividerHight, int dividerColor) {
this(context);
the act
mDividerHight = dividerHight;
//绘制颜⾊分割线的画笔
mColorPaint = new Paint();
mColorPaint.tColor(dividerColor);
}
/*
int dividerHight 分割线的线宽
Drawable dividerDrawable 图⽚分割线
*/
public GridDivider(Context context, int dividerHight, Drawable dividerDrawable) {
this(context);
mDividerHight = dividerHight;
mDividerDarwable = dividerDrawable;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//画⽔平和垂直分割线
drawHorizontalDivider(c, parent);
drawVerticalDivider(c, parent);
}
public void drawVerticalDivider(Canvas c, RecyclerView parent) {
// 这⾥传⼊的parent是recycleview,通过它我们可以获取列表的所有的元素,
// 这⾥我们遍历列表中的每⼀个元素,对每⼀个元素绘制垂直分割线
final int childCount = ChildCount();
for (int i = 0; i < childCount; i++) {
final View child = ChildAt(i);
//获取当前item布局参数,通过它可以知道该item的精确位置,我们通过这个位置去绘制它的分割线 final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) LayoutParams(); final int top = Top() - pMargin;
final int bottom = Bottom() + params.bottomMargin;
int left = 0;
int right = 0;
//左边第⼀列,
if ((i % 3) == 0) {
//item左边分割线
left = Left();
right = left + mDividerHight;
mDividerDarwable.tBounds(left, top, right, bottom);
mDividerDarwable.draw(c);
if (mColorPaint != null) {//如果是颜⾊分割线
c.drawRect(left, top, right, bottom, mColorPaint);
}
/
/item右边分割线
left = Right() + params.rightMargin - mDividerHight;
right = left + mDividerHight;
} el {
//⾮左边第⼀列好听的英文单词
流水线英文left = Right() + params.rightMargin - mDividerHight;
right = left + mDividerHight;
}
//画分割线
mDividerDarwable.tBounds(left, top, right, bottom);
mDividerDarwable.draw(c);
if (mColorPaint != null) {
c.drawRect(left, top, right, bottom, mColorPaint);
contractors}
}
}
//....⽔平分割线与垂直分割线类似,完整代码见下。
}
下⾯是完整代码:
1. MainActivity
public class MainActivity extends AppCompatActivity {
private GridFragment mGridFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
tContentView(R.layout.activity_main);
//⽹格
mGridFragment = new GridFragment();
getFragmentManager().beginTransaction().replace(R.id.activity_main, mGridFragment).commit(); }
activity_main
西藏旅游景点<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
2. GridFragment
public class GridFragment extends Fragment implements View.OnClickListener{
private RecyclerView mRecycleViewDrawable;
private RecyclerView mRecycleViewColor;
private LinearLayoutManager mManagerColor;
private LinearLayoutManager mManagerDrawable;
翻译英文private List<String> mData;
private Button mDrawable;
private Button mColor;
private MyRecycleViewAdapter mRecycleViewAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_grid_layout, container, fal);
诀别的意思是什么mRecycleViewDrawable = (RecyclerView) view.findViewById(leview_drawable);
mRecycleViewColor = (RecyclerView) view.findViewById(leview_color);
mDrawable = (Button) view.findViewById(R.id.btn_drawable);
mDrawable.tOnClickListener(this);
mColor = (Button) view.findViewById(R.id.btn_color);
mColor.tOnClickListener(this);
//设置颜⾊分割线
mManagerColor = new GridLayoutManager(getActivity(), 3);
mRecycleViewColor.tLayoutManager(mManagerColor);
mRecycleViewColor.addItemDecoration(new GridDivider(getActivity(), 20, Resources().lorAccent))); //设置图⽚分割线
mManagerDrawable = new GridLayoutManager(getActivity(), 3);
mRecycleViewDrawable.tLayoutManager(mManagerDrawable);
Drawable drawable = Drawable(getActivity(), R.mipmap.divider);
mRecycleViewDrawable.addItemDecoration(new GridDivider(getActivity(), 20, drawable));
//初始化数据
mData = new ArrayList<String>();
initData(mData);
mRecycleViewAdapter = new MyRecycleViewAdapter(getActivity(), R.layout.item_grid_recycleview, mData);
mRecycleViewColor.tAdapter(mRecycleViewAdapter);
mRecycleViewDrawable.tAdapter(mRecycleViewAdapter);
return view;
}
private void initData(List<String> dataList) {
for (int i = 0; i < 16; i++) {
dataList.add("item" + i);
}
}
@Override
public void onClick(View view) {
int id = Id();
switch (id){
ca R.id.btn_drawable:
mRecycleViewColor.tVisibility(View.INVISIBLE);
视听说3答案截图mRecycleViewDrawable.tVisibility(View.VISIBLE);
break;
ca R.id.btn_color:
mRecycleViewColor.tVisibility(View.VISIBLE);
mRecycleViewDrawable.tVisibility(View.INVISIBLE);
break;
}
}
infre
}
3.分割线 GridDivider
直接继承 RecyclerView.ItemDecoration
public class GridDivider extends RecyclerView.ItemDecoration {
private Drawable mDividerDarwable;
private int mDividerHight = 1;
private Paint mColorPaint;
public final int[] ATRRS = new int[]{android.R.attr.listDivider};
public GridDivider(Context context) {
final TypedArray ta = context.obtainStyledAttributes(ATRRS);
this.mDividerDarwable = ta.getDrawable(0);
}
/*
int dividerHight 分割线的线宽
int dividerColor 分割线的颜⾊
*/
public GridDivider(Context context, int dividerHight, int dividerColor) {
this(context);
mDividerHight = dividerHight;
mColorPaint = new Paint();
mColorPaint.tColor(dividerColor);
}
/*
int dividerHight 分割线的线宽
Drawable dividerDrawable 图⽚分割线
*/
public GridDivider(Context context, int dividerHight, Drawable dividerDrawable) {
this(context);
mDividerHight = dividerHight;
mDividerDarwable = dividerDrawable;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//画⽔平和垂直分割线
drawHorizontalDivider(c, parent);
drawVerticalDivider(c, parent);因素英语
}
public void drawVerticalDivider(Canvas c, RecyclerView parent) {
final int childCount = ChildCount();
for (int i = 0; i < childCount; i++) {
final View child = ChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) LayoutParams(); final int top = Top() - pMargin;
final int bottom = Bottom() + params.bottomMargin;
int left = 0;
int right = 0;
//左边第⼀列
if ((i % 3) == 0) {
//item左边分割线
left = Left();
right = left + mDividerHight;
mDividerDarwable.tBounds(left, top, right, bottom);
mDividerDarwable.draw(c);
if (mColorPaint != null) {
c.drawRect(left, top, right, bottom, mColorPaint);
}
//item右边分割线
left = Right() + params.rightMargin - mDividerHight;
right = left + mDividerHight;
} el {
//⾮左边第⼀列
left = Right() + params.rightMargin - mDividerHight;
right = left + mDividerHight;
}
//画分割线
mDividerDarwable.tBounds(left, top, right, bottom);
mDividerDarwable.draw(c);
if (mColorPaint != null) {
c.drawRect(left, top, right, bottom, mColorPaint);
}