在 JetpackCompose 中,我们可以使用LazyColumnFor
as RecyclerView
。
在 中RecyclerView
,要在项目之间有适当的边距/填充,我们需要使用ItemDecoration
,如本文所述
像下面
class MarginItemDecoration(private val spaceHeight: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View,
parent: RecyclerView, state: RecyclerView.State) {
with(outRect) {
if (parent.getChildAdapterPosition(view) == 0) {
top = spaceHeight
}
left = spaceHeight
right = spaceHeight
bottom = spaceHeight
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于 JetpackCompose LazyColumnFor
,相当于ItemDecoration
什么?
android android-recyclerview item-decoration android-jetpack-compose android-jetpack-compose-list
我正在使用 diff utils 来更新我的回收器视图(来自视图模型的实时数据返回一个列表),我有一个项目装饰,它向列表中的最后一个元素添加了大量填充,但 diff utils 正确更新了回收器视图通过调用notifyItemInserted而不是notifyDataSetChanged,装饰仅应用于添加的最后一个元素,因此如果我将一个项目添加到我的回收器视图并调用notify dataset更改了适配器重建,那么我的所有项目最终都会有大量填充所有项目和填充仅在最后一个元素上(这是我想要的),如果我添加相同的项目两次,它会在最后添加之前添加它,例如,如果我有一个 1,2,3 的列表并且我添加在现有 3 之前添加了另外 3 个,所以 1,2,3b,3a,有没有办法对此进行更多控制?
物品装饰
public class PredictionsHorizontalSpaceCardDecoration extends RecyclerView.ItemDecoration {
private final int horizontalSpace;
private final int endSpace;
public PredictionsHorizontalSpaceCardDecoration(int horizontalSpace, int endSpace) {
this.horizontalSpace = horizontalSpace;
this.endSpace = endSpace;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
Log.d("Deco", "parent child count " + (parent.getChildCount() - 1) + " position " + parent.getChildAdapterPosition(view));
outRect.right = horizontalSpace;
outRect.top = horizontalSpace;
outRect.bottom = horizontalSpace;
outRect.left …
Run Code Online (Sandbox Code Playgroud) android android-recyclerview item-decoration android-diffutils
我正在尝试创建一个 ItemDecoration,它将通过向最后一个项目动态添加填充来保持 RecyclerView 的最小高度。
要计算填充量,我需要知道所有孩子的总身高。我通过获取单个视图的高度并将其乘以适配器中的项目数来近似计算。
我遇到的问题是 view.getHeight() 并不总是返回正确的高度。通常第一次调用 getItemOffsets() 时,高度比它应该的要小得多(例如 30px 对 300px)。即使我在布局 XML 中给子视图一个固定的高度,也会发生这种情况。我猜这与测量/布局周期有关,因为我不确定如何在 ItemDecoration 中获得正确的视图尺寸。
在 getItemOffsets() 中以编程方式获取子视图高度的正确方法是什么?
项目装饰:
public class MinHeightPaddingItemDecoration extends RecyclerView.ItemDecoration {
private static final String LOG_TAG = MinHeightPaddingItemDecoration.class.getSimpleName();
private int mMinHeight;
public MinHeightPaddingItemDecoration(int minHeight) {
super();
mMinHeight = minHeight;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state) {
int itemCount = state.getItemCount();
int lastPosition = itemCount - 1;
int itemPosition = recyclerView.getChildAdapterPosition(view);
int layoutPosition = recyclerView.getChildLayoutPosition(view);
// If this view …
Run Code Online (Sandbox Code Playgroud) 我RecyclerView
使用GridLayoutManager
具有动态列号的a呈现不同类型的项目。问题是,我有一个RecyclerView.ItemDecoration
只适用于让我们说Type A
项目。这RecyclerView.ItemDecoration
会在左侧列中的那些项目的左侧/开始处添加边距,并在右侧列中的那些项目的右侧/结尾处添加边距。它基本上是为了使项目看起来更居中并因此拉伸(这用于平板电脑/横向模式)。该RecyclerView
网格看起来是这样的:
| A | | A |
| A | | A |
| B |
| A | | A |
| A | | A |
| B |
| A | | A |
Run Code Online (Sandbox Code Playgroud)
该ItemDecoration
如下所示:
class TabletGridSpaceItemDecoration(private val space: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) = with(outRect) {
val isTypeAItemLayout = view.findViewById<ConstraintLayout>(R.id.type_a_item_container) != null …
Run Code Online (Sandbox Code Playgroud) android gridlayoutmanager android-recyclerview recyclerview-layout item-decoration
我已经知道如何画一个东西ItemDecoration
,但现在我想画View
一个ItemDecoration
.
由于设置有点复杂,我创建了一个可以重现问题的示例项目.
我有RecyclerView
20个项目,只显示数字.我想在第5项上面添加一个带有"This is Number 5"字样的黑色标题.
当然,这是我真正问题的简化版本,在我真正的问题中我必须通过ItemDecoration来做到这一点,所以请不要给出不使用ItemDecoration的替代方案.
如下面的截图所示,装饰的大小正确,android:background="@color/black"
可以绘制xml的第一层(有); 但不包括TextView
应该显示"This is Number 5" 的子视图.
FiveHeaderDecoration.kt:
class FiveHeaderDecoration: RecyclerView.ItemDecoration() {
private var header: Bitmap? = null
private val paint = Paint()
override fun getItemOffsets(outRect: Rect?, view: View?, parent: RecyclerView?, state: RecyclerView.State?) {
val params = view?.layoutParams as? RecyclerView.LayoutParams
if (params == null || parent == null) {
super.getItemOffsets(outRect, view, parent, state)
} else {
val …
Run Code Online (Sandbox Code Playgroud) 在我的活动中,我使用带有网格布局的回收器视图,我只是设置回收器视图项目装饰,它成功地将线条添加到回收器视图,但这些线条的厚度太薄且不可见,所以我的问题是如何增加这些线条的粗细和颜色的变化。
这是我正在使用的代码:
recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));
recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL));
Run Code Online (Sandbox Code Playgroud)
我的 RecyclerView 的屏幕截图: