【问题标题】:Android ExpandableListView divider is invisibleAndroid ExpandableListView 分隔线不可见
【发布时间】:2012-08-27 15:48:35
【问题描述】:

我有以下 ExpandableListView:

<ExpandableListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/listView"
            android:groupIndicator="@android:color/transparent"
            android:background="@color/app_background"
            android:scrollingCache="false"
            android:choiceMode="none"
            android:divider="@color/gray_dark"
            android:dividerHeight="2dp"
            android:childDivider="@color/gray_dark"
            android:cacheColorHint="@color/app_background"/>

我遇到的问题是可扩展列表视图没有绘制分隔线,或者至少不可见.. 我在我的可扩展适配器中添加了一个自定义视图作为组视图以及自定义列表项。这会是个问题吗?

有谁知道我可以做些什么来为列表子项启用分隔符?

提前谢谢你。

【问题讨论】:

  • 您是通过Adapter 还是通过((ViewGroup) findViewById(R.id.listView)).addView(child) 之类的方式“添加”自定义Views 到列表中?如果是后者,它将无法按您的预期工作。
  • 我正在通过适配器添加自定义视图
  • 当我将 Android Studio 从 4.1.2 升级到 2020.3.1 时,我开始遇到这个问题,分隔线的高度开始显示不一致的值。我必须以编程方式将最小值设置为 2:expandableListView.setDividerHeight(2);

标签: android android-listview expandablelistview divider


【解决方案1】:

我的可扩展适配器似乎有问题。我被以下方法覆盖:

@Override
public boolean areAllItemsEnabled() {
    return true;
}

我没有返回 true,而是让方法返回 false,这就是问题所在... 因此,如果有人遇到此问题,请检查该方法并确保它返回 true 而不是 false

请注意,如果您扩展 BaseExpandableListAdapter,则无需重写该方法

【讨论】:

  • 太好了,这已经让我烦恼了一个小时左右 :) 为了将来参考,我还发现我需要为 isChildSelectable() 返回 true
  • 我同意@sleeke,必须同时使用areAllItemsEnabled()isChildSelectable() 才能让它不再隐形。
【解决方案2】:

我从来没有试图弄乱 xml 中的分隔符,我总是通过代码来完成。以下是一个 sn-p,它将分隔线设置为红色,并在它从中心移动到边缘时淡出。只有一个分隔高度调用,因为该部分会影响组和孩子。

import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;

// code to set up expandablelistview

int[] colors = {0, 0xFFFF0000, 0}; // red for the example 
getListView().setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors)); 
getListView().setChildDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors)); 
getListView().setDividerHeight(4); 

【讨论】:

  • 嗯,好的,我会尝试在代码中执行此操作,但如果有办法在 xml 中执行此操作会更好......我明天会尝试一下,看看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 1970-01-01
  • 2012-04-23
  • 2011-03-15
  • 2014-10-03
相关资源
最近更新 更多