前言
RecyclerView
對大家來說,基本使用應該已經比較熟悉,這裡就直接跳過,說說ItemDecoration
,也算是溫故而知新。
- 【Android進階】RecyclerView之ItemDecoration(一)
- 【Android進階】RecyclerView之快取(二)
- 【Android進階】RecyclerView之繪製流程(三)
什麼是ItemDecoration
從字面意思來說,就是裝飾,裝飾RecyclerView
。這個怎麼理解?舉幾個例子就明白了。
比如說,RecyclerView
如果要加間隔線應該如何做?
這裡就要用到ItemDecoration
DividerItemDecoration mDivider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
rvDemo.addItemDecoration(mDivider);
複製程式碼
DividerItemDecoration
繼承於ItemDecoration
public class DividerItemDecoration extends ItemDecoration {}
複製程式碼
如何寫出比較炫酷的效果?
這裡就涉及到自定義ItemDecoration
自定義 ItemDecoration
,主要涉及到3個方法
- 1.
getItemOffsets
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
@NonNull RecyclerView parent, @NonNull RecyclerView.State state) {}
複製程式碼
其中,outRect
為該item
在上下左右撐開的距離,預設為 0;view
為item
;parent
為RecyclerView
本身;state
為RecyclerView
狀態,也可以通過其在各元件之間傳遞引數,這裡暫時涉及不到。
舉個例子
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
tools:context=".deco.DecoActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_demo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
複製程式碼
定義一個DemoAdapter
,這裡使用的是 BaseRecyclerViewAdapterHelper
public class DemoAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
public DemoAdapter(@Nullable List<String> data) {
super(R.layout.item_demo, data);
}
@Override
protected void convert(BaseViewHolder helper, String item) {
helper.setText(R.id.tv_demo, item);
}
}
複製程式碼
然後組裝資料,新增ItemDecoration
List<String> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add("第" + i + "個item");
}
DemoAdapter demoAdapter = new DemoAdapter(list);
rvDemo.setLayoutManager(new LinearLayoutManager(this));
rvDemo.addItemDecoration(new DemoDecoration());
rvDemo.setAdapter(demoAdapter);
複製程式碼
DemoDecoration#getItemOffsets
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//outRect就是表示在item的上下左右所撐開的距離,預設值為0
outRect.set(10, 20, 30, 40);
}
複製程式碼
可以看到,每個item都會執行getItemOffsets
方法,所以,我們可以用這個,為RecyclerView
新增間隔線。
比如設定bottom為1,這裡就不演示了,有興趣的同學可以自己試試。
- 2.
onDraw
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView
parent, @NonNull RecyclerView.State state) {}
複製程式碼
其中,c
為getItemOffsets
所撐開區域的畫布,可以在區域內任意繪圖,區域外的話,會被item所遮擋。
舉個例子,
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView
parent, @NonNull RecyclerView.State state) {
super.onDraw(c, parent, state);
//getItemOffsets撐開的空白區域所對應的畫布,可以在getItemOffsets所撐出來的區域任意繪圖。
c.drawCircle(50, 50, 30, mPaint);
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//outRect就是表示在item的上下左右所撐開的距離,預設值為0
outRect.set(100, 0, 0, 5);
}
複製程式碼
效果圖如下
可以看到,onDraw
方法只會執行一次,如果要每個item
前都繪製一個圓,怎麼寫?
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int itemCount = parent.getChildCount();
for (int i = 0; i < itemCount; i++) {
View child = parent.getChildAt(i);
int cx = child.getWidth() / 2;
int cy = child.getTop() + child.getHeight() / 2;
c.drawCircle(cx, cy, 30, mPaint);
}
}
}
複製程式碼
效果圖如下
結合這2個方法,我們是不是可以做個分組效果?先看效果圖
public DemoDecoration() {
mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setTextSize(DensityUtils.dp2px(DecoActivity.this, 16));
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//outRect就是表示在item的上下左右所撐開的距離,預設值為0
int index = parent.getChildAdapterPosition(view);
if (index % 3 == 0) {
outRect.set(0, 100, 0, 5);
} else {
outRect.set(0, 0, 0, 5);
}
}
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView
parent, @NonNull RecyclerView.State state) {
super.onDraw(c, parent, state);
//getItemOffsets撐開的空白區域所對應的畫布,可以在getItemOffsets所撐出來的區域任意繪圖。
int itemCount = parent.getChildCount();
for (int i = 0; i < itemCount; i++) {
View child = parent.getChildAt(i);
int cx = child.getWidth() / 2 - 50;
int cy = child.getTop() - 50 + DensityUtils.dp2px(DecoActivity.this, 16) / 2;
int index = parent.getChildAdapterPosition(child);
if (index % 3 == 0) {
String text = "第" + index / 3 + "組";
c.drawText(text, 0, text.length(), cx, cy, mPaint);
}
}
}
複製程式碼
當然,分組的標題位置算的不太準備,這裡只是展示下效果。比較理想的效果,可以參考【Android檢視效果】分組列表實現吸頂效果
- 3.
onDrawOver
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {}
複製程式碼
onDrawOver
和onDraw
類似,也只會執行一次,並且它是最後繪製的,可以繪製到item
上面。
@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDrawOver(c, parent, state);
int itemCount = parent.getChildCount();
for (int i = 0; i < itemCount; i++) {
View child = parent.getChildAt(i);
int cx = child.getWidth() / 2;
int cy = child.getTop() + child.getHeight()/2 ;
c.drawCircle(cx, cy, 30, mPaint);
}
}
複製程式碼
效果圖如下
這個方法又可以做什麼效果呢?先看效果圖
可以看到,列表的最上邊有一個漸隱效果。 完整程式碼如下
public class TransDecoration extends RecyclerView.ItemDecoration {
private Paint mPaint;
private Xfermode xfermode;
private LinearGradient linearGradient;
private int layerId;
public TransDecoration() {
mPaint = new Paint();
xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
linearGradient = new LinearGradient(0.0f, 0.0f, 0.0f, 100.0f, new int[]{0, Color.BLACK}, null, Shader.TileMode.CLAMP);
}
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDraw(c, parent, state);
// 此處 Paint的引數這裡傳的null, 在傳入 mPaint 時會出現第一次開啟黑屏閃現的問題
// 注意 saveLayer 不能省也不能移動到onDrawOver方法裡
layerId = c.saveLayer(0.0f, 0.0f, (float) parent.getWidth(), (float) parent.getHeight(), null, Canvas.ALL_SAVE_FLAG);
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
}
@Override
public void onDrawOver(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDrawOver(canvas, parent, state);
mPaint.setXfermode(xfermode);
mPaint.setShader(linearGradient);
canvas.drawRect(0.0f, 0.0f, parent.getRight(), 200.0f, mPaint);
mPaint.setXfermode(null);
canvas.restoreToCount(layerId);
}
}
複製程式碼
其中,PorterDuffXfermode為影象的混合模式,詳情可參考各個擊破搞明白PorterDuff.Mode LinearGradient為漸變效果,詳情可參考自定義控制元件三部曲之繪圖篇(十九)——LinearGradient與閃動文字效果