RecyclerView操作数据及添加分割线

操作数据

官方RecyclerView.Adapter中提供基本添加、删除和修改数据的方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
//该方法用于当增加一个数据的时候,position表示新增数据显示的位置
final void notifyItemInserted(int position)

//该方法用于删除一个数据的时候,position表示数据删除的位置
final void notifyItemRemoved(int position)

//该方法表示所在position对应的item位置不会改变,但是该item内容发生变化
final void notifyItemChanged(int position)

//该方法一般用于:适配器之前装载的数据大部分已经过时,需要重新更新数据
//调用该方法的时候,RecyclerView会重新计算子Item及所有子Item重新布局
//出于效率考虑,官方建议用更加精确的方法来取代该方法
final void notifyDataSetChanged()

首先对MyAdapter.java修改,新增方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//移除数据
public void removeData(int position) {
mDataSet.remove(position);
notifyItemRemoved(position);
}
//新增数据
public void addData(int position) {
mDataSet.add(position, "Add One");
notifyItemInserted(position);
}
//更改某个位置的数据
public void changeData(int position) {
mDataSet.set(position, "Item has changed " + count++);
notifyItemChanged(position);
}

每一个Item变化的时候都不是瞬间的,都会有一个动画效果,使得用户体验很好,其实这里面使用了RecyclerView默认提供的动画效果:

1
2
//非必要,RecyclerView会默认使用
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

RecyclerView.ItemAnimator抽象类用于控制Item的动画效果,而DefaultItemAnimator()正是其默认实现类。我们可以自定义实现很多特效动画,至于它的各种动画效果,可以参考GitHub上的开源项目RecyclerViewItemAnimators:https://github.com/gabrielemariotti/RecyclerViewItemAnimators。

添加分割线

RecycleView没有像ListView可以直接在xml中添加android:divider属性,可能是考虑到RecycleView灵活多变的特点,不过可以手动添加。RecyclerView.ItemDecoration抽象类即用于添加分割线:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static abstract class ItemDecoration {

public void onDraw(Canvas c, RecyclerView parent, State state) {
onDraw(c, parent);
}

public void onDrawOver(Canvas c, RecyclerView parent, State state) {
onDrawOver(c, parent);
}

public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(), parent);
}
}

省略了三个官方不提倡的方法,这里简单说明一下以上各个方法的作用:

  • onDraw和onDrawOver:这两个方法用于绘制,绘制分割线的逻辑可以放在这里面,它们二者的具体区别是:onDraw是在Item View绘制之前调用,而onDrawOver是在Item View绘制之后调用,因此我们一般选择重写其中一个方法即可。
  • getItemOffsets:这个方法是告诉RecyclerView在绘制完一个Item View的时候,应该留下多少空位,以便于绘制分割线。

我们来写一个实现类,新建DividerItemDecoration(注:该类参考自Android官方):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

//使用系统自带的listDivider
private static final int[] ATTRS = new int[] {
android.R.attr.listDivider
};

public static final int ORIENTATION_HORIZONTAL = LinearLayoutManager.HORIZONTAL;
public static final int ORIENTATION_VERTICAL = LinearLayoutManager.VERTICAL;

private Drawable mDivider;
private int mOrientation;

public DividerItemDecoration(Context context,int orientation) {
//使用TypeArray加载该系统资源
final TypedArray ta = context.obtainStyledAttributes(ATTRS);
mDivider = ta.getDrawable(0);
//缓存
ta.recycle();
setOrientation(orientation);
}

public void setOrientation(int orientation) {
if (orientation != ORIENTATION_HORIZONTAL && orientation != ORIENTATION_VERTICAL) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == ORIENTATION_VERTICAL) {
drawVertical(c, parent);
} else {
drawHorizontal(c,parent);
}
}

public void drawVertical(Canvas c, RecyclerView parent) {
//获取分割线的左边距,即RecyclerView的padding值
final int left = parent.getPaddingLeft();
//分割线右边距
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
//遍历所有item view,为它们的下方绘制分割线
for (int i=0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}

public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();

final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == ORIENTATION_VERTICAL) {
//设置偏移的高度是mDivider.getIntrinsicHeight,该高度正是分割线的高度
outRect.set(0,0,0,mDivider.getIntrinsicHeight());
} else {
outRect.set(0,0,mDivider.getIntrinsicWidth(), 0);
}
}
}

接着在MainActivity.java添加如下代码:

1
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.ORIENTATION_VERTICAL));

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :