Empty space between listview header and first item
Asked Answered
G

6

34

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.

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).

Goodell answered 12/4, 2012 at 7:18 Comment(2)
Can you post some of the layout XML or code?Whetstone
Would the following help you? #4962499Frizzell
S
5

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.

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.

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.

Soutor answered 10/7, 2013 at 10:7 Comment(0)
M
1

I tried a solution by 幻影浪子 that works (based on android-pulltorefresh):

View Layout (header.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ProgressBar
            style="@android:style/Widget.ProgressBar.Small.Inverse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_margin="16dp"
            android:layout_marginTop="2dp" />
    </RelativeLayout>
</LinearLayout>

Inflating View:

m_headerView = inflater.inflate(R.layout.header, this, false);

Displaying View:

m_headerView.setPadding(0, 0, 0, 0);
m_headerView.setVisibility(View.VISIBLE);

Hiding View:

m_headerView.setPadding(0, -1000, 0, 0);
m_headerView.setVisibility(View.GONE);

It worked perfectly in our project. I hope it is helpful.

Mafala answered 25/1, 2013 at 7:4 Comment(2)
Your solution hides the whole header, but this is not what the OP asked for.Soutor
yes, indeed. I didn't understand the question very clearly. what I met is that even hiding the headview(set as gone), the headview still occupies a blank space, I found a solution on the web. Maybe it's not up to the question, but I think it's still useful for some other guys :)Mafala
R
0

In getview method you can check if the item is first or last and set custom devider which will be of 0 height or single pixel height of transparent color.

Riojas answered 15/7, 2013 at 9:16 Comment(0)
K
0

goto the ListView properties in android layout and search for spacing tag... some how in android, when creating new layouts, it will defaults creation is spacing header spacing and border properties. check it , if it is available then remove it

Kylix answered 26/7, 2013 at 8:2 Comment(0)
Y
0

Didn't find a great solution.

Set dividerHeight="0dp" and created my own dividers manually - either directly in the layout XML or dynamically in the adapter if you need more precise control.

Yecies answered 3/5, 2014 at 1:7 Comment(0)
P
-3

Just do

list.setDividerHeight(0)

That should take care of it.

Periodate answered 27/7, 2012 at 22:19 Comment(1)
This isn't the answer. Doing that would mean all dividers are gone, which is not what I want.Goodell

© 2022 - 2024 — McMap. All rights reserved.