DividerItemDecoration是否可以使用Layout而不是Drawable?

时间:2019-03-28 11:22:02

标签: android android-layout kotlin

我想在RecyclerView中水平滚动的项目之间使用自定义分隔符。我可以将图标用作Drawable,但是此图标将整个空间填充为分隔符并显得很拉长。我制作了自定义RelativeLayout,在其中将分隔符居中放置为ImageView,并在其中添加了自定义填充。我不想将此分隔符(箭头)添加到Layout的垂直中心,但略低于中心。这是带有自定义填充的自定义图标。

有人说我想以某种方式将此分隔符添加到DividerItemDecoration中,但它仅接受Drawable

更新:

我尝试膨胀此自定义布局并将其转换为Drawable。 问题在于,展开后的布局的宽度和高度均为0。

val view = LayoutInflater.from(app).inflate(R.layout.separator, null)
val bitmapDecoration = view?.let { viewToBitmap(it, it.width, it.height) }
val drawableDecoration = BitmapDrawable(app.resources, bitmapDecoration)
itemDecoration.setDrawable(drawableDecoration)

我的分隔符示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="40dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:paddingTop="4dp"
        android:src="@drawable/arrow_black_short"/>

</RelativeLayout>

图片(recyclerview项之间的自定义分隔线):

enter image description here

如果我仅将分隔符图标用作Drawable并将其添加到DividerItemDecoration,则它看起来像这样(它将拉伸可绘制对象以填充父对象):

enter image description here

2 个答案:

答案 0 :(得分:0)

您需要创建一个绘制分隔项装饰的类,而不是使用视图或布局。 This article详细介绍了您为什么要走这条路线,并提供了一个示例。

从api 25开始,您也可以使用它。

var decoration: DividerItemDecoration =  DividerItemDecoration(getApplicationContext(), VERTICAL)
recyclerview.addItemDecoration(decoration)

我希望这可以帮助您正确地了解要完成的工作。

答案 1 :(得分:0)

已更新:

  1. 创建以下类:

    public class HorizontalDrawableDecoration extends RecyclerView.ItemDecoration {
        private final int paddingTopPx, paddingBottomPx, paddingLeftPx, paddingRightPx;
        private final Drawable drawable;
        private Rect bounds;
    
        public HorizontalDrawableDecoration(Context context,
                                        @DrawableRes int drawableId,
                                        @DimenRes int paddingTop, @DimenRes int paddingBottom,
                                        @DimenRes int paddingLeft, @DimenRes int paddingRight ) {
            this.paddingTopPx = getDimenPx(context, paddingTop);
            this.paddingBottomPx = getDimenPx(context, paddingBottom);
            this.paddingLeftPx = getDimenPx(context, paddingLeft);
            this.paddingRightPx = getDimenPx(context, paddingRight);
            this.drawable = ContextCompat.getDrawable(context, drawableId);
            bounds = new Rect();
        }
    
        @Override
        public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.onDraw(canvas, parent, state);
            int top = Math.round((parent.getHeight() + paddingTopPx + paddingBottomPx - drawable.getIntrinsicHeight()) / 2f);
            int bottom = top + drawable.getIntrinsicHeight();
            int childCount = parent.getChildCount();
            for (int i=0; i< childCount; i++) {
                View child = parent.getChildAt(i);
                parent.getDecoratedBoundsWithMargins(child, bounds);
                final int right = bounds.right + Math.round(child.getTranslationX()) - paddingRightPx;
                final int left = right - drawable.getIntrinsicWidth();
                drawable.setBounds(left, top, right, bottom );
                drawable.draw(canvas);
            }
        }
    
        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            if (parent.getChildAdapterPosition(view) == state.getItemCount() - 1) {
                outRect.setEmpty();
            } else {
                outRect.set(0,0, drawable.getIntrinsicWidth() + paddingLeftPx + paddingRightPx, 0);
            }
        }
    
        private int getDimenPx(Context context, @DimenRes int dimenId) {
            return (context == null || dimenId == 0)? 0: context.getResources().getDimensionPixelSize(dimenId);
        }
    }
    
  2. 新建Horizo​​ntalDrawableDecoration并将其添加到您的RecyclerView。

    recyclerView.addItemDecoration(new HorizontalDrawableDecoration(
            this, R.drawable.arrow_black_short, R.dimen.dp4, 0, R.dimen.dp8, R.dimen.dp8));
    
  3. 还有一件事情,以res / values将dimens添加到dimens.xml文件中:

    <dimen name="dp8">8dp</dimen>
    <dimen name="dp4">4dp</dimen>
    
相关问题