问题描述
我无法在线上找到该问题的答案,因此答案必须如此简单,以至于没人愿意问这个问题,而我根本不知道.
I cannot find an answer to this question anywhere online, so the answer must be so simple that no one cared to ask it, and I simply don't know it.
基本上,我想在我的recyclerView中的每个项目之间以及RecyclerView的底部之间添加一条水平线.我找到了一种在RecyclerView中的每个项目之间放置分隔符 的方法,但在 end 处没有分隔符.
Essentially, I want to add a horizontal line between every item in my recyclerView AND to the bottom of the RecyclerView. I have found a way to place a divider between every item in the RecyclerView, but not one at the end.
根据文档(可在此处找到: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#addItemDecoration(android.support.v7.widget.RecyclerView.ItemDecoration ,int))
According to the documentation (found here: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#addItemDecoration(android.support.v7.widget.RecyclerView.ItemDecoration, int) )
将负整数放入addItemDecoration(RecyclerView.ItemDecoration装饰, int索引).应该解决此问题,并指出:
putting a negative int into the addItemDecoration(RecyclerView.ItemDecoration decor, int index). should solve this problem, stating:
这是我的自定义RecyclerView的构造方法:
This is the constructor method for my custom 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()中使用了一个负整数,但我无法弄清为什么它不能按预期的方式工作.
As you can see, I am using a negative integer in addItemDecoration(), and I cannot figure out why it is not working the way it is supposed to.
任何帮助将不胜感激!
推荐答案
根据Janice Kartika的建议,我将代码复制并粘贴到了我的项目中.复制并粘贴Janice的代码后,Android Studio给了我这个建议:
As per Janice Kartika's suggestion, I copied and pasted the code into my project. After I copied and pasted Janice's code, Android Studio gave me this suggestion:
RycyclerView库的较旧版本不包含分隔符装饰器,但在支持演示中提供了一个分隔器装饰器作为示例.该分隔器类已被广泛复制/粘贴到各种项目中.
在最新版本的支持库中,现在包括了分隔符装饰器,因此您可以将自定义副本替换为内置"版本android.support.v7.widget.DividerItemDecoration
这样,而不是使用HorizontalDividerItemDecoration(它是我们已导入的外部库的一部分),如下所示:
So instead of using HorizontalDividerItemDecoration (which was part of an external library that we had imported) like this:
decor = new HorizontalDividerItemDecoration.Builder(context).build();
addItemDecoration(decor, -1);
我这样使用默认的"DividerItemDecoration":
I used the default "DividerItemDecoration" like this:
decor = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
addItemDecoration(decor);
,它奏效了.原来,如果您不将"int索引"放入addItemDecoration中,则默认设置为-1,并且默认情况下将装饰添加到底部.
and it worked. Turns out if you do not put and "int index" into addItemDecoration, it will default to -1 and add the decoration to the bottom by default.
这篇关于Android RecyclerView addItemDecoration不会在RecyclerView的底部添加装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!