gpt4 book ai didi

Android RecyclerView addItemDecoration 不会给 RecyclerView 底部添加装饰

转载 作者:太空狗 更新时间:2023-10-29 15:59:18 27 4
gpt4 key购买 nike

我在网上找不到这个问题的答案,所以答案一定很简单,没人愿​​意问,我根本不知道。

基本上,我想在我的 recyclerView 中的每个项目和 RecyclerView 的底部之间添加一条水平线。我找到了一种在 RecyclerView 中的每个项目之间放置分隔线的方法,但不是在末尾

根据文档(可在此处找到:https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#addItemDecoration(android.support.v7.widget.RecyclerView.ItemDecoration,int))

将负整数放入 addItemDecoration(RecyclerView.ItemDecoration 装饰, 指数)。应该解决这个问题,说明:

If this value is negative the decoration will be added at the end.

这是我的自定义 RecyclerView 的构造方法:

public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setHasFixedSize(true);
setLayoutManager(new LinearLayoutManager(context));
decor = new HorizontalDividerItemDecoration.Builder(context).build();
addItemDecoration(decor, -1);
}

如您所见,我在 addItemDecoration() 中使用了一个负整数,但我无法弄清楚为什么它没有按预期方式工作。

如有任何帮助,我们将不胜感激!

最佳答案

根据 Janice Kartika 的建议,我将代码复制并粘贴到我的项目中。在我复制并粘贴 Janice 的代码后,Android Studio 给了我这个建议:

Replace with android.support.v7.widge.DividerItemDecoration?

Older versions of the RycyclerView library did not include a divider decorator, but one was provided as a sample in the support demos. This divider class has been widely copy/pasted into various projects.

In recent versions of the support library, the divider decorator is now included, so you can replace custom copies with the "built-in" version, android.support.v7.widget.DividerItemDecoration

所以不要像这样使用 Horizo​​ntalDividerItemDecoration(它是我们导入的外部库的一部分):

decor = new HorizontalDividerItemDecoration.Builder(context).build();
addItemDecoration(decor, -1);

我像这样使用默认的“DividerItemDecoration”:

decor = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
addItemDecoration(decor);

它奏效了。事实证明,如果您不将和“int index”放入 addItemDecoration,它将默认为 -1 并默认将装饰添加到底部。

关于Android RecyclerView addItemDecoration 不会给 RecyclerView 底部添加装饰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42591285/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com