【Android進階】RecyclerView之ItemDecoration(一)

前言

RecyclerView 對你們來講,基本使用應該已經比較熟悉,這裏就直接跳過,說說ItemDecoration,也算是溫故而知新。java

什麼是ItemDecoration

從字面意思來講,就是裝飾,裝飾RecyclerView 。這個怎麼理解?舉幾個例子就明白了。 好比說,RecyclerView 若是要加間隔線應該如何作? 這裏就要用到ItemDecorationandroid

DividerItemDecoration mDivider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
 rvDemo.addItemDecoration(mDivider);
複製代碼

DividerItemDecoration 繼承於ItemDecorationgit

public class DividerItemDecoration extends ItemDecoration {}
複製代碼

如何寫出比較炫酷的效果?

這裏就涉及到自定義ItemDecorationgithub

自定義 ItemDecoration,主要涉及到3個方法canvas

1.getItemOffsets

public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {}
複製代碼

其中,outRect爲該item在上下左右撐開的距離,默認爲 0viewitemparentRecyclerView自己;stateRecyclerView狀態,也能夠經過其在各組件之間傳遞參數,這裏暫時涉及不到緩存

舉個例子app

<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,這裏使用的是 BaseRecyclerViewAdapterHelperide

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);
    }
}
複製代碼

而後組裝數據,添加ItemDecorationpost

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#getItemOffsetsui

@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);
      }
複製代碼

image.png

能夠看到,每一個item都會執行getItemOffsets方法,因此,咱們能夠用這個,爲RecyclerView添加間隔線。 好比設置bottom爲1,這裏就不演示了,有興趣的同窗能夠本身試試。

2.onDraw

public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {}
複製代碼

其中,cgetItemOffsets所撐開區域的畫布,能夠在區域內任意繪圖,區域外的話,會被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);
}
複製代碼

效果圖以下

image.png

能夠看到,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);
                }
            }
}
複製代碼

效果圖以下

image.png

結合這2個方法,咱們是否是能夠作個分組效果?先看效果圖

image.png

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) {}
複製代碼

onDrawOveronDraw相似,也只會執行一次,而且它是最後繪製的,能夠繪製到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);
            }

        }
複製代碼

效果圖以下

image.png

這個方法又能夠作什麼效果呢?先看效果圖

image.png

能夠看到,列表的最上邊有一個漸隱效果。 完整代碼以下

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與閃動文字效果

完整源碼 PicRvDemo

你的承認,是我堅持更新博客的動力,若是以爲有用,就請點個贊,謝謝

相關文章
相關標籤/搜索