欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年03月23日 10:23:52   作者:砂礫han  
本篇文章主要介紹了android中RecyclerView自定義分割線實(shí)現(xiàn),由于RecyclerView的布局方式多種多樣,所以它的分割線也根據(jù)布局的不同有所差異,本文只針對(duì)LinearLayoutManager線性布局。

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

1、如何理解分割線

經(jīng)常聽到有人說自定義分割線麻煩,為什么不把分割線寫到item布局里,這樣不是更簡單嗎?有些情況把分割線寫到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)的效果如下:

我們可以看到明顯的縮進(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"));

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Flutter學(xué)習(xí)之實(shí)現(xiàn)自定義themes詳解

    Flutter學(xué)習(xí)之實(shí)現(xiàn)自定義themes詳解

    一般情況下我們?cè)趂lutter中搭建的app基本上都是用的是MaterialApp這種設(shè)計(jì)模式,MaterialApp中為我們接下來使用的按鈕,菜單等提供了統(tǒng)一的樣式,那么這種樣式能不能進(jìn)行修改或者自定義呢?答案是肯定的,一起來看看吧
    2023-03-03
  • Android OnFocuChangeListener焦點(diǎn)事件詳解

    Android OnFocuChangeListener焦點(diǎn)事件詳解

    這篇文章主要為大家詳細(xì)介紹了Android OnFocuChangeListener焦點(diǎn)事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Android仿微信通訊錄列表側(cè)邊欄效果

    Android仿微信通訊錄列表側(cè)邊欄效果

    這篇文章主要為大家詳細(xì)介紹了Android仿微信通訊錄列表側(cè)邊欄效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android編程開發(fā)之Spinner組件用法

    Android編程開發(fā)之Spinner組件用法

    這篇文章主要介紹了Android編程開發(fā)之Spinner組件用法,結(jié)合實(shí)例形式分析介紹了Android中Spinner組件的功能、定義及具體使用技巧,需要的朋友可以參考下
    2015-12-12
  • Android 解析XML 文件的四種方法總結(jié)

    Android 解析XML 文件的四種方法總結(jié)

    本文將詳細(xì)介紹用解析XML的四種方法,XML現(xiàn)在已經(jīng)成為一種通用的數(shù)據(jù)交換格式,平臺(tái)的無關(guān)性使得很多場合都需要用到XML,這里對(duì)幾種解析XML 的方法做詳解
    2016-07-07
  • Android從圖片獲取二維碼的方法

    Android從圖片獲取二維碼的方法

    這篇文章主要為大家詳細(xì)介紹了Android從圖片獲取二維碼的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • ListView的View回收引起的checkbox狀態(tài)改變監(jiān)聽等問題解決方案

    ListView的View回收引起的checkbox狀態(tài)改變監(jiān)聽等問題解決方案

    之前講到了自定義Adapter傳遞給ListView時(shí),因?yàn)長istView的View回收,需要注意當(dāng)ListView列表項(xiàng)中包含有帶有狀態(tài)標(biāo)識(shí)控件的問題,感興趣的朋友可以祥看本文,或許會(huì)有意外的收獲哦
    2013-01-01
  • Android自定義Dialog原理實(shí)例解析

    Android自定義Dialog原理實(shí)例解析

    這篇文章主要介紹了Android自定義Dialog原理實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Android CardView+ViewPager實(shí)現(xiàn)ViewPager翻頁動(dòng)畫的方法

    Android CardView+ViewPager實(shí)現(xiàn)ViewPager翻頁動(dòng)畫的方法

    本篇文章主要介紹了Android CardView+ViewPager實(shí)現(xiàn)ViewPager翻頁動(dòng)畫的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • Android 快速使用正則表達(dá)式,校驗(yàn)身份證號(hào)的實(shí)例

    Android 快速使用正則表達(dá)式,校驗(yàn)身份證號(hào)的實(shí)例

    下面小編就為大家分享一篇Android 快速使用正則表達(dá)式,校驗(yàn)身份證號(hào)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01

最新評(píng)論