Search code examples
androidandroid-studioandroid-layoutscrollviewexpandablelistview

How to make ExpandableListView scroll inside ScrollView and not on itself?


I can’t get the ExpandableListView to scroll inside the ScrollView and not on itself. The problem is that if I change the ExpandableListView to a TextView with a large text, everything works fine. But the ExpandableListView scroll works without a ScrollView.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/footer"
        android:layout_marginBottom="0dp"
        android:fillViewport="true"
        android:scrollbars="none">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/ImageGoods"
                    android:layout_width="match_parent"
                    android:layout_height="250dp"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:src="@drawable/pizza_pepperoni_lovers" />

                <TextView
                    android:id="@+id/ImageViewText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="25dp"
                    android:layout_marginLeft="20dp"
                    android:layout_alignLeft="@id/ImageGoods"
                    android:layout_alignTop="@id/ImageGoods"
                    android:text="NEW Product!!!"
                    android:textStyle="bold"
                    android:textColor="@color/Black"
                    android:textSize="30dp"
                    android:includeFontPadding="false"
                    android:background="@color/TextGoodsImageColor"
                    android:gravity="center"/>

                <Button
                    android:id="@+id/SizeButton1"
                    android:layout_width="80dp"
                    android:layout_height="35dp"
                    android:layout_below="@+id/textView2"
                    android:layout_alignStart="@+id/textView2"
                    android:layout_marginStart="0dp"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/button_border"
                    android:text="25mm"
                    android:textAllCaps="false"
                    android:textSize="12sp" />

                <Button
                    android:id="@+id/SizeButton2"
                    android:layout_width="80dp"
                    android:layout_height="35dp"
                    android:layout_below="@+id/textView2"
                    android:layout_marginStart="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_toEndOf="@+id/SizeButton1"
                    android:background="@drawable/button_border"
                    android:text="30mm"
                    android:textAllCaps="false"
                    android:textSize="12sp" />

                <Button
                    android:id="@+id/SizeButton3"
                    android:layout_width="80dp"
                    android:layout_height="35dp"
                    android:layout_below="@+id/textView2"
                    android:layout_marginStart="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_toEndOf="@+id/SizeButton2"
                    android:background="@drawable/button_border"
                    android:text="36mm"
                    android:textAllCaps="false"
                    android:textSize="12sp" />


                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/ImageGoods"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:text="size"
                    android:textColor="@color/Black"
                    android:textStyle="bold" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/SizeButton3"
                    android:layout_marginTop="7dp">

                    <ExpandableListView
                            android:id="@+id/lvExp"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:scrollbars="none"
                            android:transcriptMode="disabled"/>

                </RelativeLayout>

        </RelativeLayout>
    </ScrollView>
    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorFooter"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_add_goods_to_basket"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:padding="5dp"
            android:text="Add basket"
            android:inputType="textFilter"
            android:textAlignment="center"
            android:layout_alignParentRight="true"
            android:textColor="@color/White" />

        <Button
            android:id="@+id/btn_change_qtty_minus"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="7dp"
            android:layout_marginTop="7dp"
            android:layout_marginBottom="7dp"
            android:background="@drawable/plus_minus_button"
            android:drawableTop="@drawable/minus"
            android:gravity="center_horizontal"
            android:padding="5dp"
            android:textAlignment="center"
            android:textColor="@color/White"
            android:textSize="25dp" />

        <Button
            android:id="@+id/btn_change_qtty_pilus"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:layout_marginStart="7dp"
            android:layout_marginTop="7dp"
            android:layout_marginBottom="7dp"
            android:layout_toEndOf="@+id/textView5"
            android:background="@drawable/plus_minus_button"
            android:gravity="center_horizontal"
            android:padding="5dp"
            android:drawableTop="@drawable/pilus"
            android:textAlignment="center"
            android:textColor="@color/White"
            android:textSize="25dp" />

        <TextView
            android:id="@+id/TotalSum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/btn_change_qtty_minus"
            android:layout_alignBottom="@+id/btn_change_qtty_minus"
            android:layout_marginStart="7dp"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="0dp"
            android:layout_toEndOf="@+id/btn_change_qtty_pilus"
            android:textColor="@color/White"
            android:gravity="center"
            android:text="25$"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/btn_change_qtty_minus"
            android:layout_alignBottom="@+id/btn_change_qtty_minus"
            android:layout_marginStart="7dp"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="0dp"
            android:layout_toEndOf="@+id/btn_change_qtty_minus"
            android:textStyle="bold"
            android:textColor="@color/White"
            android:gravity="center"
            android:text="x1" />

    </RelativeLayout>
</RelativeLayout>

This is my xml code.

enter image description here

enter image description here

How can this problem be solved?


Solution

  • Just found the answer to my question.

    enter link description here

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.expandable, container, false);
    
        ExpandableAdapter adapter = new ExpandableAdapter();
        expandableListView.setAdapter(adapter);
        setExpandableListViewHeight(expandableListView, -1);
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
                setExpandableListViewHeight(parent, position);
                return false;
            }
        });
        return view;
    }
    
    
    
    private void setExpandableListViewHeight(ExpandableListView listView,
                                   int group) {
        ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
        int totalHeight = 0;
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
                View.MeasureSpec.EXACTLY);
        for (int i = 0; i < listAdapter.getGroupCount(); i++) {
            View groupItem = listAdapter.getGroupView(i, false, null, listView);
            groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
    
            totalHeight += groupItem.getMeasuredHeight();
    
            if (((listView.isGroupExpanded(i)) && (i != group))
                    || ((!listView.isGroupExpanded(i)) && (i == group))) {
                for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                    View listItem = listAdapter.getChildView(i, j, false, null,
                            listView);
                    listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
    
                    totalHeight += listItem.getMeasuredHeight();
    
                }
            }
        }
    
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        int height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
        if (height < 10)
            height = 200;
        params.height = height;
        listView.setLayoutParams(params);
        listView.requestLayout();
    
    }