最近一直在看RecyclerView主巍,較之ListView它確實(shí)是靈活多變您市,給予開發(fā)者更多自定義的空間,比如:需要添加頭部和尾部、item的點(diǎn)擊事件段化、自定義的LayoutManager嘁捷,還有就是下面要說的自定義的分割線。
1显熏、如何理解分割線
經(jīng)常聽到有人說自定義分割線麻煩雄嚣,為什么不把分割線寫到item布局里,這樣不是更簡(jiǎn)單嗎喘蟆?有些情況把分割線寫到item布局里是很難達(dá)到我們想要的效果现诀,例如RecyclerView里的GridLayoutManager,StaggeredGridLayoutManager和一些自定義的LayoutManager,不同位置的item需要畫的分割線并不相同履肃,這時(shí)候應(yīng)用自定義的分割線就能很好的解決這個(gè)問題仔沿。
2、如何畫分割線
網(wǎng)上也有很多關(guān)于RecyclerView自定義分割線的寫法尺棋,很多都是通過獲取系統(tǒng)屬性中的listDivider來添加封锉,在系統(tǒng)中的AppTheme中設(shè)置,但是如果我有兩種風(fēng)格的分割線膘螟,這就尷尬了呀成福,所以我希望像ListView一樣能傳入一個(gè)drawable來設(shè)置分割線,所以我們的思路就是最終能像下面這樣設(shè)置分割線:
rvStore.addItemDecoration(new CustomDecoration(context,CustomDecoration.VERTICAL_LIST,R.drawable.divider_love,UnitHelper.dip2px(this,15)))
3荆残、具體代碼實(shí)現(xiàn)
由于RecyclerView的布局方式多種多樣奴艾,所以它的分割線也根據(jù)布局的不同有所差異,本文只針對(duì)LinearLayoutManager線性布局
- 繼承自RecyclerView.ItemDecoration
- 重寫getItemOffsets()内斯、 onDraw()方法
現(xiàn)在給出完整的類蕴潦,代碼中關(guān)鍵地方都有注釋,就不再一一說明:
public class CustomDecoration extends RecyclerView.ItemDecoration {
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
/**
* 分割線縮進(jìn)值
*/
private int inset;
private Paint paint;
/**
* @param context
* @param orientation layout的方向
* @param drawable 引入的drawable的ID
* @param inset 分割線縮進(jìn)值
*/
public CustomDecoration(Context context, int orientation, int drawable, int inset) {
mDivider = context.getResources().getDrawable(drawable);
this.inset = inset;
paint = new Paint();
paint.setColor(context.getResources().getColor(R.color.white));
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
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) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
private void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
//最后一個(gè)item不畫分割線
for (int i = 0; i < childCount - 1; 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();
if (inset > 0) {
c.drawRect(left, top, right, bottom, paint);
mDivider.setBounds(left + inset, top, right - inset, bottom);
} else {
mDivider.setBounds(left, top, right, bottom);
}
mDivider.draw(c);
}
}
private 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 - 1; 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);
}
}
//由于Divider也有寬高俘闯,每一個(gè)Item需要向下或者向右偏移
@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);
}
}
}
4潭苞、具體怎么用
- RecyclerView的三部曲
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new CustomDecoration(this, CustomDecoration.VERTICAL_LIST, R.drawable.divider_love, UnitHelper.dip2px(this, 15)));
recyclerView.setAdapter(adapter);
- R.drawable.divider_love
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#CB8589"/>
<size android:height="15dp"/>
</shape>
對(duì)應(yīng)的效果如下:
我們可以看到明顯的縮進(jìn)效果,設(shè)置成零就沒有縮進(jìn)了真朗。
- 還是看看正常使用中是什么樣子吧
對(duì)應(yīng)的 R.drawable.divider_love
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#CD3131"/>
<size android:height="1dp"/>
</shape>
我們只需要修改下CustomDecoration中paint的顏色就可以讓縮進(jìn)的顏色和背景色一致了此疹,默認(rèn)是白色。
paint.setColor(Color.parseColor("#ECF0F1"));
- That's all遮婶。