【问题标题】:Add thickness to recycler view item decoration lines为回收站视图项目装饰线添加厚度
【发布时间】:2019-03-09 04:22:04
【问题描述】:

在我的活动中,我正在使用带有网格布局的回收站视图,我只是设置了回收站视图项目装饰,它成功地向回收站视图添加了线条,但是这些线条的厚度太薄而且不可见,所以我的问题是如何我增加了这些线条的粗细和颜色。

这是我正在使用的代码:

recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));
        recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL));

我的回收站视图的屏幕截图:

Screenshot of recyclerview

【问题讨论】:

  • 请检查我的答案...

标签: java android android-recyclerview item-decoration


【解决方案1】:

我想我找到了您问题的解决方案...您需要实现一个可绘制文件来增加装饰线的厚度...并将其添加到装饰中...为了更改颜色,在可绘制中您声明您的颜色想在装饰线条中表现出来..

解决方法如下:

在你的drawable文件夹中添加divider.mxl...

divider.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="line">
<size
    android:width="1dp"
    android:height="15dp"/>

    <solid android:color="@android:color/white"/>
</shape>

这就是你的活动。

 Drawable mDivider = ContextCompat.getDrawable(this, R.drawable.divider);
 dividerItemDecoration.setDrawable(mDivider);

【讨论】:

  • 谢谢,但您应该使用 dp 而不是 px
【解决方案2】:

这里有一个更清洁的解决方案:

首先我们可以使用 DividerItemDecoration 来设置线条。除了设置thickness,您还可以设置分隔线的color

  • GradientDrawable.setSize(width, height) 以像素为单位设置尺寸;指定高度会调整分隔线的厚度。
  • GradientDrawable 构造函数采用渐变方向和颜色整数数组;必须指定两种颜色,您可以保持它们的值相同。
DividerItemDecoration itemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{0xfff7f7f7, 0xfff7f7f7});
drawable.setSize(1,3);
itemDecoration.setDrawable(drawable);
recyclerView.addItemDecoration(itemDecoration);

【讨论】:

    【解决方案3】:

    您可以使用它来设置网格平等

    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
                }
            }
        }
    }
    
    /**
     * Converting dp to pixel
     */
    private int dpToPx(int dp) {
        Resources r = getResources();
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
    }
    

    然后,

    recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
    

    【讨论】:

    • 这不起作用,而不是用线条添加装饰它在项目之间添加了空间而不是线条
    【解决方案4】:

    试试这个java代码

    ShapeDrawable vLine = new ShapeDrawable(new Shape() {
        @Override
        public void draw(Canvas canvas, Paint paint) {
            paint.setStrokeWidth(10);
            paint.setColor(Color.BLACK);
            canvas.drawLine(0, 0, 0, 500, paint);
        }
    });
    ShapeDrawable hLine = new ShapeDrawable(new Shape() {
        @Override
        public void draw(Canvas canvas, Paint paint) {
            paint.setStrokeWidth(10);
            paint.setColor(Color.BLACK);
            canvas.drawLine(0, 0, 500, 0, paint);
        }
    });
    DividerItemDecoration vDID = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
    DividerItemDecoration hDID = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL);
    vDID.setDrawable(vLine);
    hDID.setDrawable(hLine);
    recyclerView.addItemDecoration(vDID);
    recyclerView.addItemDecoration(hDID);
    

    注意:您可以通过 XML 来完成,但上面的解决方案是一站式解决方案,即使代码看起来很大。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      相关资源
      最近更新 更多