RecyclerView 控制分割线样式

在使用RecyclerView的过程中,一开始按照鸿洋的博客
Android RecyclerView 使用完全解析 体验艺术般的控件
中介绍的那样在style.xml中使用

<style> <item android:listDivider>@drawable/divider_bg</item> </style>

一开始使用的这样觉得挺好的,但是后来另外一个页面也需要使用RecyclerView,这个item的高度和上一次使用的又是不一样的,这次我就发蒙了,style.xml文件中只能定义一次这可怎么办。后来我又想了一下,我只能修改 鸿洋给的那个DividerItemDecoration 了
在下面给出的代码中,setDivider(int)是我添加的内容,不再使用Android中的listDivider了。

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    // private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private Drawable mDivider;
    private Context mContext;
    private int mOrientation;

    public DividerItemDecoration(Context context, int orientation)
    {
        // final TypedArray a = context.obtainStyledAttributes(ATTRS);
        // mDivider = a.getDrawable(0);
        // a.recycle();
        mContext = context;
        setOrientation(orientation);
    }

    **/** * 设置分割线的显示样式 * * @param resId drawable资源,可以使自定义的shape文件 */
    public void setDivider(int resId)
    {
        mDivider = mContext.getResources().getDrawable(resId);
    }**

    public void setOrientation(int orientation)
    {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST)
        {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent)
    {
        if (mOrientation == VERTICAL_LIST)
        {
            drawVertical(c, parent);
        }
        else
        {
            drawHorizontal(c, parent);
        }

    }


    public void drawVertical(Canvas c, RecyclerView parent)
    {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++)
        {
            final View child = parent.getChildAt(i);
            android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawHorizontal(Canvas c, RecyclerView parent)
    {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++)
        {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent)
    {
        if (mOrientation == VERTICAL_LIST)
        {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        }
        else
        {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }
}

使用方法:

        recycler_view.setAdapter(mMineNewsAdapter);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context,
                                                                                              DividerItemDecoration.VERTICAL_LIST);
        dividerItemDecoration.setDivider(R.drawable.news_divider_line_bg);
        recycler_view.addItemDecoration(dividerItemDecoration);
        recycler_view.setItemAnimator(new DefaultItemAnimator());

你可能感兴趣的:(android,控件)