RecyclerView新增虛線分割線
阿新 • • 發佈:2019-01-02
先看下效果圖吧
有些同學覺得這不是很簡單嗎,隨便百度一大堆(好吧你贏了,我只是來做筆記的。。。)
簡單粗暴上關鍵程式碼
mAdapter.setData(data);
mGrideLayout.setAdapter(mAdapter);
mGrideLayout.setLayoutManager(new
GridLayoutManager(this,
2));
mGrideLayout.addItemDecoration(new
DividerGridItemDecoration(this));
關鍵這個DividerGridItemDecoration類
//繪製水平虛線 mPaint = newPaint(); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(Color.GRAY); Path path = new Path(); path.moveTo(left, top); path.lineTo(right,top); PathEffect effects = new DashPathEffect(new float[]{8,8,8,8},0); mPaint.setPathEffect(effects); c.drawPath(path, mPaint);
//繪製垂直虛線 mPaint = new Paint(); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(Color.GRAY); Path path = new Path(); path.moveTo(left, top); path.lineTo(left,bottom); PathEffect effects = new DashPathEffect(new float[]{8,8,8,8},0); mPaint.setPathEffect(effects); c.drawPath(path, mPaint);
好了就這樣了?不,我想說我碰到的一些坑,分享給各位同學
問題一:
What?右邊怎麼多出來了一條線,我想說這個類DividerGridItemDecoration
好吧上程式碼:
修改前;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_gride" android:paddingBottom="200dp" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
修改後:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_gride" android:paddingBottom="200dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
應該是在父佈局的邊界才隱藏,在來看看美團的,也是這個細節問題(截圖不明顯)
問題二:
我現在recyclerview中設定了paddingbottom,然後就。。。估計這就是paint的神奇之處吧
參考:http://blog.csdn.net/lmj623565791/article/details/45059587
http://www.jianshu.com/p/fe41428ca2f3