gpt4 book ai didi

android expandablelistview如何设置组项目之间的空间

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:49 25 4
gpt4 key购买 nike

我有 expandablelistview,我想在组项目之间添加填充(或边距),我在组项目上使用了 margin-botton,它有效,但现在它也应用于组项目和它的 child ,我想在组项目之间保留一个空间,而不是在组项目和它的 child 之间,我是这样工作的:

主要 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF">

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="serif"
android:textSize="25dip"
android:textColor="#025f7c"
android:text="@string/tv_allAddresses"
android:layout_gravity="center"
android:layout_marginTop="20dip"
android:layout_marginBottom="25dip"/>

<ExpandableListView
android:id="@+id/elv_all_addresses_addresses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip">
</ExpandableListView>

</LinearLayout>

组xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_all_address_group_groupName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30dip"
android:textColor="#000000"
android:textSize="20dip"
android:typeface="serif" />

</LinearLayout>

子xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
android:id="@+id/tv_all_address_child_label"
android:paddingLeft="50dip"
android:typeface="serif"
android:textSize="15dip"
android:textColor="@color/BlueViolet"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tv_all_address_child_value"
android:textColor="#000000"
android:layout_marginLeft="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

最佳答案

因为您正在使用从 BaseExpandableListAdapter 扩展而来的 adapter,所以您可以通过在组未展开时将填充设置为组项目来以编程方式设置填充,然后删除填充当组正在扩展时,为每个组设置填充到其中的最后一个 child 。

设置最后一个 child 的填充

public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (childPosition == groups.get(groupPosition).getChilds().size() - 1) {
convertView.setPadding(0, 0, 0, 20);
} else
convertView.setPadding(0, 0, 0, 0);
return convertView;
}

分组项展开时设置内边距

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (isExpanded)
convertView.setPadding(0, 0, 0, 0);
else
convertView.setPadding(0, 0, 0, 20);
return convertView;
}

注意事项

我假设您正在为您的组和您的 child 使用 arraylist,您可以将 groups.get(groupPosition).getChilds().size() - 1 替换为您的大小根据你的结构分组

关于android expandablelistview如何设置组项目之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14802189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com