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

标签 android android-layout kotlin

我想在 RecyclerView 中水平滚动的项目之间使用自定义分隔符。我可以将图标用作 Drawable 但是这个图标作为分隔符填充了整个空间并且看起来被拉伸(stretch)了。我制作了自定义 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 它看起来像这样(它将拉伸(stretch) drawable 以填充父级):

enter image description here

最佳答案

更新:

  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.xml 文件:

    <dimen name="dp8">8dp</dimen>
    <dimen name="dp4">4dp</dimen>
    

关于android - 是否可以对 DividerItemDecoration 使用 Layout 而不是 Drawable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55396350/

相关文章:

android - 为什么 GridLayoutManager 是从下到上,从右到左填充项目?

android - ConstraintLayout - 垂直或水平居中 View

android - 当第二个 Activity 方向改变时 registerForActivityResult 不起作用

kotlin var 在 getter 上具有不同的返回类型

java - 如何创建不需要绘图的简单游戏循环

android - 如何防止过渡到 Activity

android - 如何通过android访问远程服务器存储和检索文件

android - onTouchListener 的热点不沿 TranslationAnimation 移动

android - 将RelativeLayout可见性设置为GONE不会影响子项

generics - Kotlin 泛型继承