RecyclerView(二)—— ItemDecoration

Recycler沒有直接提供設置item間距的功能,而是提供了一個更強大的基類ItemDecoration。類如其名,這個類是Item的裝飾。它既能夠做爲Item的間距,也能夠在item之間繪製分隔線,甚至能夠對每一個item的邊緣都進行不一樣的繪製。android

ItemDecoration自己是一個虛類,咱們在使用時,只能繼承它。ide

  先看一個簡單版本,這個版本的ItemDecoration只提供一個Item的間距。ui

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

/**
 * Created by fishboneLsy on 2016/6/25.
 */
public class DivideDecoration extends RecyclerView.ItemDecoration{

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (parent.getChildAdapterPosition(view) < parent.getAdapter().getItemCount() - 1){
            outRect.bottom = 30;
        }
    }
}

  上面的例子中,咱們將每一個item下面,留出30像素的間隔,而且增長一個判斷語句,即最後一個item的下方不會加間隔。從上面的例子觸類旁通,咱們能夠爲不一樣的item,加上不一樣的空格。spa

 

----------------------------------------------------------------------------------------------------------------------code

  除了設置間隔外,咱們還能夠繪製分隔線:blog

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by fishboneLsy on 2016/6/25.
 */
public class DrawerDecoration extends RecyclerView.ItemDecoration {


    Drawable mDivider;

    public DrawerDecoration(Context context){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mDivider = context.getResources().getDrawable(R.mipmap.ic_launcher ,null);
        }else {
            mDivider = context.getResources().getDrawable(R.mipmap.ic_launcher );
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.bottom = mDivider.getIntrinsicHeight();
    }
}

  由上面的例子,咱們能夠看到,分隔線的繪製實質是在一個循環中完成的。也就是說,咱們能夠爲每一個item繪製不一樣的分隔線。咱們能夠本身在Canvas上繪製,也能夠直接將一個Drawable繪製在Canvas上,上面的代碼,就是將一個Drawable直接繪製在Canvas上。繼承

  在這個例子中,咱們的outRect.bottom就必須獲取Drawable的實質高度了。這樣的一個類,爲咱們提供了極大的分隔線訂製的靈活性。 也許打造了一個體驗良好的列表控件,提供了不少方便。ip

 

Done~get

importandroid.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
* Created by fishboneLsy on 2016/6/25.
*/
public class DrawerDecoration extends RecyclerView.ItemDecoration {


Drawable mDivider;

public DrawerDecoration(Context context){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mDivider = context.getResources().getDrawable(R.mipmap.ic_launcher ,null);
}else {
mDivider = context.getResources().getDrawable(R.mipmap.ic_launcher );
}
}

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();

int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);

RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();

mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.bottom = mDivider.getIntrinsicHeight(); }}
相關文章
相關標籤/搜索