Android expandablelistview how to set space between group elements

I have an extended list and I want to add indentation (or margins) between the elements of the groups, I used margin-botton for the elements of the group, it works, but now it also applies to the group element and its children, I want to save space between the elements groups, and not between a group element and its child, I work as follows:

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> 

xml group

 <?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> 
+7
source share
6 answers

Because you are using an adapter that extends from BaseExpandableListAdapter , so you can programmatically set the registration by setting the addition to the group element when the group does not expand, and then indenting when the group expands, and for each group adding the last child to it.

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

add-on settings for a group element when it is expanded

 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 use arraylist for your groups and your children, you can just replace groups.get(groupPosition).getChilds().size() - 1 with the size of your group depending on your structure

+6
source

In case you are looking for the best option, just add

 expandablelsv.setDividerHeight(); 

after group and child indicator it works fine in my case, try

+4
source

check if the latter is the last, and add the addition there

 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (isLastChild) { convertView.setPadding(0, 0, 0, 30); } return convertView; } 
+2
source

I had the same problem, so I came up with a childish solution. What I did was added to the linear layout with the same properties as in the parent layout and in the childhood layout file of the extensible list view. And when the group was pressed, I made the โ€œlinear layoutโ€ of the parent layout invisible, and my work was done.

Group Layout File

 <TextView android:id="@+id/expandable_faqparent_id" android:layout_width="match_parent" android:layout_height="50dp" android:layout_margin="2dp" android:layout_marginBottom="30dp" android:background="#ffffff" android:gravity="center" android:text="hello this is dummy text" android:textSize="20dp" android:textStyle="bold" /> <LinearLayout android:id="@+id/linear_parent_faq" android:layout_width="match_parent" android:layout_height="10dp" android:background="#cfd8dc" android:layout_weight="0.06" android:orientation="vertical"> </LinearLayout> 

Baby Layout File

 <TextView android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/expandable_faq_child_id" android:text="DUMMY PARENT" android:background="#ffffff" android:gravity="center" android:elevation="2dp"/> <LinearLayout android:id="@+id/linearfaq" android:layout_width="match_parent" android:layout_height="10dp" android:background="#cfd8dc" android:orientation="vertical"> </LinearLayout> 

To hide the linear layout when clicking on any group Add this line to the addChildView method in the adapter class

 lv.setVisibility(View.INVISIBLE); 

here lv contains the id of the linear layout

linear_parent_faq

group layout file.

0
source

I solved this by adding two views, one to the header and the other to the child, as shown below.

Header.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:id="@+id/rl_header" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/square_box" android:padding="@dimen/padding_10"> <ImageView android:id="@+id/expandableIndicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/ic_downarrow" /> </RelativeLayout> <View android:id="@+id/view_hidden" android:layout_width="match_parent" android:layout_height="10dp" android:layout_below="@+id/rl_header" android:background="@android:color/transparent" /> </RelativeLayout> 

child.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:id="@+id/rl_childView" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/square_box" android:padding="@dimen/padding_10"> <TextView android:id="@+id/tv_startDate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:gravity="left" android:paddingTop="@dimen/padding_5" android:paddingBottom="@dimen/padding_5" android:text="Start Date \nSep. 23, 2019" android:textSize="@dimen/text_16" /> <ImageView android:id="@+id/iv_arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/right_arrow" /> </RelativeLayout> <View android:id="@+id/view_hidden" android:layout_width="match_parent" android:layout_height="10dp" android:layout_below="@+id/rl_childView" android:background="@android:color/transparent" /> </RelativeLayout> 

Now add this code to your Adapter class. The idea is to show / hide the view, when the group is expanded / collapsed, show on the last element of the child element.

 @Override public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) { String listTitle = (String) getGroup(listPosition); String listCount = "(" + getChildrenCount(listPosition) + ")"; if (convertView == null) { LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(R.layout.expandable_header, null); } View view_hidden = convertView.findViewById(R.id.view_hidden); if (isExpanded) { view_hidden.setVisibility(View.GONE); } else { view_hidden.setVisibility(View.VISIBLE); } return convertView; } @Override public View getChildView(int listPosition, final int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) { final Medication medication = (Medication) getChild(listPosition, expandedListPosition); if (convertView == null) { LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(R.layout.expandable_item, null); } View view_hidden = convertView.findViewById(R.id.view_hidden); if (isLastChild) { view_hidden.setVisibility(View.VISIBLE); } else { view_hidden.setVisibility(View.GONE); } return convertView; } 
0
source

These two attributes help free up space.

 android:divider="@android:color/transparent" android:dividerHeight="8dp" 
0
source

All Articles