最近在项目中多次使用了RecyclerView,其实真的使用起来还没有ListView简单,比如点击、长按事件都需要自己写,Item的间隔什么的都要自己写,用ListView根本就不用管的好嘛。
但是呢,和它的优点比起来,这些缺点根本就不是事:
- 高度可定制性,以轻松的通过这个控件实现ListView,GirdView,瀑布流等效果,同时还可以增加Item增删动画,使用ItemDecoration给item添加自定义的间隔等等
- 性能优,个人测试当数据量较大的时候,ListView有明显卡顿而RecyclerView没有
以下是个人在项目中使用时遇到的一些心得:
1.Item之间的间隔
RecyclerView默认没有间隔,不像ListView有一条线。使用时自己添加:
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
Log.v("recyclerview - itemdecoration", "onDraw()");
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
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, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
然后给RecyclerView设置ItemDecoration即可
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,
DividerItemDecoration.VERTICAL_LIST));
2.Item点击效果
RecyclerView默认没有点击效果,ListView点击Item背景会变暗,这样可以给用户很清晰的感知,RecyclerView点了看不出来效果,不知道有没有反应也不清楚点击了哪一个,所以需要添加以明确的反馈,在item的布局文件里面添加:
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!