【问题标题】:ListView Divider not shown on click-disabled elements on Android LollipopListView Divider 未显示在 Android Lollipop 上的单击禁用元素上
【发布时间】:2015-05-21 18:44:12
【问题描述】:

我有一个 ListView 填充了几个元素。如果满足某个条件,则其中一些元素将变为不可点击。另外,我为我的 ListView 制作了一个自定义分隔线。问题是,只有在 Lollipop 版本的 Android 上,如果某个元素被禁用,则不再显示分隔符。在其他版本的Android上运行良好,问题只出现在Android 5.0+上。

禁用项目的代码

@Override
public boolean isEnabled(int position) {
    Account account = this.items.get(position);
    if (account.getActive.equals("Inactive")) {
        return false;
    }
    return true;
}

列表视图 xml

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@drawable/separator_listview"
        android:dividerHeight="1dp" >
</ListView>

任何想法为什么会发生这种情况?

【问题讨论】:

  • 列表视图的高度和宽度必须始终为 match_parent。
  • @Kartheek MATCH_PARENT 是我最初的方法。但仍然有相同的结果。

标签: android listview android-listview


【解决方案1】:

要为ListView 添加分隔线,您需要创建一个drawable。
res/drawable/divider.xml

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

Listview.xml

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@drawable/drawable"
        android:dividerHeight="1dp" >
</ListView>

【讨论】:

  • 正如我所提到的,我创建了自定义可绘制对象,并将其应用于我的 xml 代码。
猜你喜欢
  • 2011-02-23
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
相关资源
最近更新 更多