问题描述
我已经创建了一个Android应用程序与ListView控件。我已经添加了两个页眉和页脚到列表中。但加入除法时/分离器还创建报头和第一ListView项之间的空的空间。它做了同样在过去的ListView项和页脚。
I've created an android application with a ListView. I've added both a header and footer to the list. But when adding a divider/separator it also creates an empty space between the header and the first ListView item. It does the same for the last ListView item and the footer.
的空的空间相当于所有的ListView项目之间的分隔,以使其不绘制分频器的差的大小和刚刚离开空的空间。我以为我找到了与XML属性启用页脚分隔和启用头分隔的解决方案。但它们设置为false时,它不会改变任何事情。我甚至尝试以编程方式设置它们与
The empty space is equivalent to the size of the divider between all the ListView items, with the difference that it doesn't draw the divider and just leaves empty space. I thought I found the solution with the xml attributes 'Footer dividers enabled' and 'Header dividers enabled'. But when setting them to false, it doesn't change anything. I even tried to set them programmatically with
list.setFooterDividerEnabled(false);
list.setHeaderDividerEnabled(false);
但它是行不通的。有办法解决这个问题?我只是不希望空在那里,我想第一个项目恰好适合头(同为页脚)。
But it just doesn't work. Any way to fix that problem? I just don't want the empty space to be there, I want the first item to fit exactly to the header (same for the footer).
推荐答案
我偶然发现了同样的问题,但略有不同的情况比你。我的的ListView
有一个头(一个搜索框),但它下面的第一个项目包含一个节头(日期或一个字母),而不是常规列表项(与在图像中,一些文本,等)的形式的实际内容。因此,我希望它不被选择,所以在我的自定义适配器我已重写 areAllItemsEnabled
返回假
。
I stumbled upon the same problem, but in a slightly different situation than yours. My ListView
has a header (a search box), but the first item below it contains a section header (a date, or a letter) rather than being a regular list item (with the actual content in form of an image, some text, and so on). As such, I want it not to be selectable, so in my custom adapter I have overridden areAllItemsEnabled
to return false
.
大错,因为这正是罪魁祸首。见,看来,在设计上,的ListView
实施的,但仍预留空间之间的分隔所启用的项目和一个失效,即使这些分频器将不会被绘制之间的分隔。事实上,这是一种有意识的设计决定并不意味着它当然不傻。而不是到 IsEnabled的单调用返回的值最奇怪的是,该分频器绘图政策的依据是刚刚由
为后续的项目。 areAllItemsEnabled
返回的值
Big mistake, because that's exactly the culprit. See, it appears that, by design, the ListView
implementation only draw dividers between two enabled items, but still reserve space for dividers between an enabled item and a disabled one even if those dividers will not be drawn. The fact this is a conscious design decision does not mean it's not stupid, of course. Most weird of all, this dividers drawing policy is based just on the value returned by areAllItemsEnabled
instead of the values returned by single calls to isEnabled
for subsequent items.
因此,要解决它,我不得不返回真
从 areAllItemsEnabled
(我不停的重写的方法并添加有关这个问题的评论,否则我将无法在一个月从现在记得它):你瞧,白色空间消失,由分频器代替。现在,如果我想显示的ListView
头,并且是完全邻近第一部分头,我只需要选择一个除法颜色是一样的节头彩。
Thus, to work around it, I just had to return true
from areAllItemsEnabled
(I kept the overridden method and add a comment about this issue, otherwise I would not be able to remember it a month from now): lo and behold, white space disappeared, replaced by a divider. Now, if I want to show the ListView
header and the first section header as being exactly adjacent, I just have to choose a divider color that's the same as the section header color.
真希望这是相同的情况下你,或者我的解决方案可以帮助您以其它方式。
Really hope that's the same case as yours, or that my solution helps you in some other way.
这篇关于列表视图头和第一个项目之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!