安卓expandablelistview如何设置团体项目之间的空间如何设置、团体、项目、空间

2023-09-05 09:26:48 作者:卑微

我有expandablelistview,我想组项目之间添加填充物(或保证金),我用保证金钮的项目组,它的工作原理,但现在它是也适用于组项目和它的孩子的,我想保持组项之间的空间,而不是之间的组项目和它的孩子,我的工作是这样的:

主XML

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:后台=#FFFFFF>

    < TextView的Andr​​oid的:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:字体=衬线
        机器人:TEXTSIZE =25dip
        机器人:文字颜色=#025f7c
        机器人:文本=@字符串/ tv_allAddresses
        机器人:layout_gravity =中心
        机器人:layout_marginTop =20dip
        机器人:layout_marginBottom =25dip/>

    < ExpandableListView
        机器人:ID =@ + ID / elv_all_addresses_addresses
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
         机器人:填充=10dip>
    < / ExpandableListView>

< / LinearLayout中>
 

组XML

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

    <的TextView
        机器人:ID =@ + ID / tv_all_address_group_groupName
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:以下属性来=30dip
        机器人:文字颜色=#000000
        机器人:TEXTSIZE =20dip
        机器人:字体=衬/>

< / LinearLayout中>
 

子XML

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向>

    <的TextView
        机器人:ID =@ + ID / tv_all_address_child_label
        机器人:以下属性来=50dip
        机器人:字体=衬线
        机器人:TEXTSIZE =15dip
        机器人:文字颜色=@色/蓝紫
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>

    <的TextView
        机器人:ID =@ + ID / tv_all_address_child_value
        机器人:文字颜色=#000000
        机器人:layout_marginLeft =10dip
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>

< / LinearLayout中>
 
Android ExpandableListView 二

解决方案

由于您使用的是适配器从被延长 BaseExpandableListAdapter 这样你就可以通过编程设置填充设置填充,以组项目组的时候不扩大,然后取出时,该集团正在扩大的填充和各组组填充到最后一个孩子在里面。

设置填充到最后一个孩子

 公开查看getChildView(INT groupPosition,INT childPosition,
            布尔isLastChild,查看convertView,ViewGroup中父){
如果(childPosition == groups.get(groupPosition).getChilds()的大小() -  1){
            convertView.setPadding(0,0,0,20);
        } 其他
            convertView.setPadding(0,0,0,0);
        返回convertView;
}
 

设置填充到组项目时,它正在扩大

 公开查看getGroupView(INT groupPosition,布尔isExpanded,
            查看convertView,ViewGroup中父){
    如果(isExpanded)
            convertView.setPadding(0,0,0,0);
        其他
            convertView.setPadding(0,0,0,20);
        返回convertView;
    }
 

注意

我假设你正在使用ArrayList中为用户组和您的孩子,您只需更换 groups.get(groupPosition).getChilds()的大小() - 1 通过你的小组,这取决于在你的结构尺寸

I have expandablelistview and I want to add padding (or margin) between the groups items, I used margin-botton on the group items, it works but now it is also applied to the group item and its childs, I want to keep a space between groups items, not between a group item and its child, i work like this:

main 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>

group 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>

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="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>

解决方案

Because you are using adapter that is extending from BaseExpandableListAdapter so you can set padding programmatically by setting padding to group item when the group is not expanding and then remove the padding when the group is expanding and for each group set padding to last child in it.

setting padding to the last 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;
}

setting padding to group item when it is expanding

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;
    }

Note

I assume that you are using arraylist for your groups and your childs, you can just replace the groups.get(groupPosition).getChilds().size() - 1 by the size of your group depending in your structure