国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

萬能RecyclerView分割線

2019-12-12 04:07:10
字體:
供稿:網(wǎng)友

就不多敘述了,直接上代碼

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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 定结县| 金溪县| 定兴县| 宿松县| 亳州市| 米脂县| 玛纳斯县| 富宁县| 永新县| 东山县| 习水县| 旬邑县| 买车| 逊克县| 沁水县| 长沙市| 裕民县| 彝良县| 临武县| 江津市| 耒阳市| 青州市| 南汇区| 汝南县| 梁河县| 涟水县| 奉新县| 柏乡县| 西林县| 玛纳斯县| 民勤县| 夏津县| 兰州市| 宽城| 北票市| 余姚市| 江源县| 浪卡子县| 邯郸市| 岳阳县| 高清|