Rectangle 27 0

This is a tricky one. There were several discussion here on SO on how to use ScrollView with ListView. Have a look at this question: Scrolling with Multiple ListViews for Android. The idea is to have only one list (create a custom adapter) and add headers and footers before and after your Expandable list.

android - Using a ScrollView on RelativeLayout that contains an Expand...

android scrollview relativelayout expandablelistview
Rectangle 27 0

RelativeLayout is not scrollable, hence cannot be larger than the physical display.

The first solution that comes to mind is placing RelativeLayout in ScrollView. There are couple of issues with that:

RelativeLayout
ScrollView
ExpandableListView
wrap_content

Note: You cannot use the value wrap_content for the android:layout_height attribute of a ExpandableListView in XML if the parent's size is also not strictly specified (for example, if the parent were ScrollView you could not specify wrap_content since it also can be any length. However, you can use wrap_content if the ExpandableListView parent has a specific size, such as 100 pixels.

  • ListView should never be placed under ScrollView. From docs:

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.

In this specific case, since the view hierarchy is flat (just one level), you should be using LinearLayout instead of RelativeLayout. RelativeLayout will normally make 2 measure pass.

Use LinearLayout instead of RelativeLayout and set layout_weight for all the list views including expandable list view, so that left out space is equally divided by all the list views.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cccc"
    android:orientation="vertical"
    android:weightSum="4">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/listView"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#cccc"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Men"
        android:textSize="30dp"
        android:textAllCaps="true"
        android:textAlignment="center"
        android:id="@+id/menTextView" />

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/menExpandableListView"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="women"
        android:textSize="30dp"
        android:textAllCaps="true"
        android:textAlignment="center"
        android:id="@+id/womenTextView"/>

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/womenExpandableListView"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="kids"
        android:textSize="30dp"
        android:textAllCaps="true"
        android:textAlignment="center"
        android:id="@+id/kidsTextView"/>

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/kidsExpandableListView"/>

</LinearLayout>

@user3249477 I checked in Moto G in landscape mode, all 3 expandable list views are displayed with 1 item. Rest items need to be scrolled. Either OP has to change the UI design or have to live with it.

What you are describing, one item in each expandable list view, I also got that result after fiddling with it for a while.. But that is not acceptable. How do you suggest I change my UI where I have three levels, Men -> Footwear -> Casual Shoes etc..

I'm testing the LinearLayout solution..

your solution looks good in larger screens but still looks a tiny bit awkward in small screens, I want all the children and opened groups to be opened completely, and not fit in the little space they are given wrt to the ratios I set up. I want them to wrap content and still be able to scroll to the missing view. I'm gonna try the other answer as well before marking the correct answer. Your solution is still acceptable but I want to test the other solution too.. and see what works better..

android - Set of alternate TextViews and ExpandableListView inside a R...

android android-layout scrollview relativelayout expandablelistview
Rectangle 27 0

Apart from header/footer approach if you will define all the view before expandableList and give it relative position (layout_above/layout_below) , u will get the desired view .

android - Using a ScrollView on RelativeLayout that contains an Expand...

android scrollview relativelayout expandablelistview
Rectangle 27 0

I know it is too late but I Wrote for other people that encountering this problem.The problem is when you use ExpandableListView inside Scrollview ExpandableListView height not defined correctly.So I compute height programmatically like this:

//for converting dp to pixel
final float scale = getApplicationContext().getResources().getDisplayMetrics().density;
//when all the headers are Collapse
expListView.getLayoutParams().height=listDataHeader.size()*(int) (43 * scale + 0.5f);

expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int groupPosition) {

                    expListView.getLayoutParams().height+=listDataChild.get(listDataHeader.get(groupPosition)).size()*(int) (34 * scale + 0.5f);

                }
            });

            // Listview Group collasped listener
            expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

                @Override
                public void onGroupCollapse(int groupPosition) {
                    expListView.getLayoutParams().height-=listDataChild.get(listDataHeader.get(groupPosition)).size()*(int) (34 * scale + 0.5f);

                }
            });

android - ScrollView and RelativeLayout scrolling issue for Expandable...

android scrollview relativelayout expandablelistview
Rectangle 27 0

// try this way,hope this will help you...
<?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">

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.70"
        android:fillViewport="true" >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/carType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TableLayout
                android:id="@+id/infoTable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/carType"
                android:stretchColumns="*" >

                <TableRow>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:paddingBottom="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="5dp"
                        android:text="@string/tire_replacement"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/tirereplacement"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:clickable="true"
                        android:text="@string/add_date" />
                </TableRow>

                //...rows...

            </TableLayout>
        </RelativeLayout>
    </ScrollView>
    <ExpandableListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.30">
    </ExpandableListView>
</LinearLayout>

Android - RelativeLayout with ExpandableListView in ScrollView - Stack...

android android-layout scrollview relativelayout expandablelistview
Rectangle 27 0

You may wish to get rid of the ScrollView entirely and place the 'user_profile' RelativeLayout in a separate layout file, in order to inflate it as a header to your ExpandableListView. Like this:

ExpandableListView listView = (ExpandableListView) findViewById(R.id.expandable_list);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.user_profile, listView, false);
listView.addHeaderView(header);

android - RelativeLayout and ListView inside ScrollView - Stack Overfl...

android android-layout listview scrollview relativelayout
Rectangle 27 0

Put all the views above the expandable listView as listView header , and all the views below as listView footer. Then you have scrolling enabled for everything.

android - Using a ScrollView on RelativeLayout that contains an Expand...

android scrollview relativelayout expandablelistview
Rectangle 27 0

Maybe this will help you. put a ListView in a ScrollView and fix the height of ListView according to it's item How can I put a ListView into a ScrollView without it collapsing?

Android - RelativeLayout with ExpandableListView in ScrollView - Stack...

android android-layout scrollview relativelayout expandablelistview
Rectangle 27 0

The whole problem is in your design. First of all RelativeLayout takes up the whole screen (or the free space that's left) thus, the last ExpandableListView becomes very small and practically unscrollable.

right

Make a single ExpandableListView

Headers act as empty groups

getGroupTypeCount
  • getGroupType return 1 if a header, else return 0
  • Determine the group type in getGroupView and inflate accordingly
expListView.setIndicator(null);
  • Make them a part of your normal group layout (as an ImageView)
getGroupView
elv.setOnGroupItemClick()

Looks like a lot of work but in the end it will be worth it.

The result: a dynamic ExpandableListview which scrolls properly no matter how many headers, groups and children you have.

sorry for late reply, I waited so long for someone to answer, and totally disappeared when someone finally replied..

This is how I did it, and used the isExpanded flag available to us in getGroupView to have my custom expand/collapse indicator instead of messing with elv.OnGroupItemClickListener

android - Set of alternate TextViews and ExpandableListView inside a R...

android android-layout scrollview relativelayout expandablelistview