ListView divider not showing in Android 5

后端 未结 4 1152
我寻月下人不归
我寻月下人不归 2021-01-04 23:34

I have a simple listview for which I have defined a custom drawable for the divider. I have defined the divider height to be 1dp. The listview is within a fragment.

相关标签:
4条回答
  • 2021-01-05 00:07

    By any chance, is your list item disabled via overriding isEnabled() to return false? There is a change (bug?) in Android L that causes list item dividers to be hidden if an item is disabled. I ran into this same issue, my list dividers working in everything but L, and this turned out to be the cause.

    Here are some more posts where this has been discussed, as well as an issue opened up with Google:

    Comments here: Disappearing divider in ListView when ArrayAdapter.isEnabled returns false

    How to add dividers between disabled items in ListView? - Lollipop

    https://code.google.com/p/android/issues/detail?id=83055

    If this is the case, it sounds like you may need to manually draw the divider with a custom view and set the divider to null on the list. I will be trying the same.

    0 讨论(0)
  • 2021-01-05 00:16

    You should use android:shape="rectangle" instead of android:shape="line" to make it work on every android version... (also change stroke to solid)

    <shape
        android:shape="rectangle" >
        <solid android:color="@color/custom_color" />
        <gradient android:height="1dp" />
    </shape>
    

    Have fun!

    0 讨论(0)
  • 2021-01-05 00:18

    Updated answer

    After further testing it appears that the dividers will only show if the height of the divider is strictly less than the dividerHeight set for the ListView. For instance:

    custom_divider.xml (Note that the divider height is specified by android:width)

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line" >
        <stroke
            android:width="1dp"
            android:color="$ffff0000" />
    </shape>
    

    Layout xml

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:divider="@drawable/custom_divider"
        android:dividerHeight="2dp"/>
    

    ...Will work. But this will not:

    custom_divider.xml (Note that the divider height is specified by android:width)

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line" >
        <stroke
            android:width="1dp"
            android:color="$ffff0000" />
    </shape>
    

    Layout xml

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:divider="@drawable/custom_divider"
        android:dividerHeight="1dp"/>
    

    My guess is that Google messed up with the optimization for drawing Listview dividers and will simply not draw them if there is not enough room.

    Original post

    Looks like you need to both set the dividerHeight on the ListView and the stroke width of the divider drawable for this to work on Android 5.

    Example:

    custom_divider.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line" >
        <stroke
            android:width="10dp"
            android:color="$ffff0000" />
    
        <gradient android:height="1dp" />
    </shape>
    

    Layout xml

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:divider="@drawable/custom_divider"
        android:dividerHeight="20dp"/>
    
    0 讨论(0)
  • 2021-01-05 00:22

    Height attribute is not a property under gradient tag. Use size attr like below.

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <solid android:color="@android:color/holo_blue_dark" />
    
        <size android:height="1px" />
    
    </shape>
    
    0 讨论(0)
提交回复
热议问题