Search code examples
androidscrollview

resizing scrollView when its content changes


i have the layout below :

<?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"
    android:animateLayoutChanges="true"
    android:orientation="vertical">

    <eddine.charef.mechalikh.swipedemo.NotifyingScrollView
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:layout_marginTop="-40dp"
        android:fillViewport="false">

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

        <ImageView
            android:id="@+id/image_header"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:src="@drawable/stethoscope"
            android:scaleType="centerCrop" />
        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/view"
            android:background="@drawable/gradient"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/view2"
            android:layout_above="@+id/linearLay"
            android:background="@drawable/gradient2"
            android:visibility="gone" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="-10dp"
            android:layout_below="@+id/image_header"
            android:id="@+id/linearLay"
            android:animateLayoutChanges="true"
            android:background="#ffffff"
            android:layout_alignTop="@+id/editPrenom">

            <Space
                android:layout_width="fill_parent"
                android:layout_height="22dp"
                android:id="@+id/space_spinner"
                android:visibility="gone" />

            <Spinner
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/spinner3"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:visibility="gone"
                android:gravity="center" />

        </LinearLayout>


        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView5"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/linearLay"
            android:visibility="visible" />


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/bienvenue"
            android:id="@+id/textView20"
            android:textColor="#000"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/view"
            android:visibility="visible"
            android:layout_marginTop="20dp"
            android:gravity="left"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/textView21"
            android:textColor="#000"
            android:layout_below="@id/textView20"
            android:layout_centerHorizontal="true"
            android:visibility="visible"
            android:gravity="left"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/imageView5"
            android:layout_below="@+id/linearLay"
            android:src="@drawable/shadow_divider"
            android:layout_marginTop="-3dp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView2"
            android:adjustViewBounds="true"
            android:src="@drawable/param"
            android:onClick="params_click"
            android:visibility="visible"
            android:layout_above="@+id/linearLay"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="15dp"
            android:layout_marginBottom="-25dp"
            android:scaleType="center" />

    </RelativeLayout>
</eddine.charef.mechalikh.swipedemo.NotifyingScrollView>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/image_scroll_up_2"
        android:adjustViewBounds="true"
        android:src="@drawable/scroll_up"
        android:onClick="scroll_up"
        android:visibility="invisible"
        android:layout_above="@+id/space"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="15dp" />

    <Space
        android:layout_width="fill_parent"
        android:layout_height="15dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/space" />
</RelativeLayout>

at runtime the listView inside the scrollView will be updated ... when user clicks "date", it displays everything normal (no problem) when he clicks "followed", even if the listview is empty and the scrollview is ok (no problem, cannot scroll beacuse the list is empty) but when selecting "messages" , when the list is empty or just few items ...the scroll view can scroll (i mean a lot of space see the picture)

enter image description here

enter image description here

THAT WHITE DISC IN THE CENTRE OF THIS PICTURE IS THE TOUCH POINT FROM DEVELOPERS OPTIONS IN SETTINGS ,IT IS NOT A PART OF MY LAYOUT

you can see where the list ends in image two (the background of the listView isn't the same with the scrollView )

So why the scrollView won't be resized ?

Note: i use this function to resize the listView so it will be displayed in the scrollView

           ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
        // pre-condition
              return;
        }

        int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
        for (int i = 0; i < listAdapter.getCount(); i++) {
             View listItem = listAdapter.getView(i, null, listView);
             if (listItem instanceof ViewGroup) {
                listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
             }
             listItem.measure(0, 0);
             totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
                  listView.setLayoutParams(params);
    }

and I call it everywhere where the items change.


Solution

  • I don't understand why you put a scollview inside a listview, did you try to perform a parallax or play with the toolbar?

    To play with parallax or toolbar it's more simple to use RecyclerView. You can work with ObservableRecyclerView witch provide a lot of sample:

    https://github.com/ksoichiro/Android-ObservableScrollView