Android:如何隐藏可展开列表中子项的分割线

44

我需要完全从 ExpandableListView 中删除分隔符。对于父项,有一个 setDividerHeight 方法,可以传递零值来达到目的。但是没有类似的方法适用于子分隔符。是否有任何方法来隐藏它?

7个回答

54
如果您想完全删除ExpandableListView中的分隔符,setDividerHeight可以用于父项和子项。子项的分隔线将使用与正常分隔线相同的高度绘制或由setDividerHeight()设置。同时我正在使用以下方法来隐藏一个分隔线并显示另一个分隔线,只需将分隔线和项目设置为相同的颜色即可:
 ExpandableListView expView = getExpandableListView();
 expView.setGroupIndicator(null);
 expView.setChildIndicator(null);
 expView.setChildDivider(getResources().getDrawable(R.color.greywhite));        
 expView.setDivider(getResources().getDrawable(R.color.white));
 expView.setDividerHeight(2);

setDividerHeight 必须放在 setChildDividersetDivider 之后,否则分隔线高度会变为0。

等待更多答案...


当然,如果为填充设置间距很烦人,您可以将DividerHeight设置为0,并在自定义适配器中使用单元格中的分隔符。为了防止底部项目有分隔符,您可以在index == getGroupCount()-1 && index2 == getChildCount()-1时隐藏分隔符。 - csga5000

27

要隐藏子分隔符,请将其颜色设置为透明 #00000000

在color.xml文件中定义透明度。

<color name="transparent">#00000000</color>

然后设置子分隔符

listView.setChildDivider(getResources().getDrawable(R.color.transparent))

或者在布局的XML文件中

<ExpandableListView 
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:childDivider="#00000000"/>

10
将分隔符设置为透明颜色并不是真正地将其删除。 - 7heViking
7
你可以使用android:childDivider="@android:color/transparent"作为另一种选择。 - cassioso
这仅在您列表背景与列表元素具有相同颜色时有效。如果您的背景为淡色,元素为白色,则此方法将无效。 - Gabe Sechan

9
您可以使用属性
<ExpandableListView 
    android:id="@+id/interestlvExp"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:divider="@null">
</ExpandableListView>

但这也会删除组列表分隔符。

3

不要写冗长的代码,使用属性 ExpandableListView。 这样就可以了 :-

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

2
这将隐藏分隔线,而不是删除它(它仍然会占用空间)。 - O.O.Balance

1
如果你只想移除子项分隔线,你可以创建一个与子项背景颜色相同的可绘制对象。然后将其设置为你的子项分隔线。 ShapeDrawable sd1 = new ShapeDrawable(new RectShape()); sd1.getPaint().setColor(你的子项背景颜色); mExpListView.setChildDivider(sd1);

0
将您的可扩展列表视图的分隔符高度设置为0。
//setDividerHeight(0)

然后在您的headerView xml中底部添加一个视图

<View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_gravity="bottom"
        android:background="@android:color/darker_gray"
        />

-1

android:divider="@color/tranceParent"

安卓:分隔线="@color/tranceParent"

 <ExpandableListView
        android:id="@+id/expandableList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/tranceParent"
        app:layout_constraintTop_toTopOf="parent"
        android:divider="@color/tranceParent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:listitem="@layout/item_parent_case"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接