萬能RecyclerView分割線
更新時間:2017年01月05日 15:39:21 作者:無心下棋
這篇文章主要介紹了萬能RecyclerView分割線的相關(guān)代碼,告訴大家如何準(zhǔn)確調(diào)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
就不多敘述了,直接上代碼
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
/*
* RecyclerView的布局方向,默認(rèn)先賦值
* 為縱向布局
* RecyclerView 布局可橫向,也可縱向
* 橫向和縱向?qū)?yīng)的分割想畫法不一樣
* */
private int mOrientation = LinearLayoutManager.VERTICAL;
/**
* item之間分割線的size,1---5
*/
private int mSize;
/**
* 繪制item分割線的畫筆,和設(shè)置其屬性
* 來繪制個性分割線
*/
private Paint mPaint;
/**
* 構(gòu)造方法傳入布局方向,不可不傳
*
* @param context context
* @param orientation 布局方向
* @param color 顏色
* @param mItemSize item之間分割線的size
*/
public DividerItemDecoration(Context context, int orientation, int color, int mItemSize) {
this.mOrientation = orientation;
/*
item之間分割線的顏色
*/
this.mSize= mItemSize;
if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
throw new IllegalArgumentException("LinearLayoutManager error");
}
mSize = (int) TypedValue.applyDimension(mItemSize, TypedValue.COMPLEX_UNIT_DIP, context.getResources().getDisplayMetrics());
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(color);
/*設(shè)置填充*/
mPaint.setStyle(Paint.Style.FILL);
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == LinearLayoutManager.VERTICAL) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
/**
* 繪制縱向 item 分割線
*
* @param canvas canvas
* @param parent parent
*/
private void drawVertical(Canvas canvas, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + layoutParams.bottomMargin;
final int bottom = top + mSize;
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
/**
* 繪制橫向 item 分割線
*
* @param canvas canvas
* @param parent parent
*/
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight() + layoutParams.rightMargin;
final int right = left + mSize;
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
/**
* 設(shè)置item分割線的size
*
* @param outRect outRect
* @param view view
* @param parent parent
* @param state state
*/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == LinearLayoutManager.VERTICAL) {
outRect.set(0, 0, 0, mSize);
} else {
outRect.set(0, 0, mSize, 0);
}
}
}
調(diào)用的時候這樣寫:
復(fù)制代碼 代碼如下:
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL, Color.RED,5));
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android?Recyclerview實(shí)現(xiàn)左滑刪除功能
這篇文章主要為大家詳細(xì)介紹了Android?Recyclerview實(shí)現(xiàn)左滑刪除功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Android中ImageView實(shí)現(xiàn)選擇本地圖片并顯示功能
本文主要介紹了android中ImageView實(shí)現(xiàn)選擇本地圖片并顯示功能的示例代碼。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04
Android實(shí)現(xiàn)帶指示點(diǎn)的自動輪播無限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶指示點(diǎn)的自動輪播無限循環(huán)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android基于OkHttpUtils網(wǎng)絡(luò)請求的二次封裝
這篇文章主要介紹了Android基于OkHttpUtils網(wǎng)絡(luò)請求的二次封裝,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Android學(xué)習(xí)筆記45之gson解析json
JSON即JavaScript Object Natation,是一種輕量級的數(shù)據(jù)交換格式,采用完全獨(dú)立于語言的文本格式,為Web開發(fā)提供了一種理想的數(shù)據(jù)交換格式。通過本篇文章給大家介紹Android學(xué)習(xí)筆記45之gson解析json的相關(guān)內(nèi)容,對android gson解析json相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2015-12-12
Android使用OKHttp包處理HTTP相關(guān)操作的基本用法講解
這篇文章主要介紹了Android使用OKHttp包處理HTTP相關(guān)操作的基本用法講解,包括操作如何利用OKHttp操作HTTP請求與處理緩存等內(nèi)容,需要的朋友可以參考下2016-07-07
Android開發(fā)之BottomSheetDialog組件的使用
BottomSheetDialog是底部操作控件,可在屏幕底部創(chuàng)建一個支持滑動關(guān)閉視圖。本文將通過示例詳細(xì)講解它的使用,感興趣的小伙伴可以了解一下2023-01-01

