开发者

Disappearing divider in ListView when ArrayAdapter.isEnabled returns false

开发者 https://www.devze.com 2023-02-17 21:54 出处:网络
I\'m using ListActivity with my own ArrayAdapter class. When I override the method开发者_运维知识库s ArrayAdapter.areAllItemsEnabled() and ArrayAdapter.isEnabled() the divider between some cells in th

I'm using ListActivity with my own ArrayAdapter class. When I override the method开发者_运维知识库s ArrayAdapter.areAllItemsEnabled() and ArrayAdapter.isEnabled() the divider between some cells in the list view disappear. Does anyone know how to avoid this? I need the dividers to display even for disabled cells.


Return true in areAllItemsEnabled() and false in isEnabled for specific item. The disabled item wont be clickable but you will still be able to view the divider lines

Note: This doesn't work for Android 5


You can essentially disable a list item by giving any one of its elements the following properties.

android:focusable="true"
android:clickable="true"

So the following list item layout will not be clickable, but will show dividers, without the need to implement areAllItemsEnabled() or isEnabled(int position).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="true"
    android:clickable="true">
    <TextView
        android:text="Large Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

This may help in Android 5.0 where the original answer no longer seems to work.


This worked for me. This will show the divider as well as disable the click on list item. Even in Android 5.0.

Set this on the list item

android:focusable="true"
android:clickable="false"

Setting just clickable to 'false' didn't work for me. And Overriding isEnabled() caused the above mentioned issue of hiding the divider in 5.0.

And my ListView looks like this.

<ListView
    android:id="@+id/lvItems"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@color/Gray"
    android:dividerHeight="1px">
</ListView>

No android:listSelector="@android:color/transparent" needed here


All of the solutions above have a problem with compatibility (works nice on one version of Android, but fails on other) - especially if you want to use transparency. The only really robust solution is to use nine-patch file.

You can download from: http://ge.tt/517ZIFC2/v/1?c

Just place the chosen png to the drawable-nodpi directory to avoid resampling.

And use it in your Adapter:

convertView.setBackgroundResource(R.drawable.border_b1px_black);


You will have to enable the rows in the isEnabled() method for Android 5.0 and higher.

But you can still disable the rows another way:

In the following example, I'm disabling every row but row 0:

In your getView method for your Adapter:

if (position > 0) {
        convertView.setClickable(true);
}

For some reason setting clickable to true disables the row. I'm not sure why. I assumed at first this would make it clickable but it doesn't.

Reference: Android ListView child View setEnabled() and setClickable() do nothing

See user622689 answer.


Instead of setting isEnabled, on your ListView, set android:listSelector="@android:color/transparent" as well as android:focusable="true" and android:clickable="true". It gives the same effect


I can verify that when areAllItemsEnabled() returns false, then for every specific row that you want to set as non-selectable via isEnabled(int position) the line separator (divider) disappears. Setting areAllItemsEnabled() to always return true, and playing just with isEnabled(int position) should make specific rows non-selectable with the divider showing just fine.


use setDivider(Drawable divider) method of the listview


I had the same problem on Lollipop. Workaround for me was to change the background in the listview to transparent. In my case I was missing the dividers on the nav drawer on rows where isEnabled returns false when I set a background to a color. Try something like:

 android:background="@android:color/transparent"


I had a similar problem. I solved it by explicitly setting the divider height of the list view to be "1dp"

    <ListView
         android:id="@+id/listView"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:dividerHeight="1dp"
    />


To actually EMULATE a disabled item, in the getView of the adapter, where layout = (your return value):

        if ( ! YOUR_ITEM_REF?.isEnabled()) {
            layout.setClickable(true);
            layout.setAlpha(0.5f);
        } else {
            layout.setClickable(false);
            layout.setAlpha(1f);
        }

This 100% works the way you would think it should, by simply disabling the item, and painting it as disabled by using a simple alpha filter to get the 'Ghost' effect.

Where YOUR_ITEM_REF is your object (item) in the list, not the adapter or the list, but the actual item.

Your adapter should return true for allItemsEnabled and isEnabled, to disable the broken default feature handling.

This is how i fixed my problem, using the answer given by Sandy D.. This does not work if your layout has a click handler set (which is not likely, but could happen through user error).

0

精彩评论

暂无评论...
验证码 换一张
取 消