android中RecyclerView自定義分割線實(shí)現(xiàn)

最近一直在看RecyclerView,較之ListView它確實(shí)是靈活多變,給予開發(fā)者更多自定義的空間,比如:需要添加頭部和尾部、item的點(diǎn)擊事件、自定義的LayoutManager,還有就是下面要說的自定義的分割線。

站在用戶的角度思考問題,與客戶深入溝通,找到利辛網(wǎng)站設(shè)計(jì)與利辛網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋利辛地區(qū)。

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è)置分割線:

復(fù)制代碼 代碼如下:

rvStore.addItemDecoration(new CustomDecoration(context,CustomDecoration.VERTICAL_LIST,R.drawable.divider_love,UnitHelper.dip2px(this,15)))

3、具體代碼實(shí)現(xiàn)

由于RecyclerView的布局方式多種多樣,所以它的分割線也根據(jù)布局的不同有所差異,本文只針對(duì)LinearLayoutManager線性布局

  1. 繼承自RecyclerView.ItemDecoration
  2. 重寫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

<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)的效果如下:

android中RecyclerView自定義分割線實(shí)現(xiàn)

我們可以看到明顯的縮進(jìn)效果,設(shè)置成零就沒有縮進(jìn)了。

還是看看正常使用中是什么樣子吧

android中RecyclerView自定義分割線實(shí)現(xià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"));

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前文章:android中RecyclerView自定義分割線實(shí)現(xiàn)
當(dāng)前網(wǎng)址:http://muchs.cn/article40/picdeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站排名、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站收錄、關(guān)鍵詞優(yōu)化、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)