Android RecycleView(二)——添加分割线

     上一篇 Android RecycleView(一)完成了最最基本的recycleview的用法 这一篇,完成其分割线的设置。html

recycleview 有其本身设置分割线的方法:recyclerView.addItemDecoration() 参数是咱们本身定义的一个ItemDecoration经过自定义itemDecorationandroid

咱们能够根据本身的喜爱来自定义分割线的样式,不管是color仍是图片,或者自定义的shape。canvas

自定义itemDecoration:app

继承系统提供的RecyclerView.ItemDecoration重写onDraw或onDrawOver方法   一个实在childitem以前绘制一个是在childitem以后绘制。ide

package com.chs.myrecycleview.adapter;

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

import com.chs.myrecycleview.R;

/**
 * 做者:chs on 2016/2/2 15:20
 * 邮箱:657083984@qq.com
 */
public class RecycleViewDivider extends RecyclerView.ItemDecoration{
    private Drawable drawable;
    public RecycleViewDivider(Context context) {
        drawable = context.getResources().getDrawable(R.mipmap.list_divider);
////        drawable = context.getResources().getDrawable(R.drawable.shape);
    }
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//        drawVertical(c, parent);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        drawVertical(c, parent);
    }

    private void drawVertical(Canvas canvas, RecyclerView parent) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth()-parent.getPaddingRight();
        View child;
        RecyclerView.LayoutParams layoutParams;
        int top;
        int bottom;
        int count = parent.getChildCount();
        for(int i = 0;i<count;i++){
            child = parent.getChildAt(i);
            layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            top = child.getBottom()+layoutParams.bottomMargin;
            bottom = top+drawable.getIntrinsicHeight();
            drawable.setBounds(left,top,right,bottom);
            drawable.draw(canvas);
        }

    }
//若是等于分割线的高度或宽度的话能够不重写次方法
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (parent.getChildPosition(view) == parent.getChildCount() - 1) {
            outRect.set(0, 0, 0, 0);
        } else {
            outRect.set(0, 0, 0, drawable.getIntrinsicHeight());
        }
    }
}


</pre><p></p><pre>
效果图  一个是用的图片,一个是用的自定义的一个shape

这样就实现它的分割线了 而且咱们能够随意的绘制。spa

这种方法比较简单,可是若是咱们把上述代码放到编译器中,你会发现构造方法中的.net

context.getResources().getDrawable(R.mipmap.list_divider); 这个方法是被废弃的一个方法。最然如今能够用,可是毕竟有风险,反正我不喜欢看到一个横杠在代码上画着。
后来发现咱们能够经过改变系统属性来实现  系统属性 listDivider
 
<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:listDivider">@drawable/shape</item>
    </style>
 

构造方法中改成:3d

public RecycleViewDivider(Context context) {
//        drawable = context.getResources().getDrawable(R.mipmap.list_divider);
//        drawable = context.getResources().getDrawable(R.drawable.shape);
        TypedArray array = context.obtainStyledAttributes(new int[]{android.R.attr.listDivider});
        drawable = array.getDrawable(0);
        array.recycle();
    }


效果:code


跟上面的效果同样并且没有被废弃的方法,仍是比较完美哈哈
htm

OK分割线已经搞定  可是咱们还会发现一个让人郁闷的地方,它没有提供给咱们 OnclickListener和OnLongClickListener方法

一种简单的方法咱们能够去adapter中本身实现 ,下一篇在写吧~~