文章目录
1 RecyclerView 的简单使用
1.1 首先在android studio中添加依赖
compile 'com.android.support:recyclerview-v7:24.1.1'
1.2 在xml布局中引入控件
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_divider"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
1.3 自定义adapter
先继承RecyclerView.Adapter,并传入自定义的ViewHolder.并实现其抽象方法。
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder>
自定义ViewHolder,和以前ListView的Adapter类似。但是已经给集成好了,需要自己定义ViewHolder。然后通过泛型传给Adapter.
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(android.R.id.text1);
}
}
抽象方法01 构造MyViewHolder
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder viewHolder = new MyViewHolder(View.inflate(parent.getContext(),android.R.layout.simple_list_item_1,null));
return viewHolder;
}
抽象方法02 和以前ListView的Adapter的getView方法类似。RecyclerView不能给item添加点击事件,所以这里只能自己添加点击回调。
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.textView.setText(stringList.get(position));
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(position);
}
});
}
抽象方法03 返回item的个数
@Override
public int getItemCount() {
return stringList.size();
}
点击事件的回调
public interface OnItemClickListener {
void onItemClick(int position);
}
1.4 初始化recyclerView,给recyclerView设置布局管理器,设置adapter
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.rv_divider);
//1 模拟假数据
for (int i = 0; i < 15; i++) {
stringList.add("item" + i);
}
//2 给recyclerView设置布局管理器
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
//3 设置adapter
recyclerView.setAdapter(new MyRecyclerViewAdapter(stringList, new MyRecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Toast.makeText(DividerActivty.this, "点到我了!" + position, Toast.LENGTH_LONG).show();
}
}));
}
此时RecyclerView已经能显示出来。
上面这段代码的布局管理器就是给RecyclerView设置item的排列样式。比如设置成
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
通过上面这一行RecyclerView就会以GridView的样式显示。同样也可以直接以线性排列的样式显示。线性排列默认的是垂直的。
recyclerView.setLayoutManager(new LinearLayoutManager(this));
也可以设置成水平排列 true/false 是否从后往前显示
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true));
效果图
2 给RecyclerView设置分割线
1 自定义继承自RecyclerView.ItemDecoration的实现类。
GridViewDecoration extends RecyclerView.ItemDecoration
2 在构造方法中初始化Drawable对象 并把Drawable的属性赋值
private Drawable mDivider;
系统的分割线属性
private int[] attrs = new int[]{android.R.attr.listDivider};
初始化mDIvider
public GridViewDecoration(Context context) {
TypedArray typedArray = context.obtainStyledAttributes(attrs);
mDivider = typedArray.getDrawable(0);
typedArray.recycle();
}
3 实现抽象方法。
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
drawHorizontalLine(c, parent);
drawVerticalLine(c, parent);
}
在onDraw()方法中分别绘制水平和垂直方向的分割线。
//绘制水平间隔线
private void drawHorizontalLine(Canvas canvas, RecyclerView parent) {
int childCount = parent.getChildCount();
Log.i("Divider","childCount="+childCount);
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int left = child.getLeft() - params.leftMargin;
int top = child.getBottom() + params.bottomMargin;
int right = child.getRight() + params.rightMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
}
//绘制垂直间隔线
private void drawVerticalLine(Canvas canvas, 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 = child.getRight() + params.rightMargin;
int right = left + mDivider.getIntrinsicWidth();
int top = child.getTop() - params.topMargin;
int bottom = child.getBottom() + params.bottomMargin;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
}
这两断代码做法是一样的。首先是根据RecyclerView通过获取item,然后获取item的坐标,再根据item的坐标来计算出分割线的坐标。根据分割线的坐标来绘制分割线。
4 给RecyclerView设置分割线
private GridViewDecoration decor;
decor = new GridViewDecoration(this);
recyclerView.addItemDecoration(decor);
效果图
3 设置RecyclerView分割线的颜色和宽度
1 自定义方形的drawable xml文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="2dp"
android:height="2dp"></size>
<solid android:color="#F00"></solid>
</shape>
2 在主题样式中添加属性 引入上面的drawable
<item name="android:listDivider">@drawable/item_divider</item>
效果图