RecyclerView 分间线设置


1、单条横线及横线左右间隔

   LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getActivity());
        linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        linearLayoutManager2.setSmoothScrollbarEnabled(true);
        linearLayoutManager2.setAutoMeasureEnabled(true);

        rvArticle.setHasFixedSize(true);
        rvArticle.setLayoutManager(linearLayoutManager2);
        int spacingleftRight = getResources().getDimensionPixelSize(R.dimen.s12);
        int spacingBottom = getResources().getDimensionPixelSize(R.dimen.s1);
        SpacesItemColorDecoration decoration2 = new SpacesItemColorDecoration(spacingleftRight, spacingBottom, getResources().getColor(R.color.colorDivider));
        rvArticle.addItemDecoration(decoration2);
        rvArticle.setNestedScrollingEnabled(false);
        mArticleAdapter = new MainArticleRecyclerAdapter(getActivity(), articleList);
        rvArticle.setAdapter(mArticleAdapter);


/**
 * Created by Administrator on 2017/10/16.
 * int leftRight = dip2px(2);
 int topBottom = dip2px(2);
 rv_content.addItemDecoration(new SpacesItemDecoration(leftRight, topBottom,getResources().getColor(R.color.colorPrimary)));
 链接:http://www.jianshu.com/p/3b860938e503
 */

public class SpacesItemColorDecoration extends RecyclerView.ItemDecoration {

    private int leftRight;
    private int topBottom;
    //color的传入方式是resouce.getcolor
    protected Drawable mDivider;

    public SpacesItemColorDecoration(int leftRight, int topBottom, int mColor) {
        this.leftRight = leftRight;
        this.topBottom = topBottom;
        if (mColor != 0) {
            mDivider = new ColorDrawable(mColor);
        }
    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
        //没有子view或者没有没有颜色直接return
        if (mDivider == null || layoutManager.getChildCount() == 0) {
            return;
        }
        int left;
        int right;
        int top;
        int bottom;
        final int childCount = parent.getChildCount();
        if (layoutManager.getOrientation() == GridLayoutManager.VERTICAL) {
            for (int i = 0; i < childCount - 1; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                //将有颜色的分割线处于中间位置
                float center = (layoutManager.getTopDecorationHeight(child) - topBottom) / 2;
                //计算下边的
                left = layoutManager.getLeftDecorationWidth(child);
                right = parent.getWidth() - layoutManager.getLeftDecorationWidth(child);
                top = (int) (child.getBottom() + params.bottomMargin + center);
                bottom = top + topBottom;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        } else {
            for (int i = 0; i < childCount - 1; i++) {
                final View child = parent.getChildAt(i);
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                //将有颜色的分割线处于中间位置
                float center = (layoutManager.getLeftDecorationWidth(child) - leftRight) / 2;
                //计算右边的
                left = (int) (child.getRight() + params.rightMargin + center);
                right = left + leftRight;
                top = layoutManager.getTopDecorationHeight(child);
                bottom = parent.getHeight() - layoutManager.getTopDecorationHeight(child);
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }



    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
        //竖直方向的
        if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {
            //最后一项需要 bottom
            if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {
                outRect.bottom = topBottom;
            }
            outRect.top = topBottom;
            outRect.left = leftRight;
            outRect.right = leftRight;
        } else {
            //最后一项需要right
            if (parent.getChildAdapterPosition(view) == layoutManager.getItemCount() - 1) {
                outRect.right = leftRight;
            }
            outRect.top = topBottom;
            outRect.left = leftRight;
            outRect.bottom = topBottom;
        }
    }


}



2、多列分割

  GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        recyclerViewTbk.setLayoutManager(gridLayoutManager);
//        recyclerViewTbk.addItemDecoration(new DividerGridItemDecoration(getActivity()));

        int spanCount = 2;//跟布局里面的spanCount属性是一致的
        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.s10);//每一个矩形的间距
        boolean includeEdge = true;//如果设置成false那边缘地带就没有间距s
        //设置每个item间距
        recyclerViewTbk.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacingInPixels, includeEdge));

        mTbkAdapter = new MallTbkAdapter(getActivity(), tbkList);
        recyclerViewTbk.setAdapter(mTbkAdapter);



public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int column = position % spanCount; // item column

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
            outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

            if (position < spanCount) { // top edge
                outRect.top = spacing;
            }
            outRect.bottom = spacing; // item bottom
        } else {
            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
            if (position >= spanCount) {
                outRect.top = spacing; // item top
            }
        }
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 RecyclerView 中去除分割线,有几种方法可以实现: 1. 使用默认分割线并将其设置为空: ```java RecyclerView recyclerView = findViewById(R.id.recyclerView); recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); ``` 在这种情况下,您可以使用以下代码将默认分隔线设置为空: ```java recyclerView.removeItemDecorationAt(0); ``` 2. 自定义分割线: 如果您想要自定义分割线的样式或者需要更多的控制,您可以创建一个自定义的 ItemDecoration 类,并在 RecyclerView 上使用它。以下是一个示例: ```java public class CustomDividerItemDecoration extends RecyclerView.ItemDecoration { private Drawable divider; public CustomDividerItemDecoration(Context context) { divider = ContextCompat.getDrawable(context, R.drawable.custom_divider); // 自定义分割线的样式 } @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 - 1; i++) { View child = parent.getChildAt(i); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); int top = child.getBottom() + params.bottomMargin; int bottom = top + divider.getIntrinsicHeight(); divider.setBounds(left, top, right, bottom); divider.draw(c); } } } ``` 然后在您的 Activity 或 Fragment 中使用它: ```java RecyclerView recyclerView = findViewById(R.id.recyclerView); recyclerView.addItemDecoration(new CustomDividerItemDecoration(this)); ``` 无论您选择哪种方法,都可以将分割线RecyclerView 中移除。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值