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

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

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

2019-12-12 03:18:18
字體:
供稿:網(wǎng)友

最近一直在看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ù)布局的不同有所差異,本文只針對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>

對應(yīng)的效果如下:

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

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

對應(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阜南县| 西华县| 广丰县| 衡南县| 华安县| 石嘴山市| 儋州市| 乌鲁木齐县| 兴隆县| 神农架林区| 定陶县| 通渭县| 普陀区| 宿松县| 华宁县| 新化县| 鹰潭市| 蕲春县| 屯留县| 宁南县| 嘉义县| 崇阳县| 娄底市| 蕲春县| 上杭县| 定安县| 黄山市| 三明市| 黔南| 吉安县| 光山县| 墨竹工卡县| 驻马店市| 娱乐| 翁源县| 五峰| 兰西县| 富川| 太白县| 溆浦县| 六盘水市|