android - DividerItemDecoration 是否可以使用 Layout 而不是 Drawable?
问题描述
我想在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 项目之间的自定义分隔线):
如果我只使用分隔线图标Drawable
并将其添加到DividerItemDecoration
它看起来像这样(它将拉伸可绘制以填充父级):
解决方案
更新:
创建以下类:
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); } }
新建 HorizontalDrawableDecoration 并将其添加到您的 RecyclerView。
recyclerView.addItemDecoration(new HorizontalDrawableDecoration( this, R.drawable.arrow_black_short, R.dimen.dp4, 0, R.dimen.dp8, R.dimen.dp8));
还有一件事,将尺寸添加到 res/values 中的 dimens.xml 文件:
<dimen name="dp8">8dp</dimen> <dimen name="dp4">4dp</dimen>
推荐阅读
- node.js - 我如何在服务器端节点js上加载谷歌字体
- forms - 如何将“选择的下拉选项”发送给reciepnt,而不是在contactform7中与它相关联的相应电子邮件ID?
- ios - UIBezierPath 自定义 lineCapStyle 两端不同
- pandas - strftime 语句中的 SQLITE DISTINCT COUNT 引发错误
- c# - 为什么在特定场景中仅请求 Release 时,msbuild 会在 Debug 和 Release 中编译?
- javascript - 如何将base64字节数据转换为可下载的pdf文件?
- php - 如何在php中对多个数组进行排序
- java - 如何将包含自定义对象列表的地图字段的自定义对象转换为 json?
- sql - 在 Oracle SQL 中计算一年中的手动周数
- javascript - 为什么是 || a = 'bar' 会在 javascript 中出现引用错误?