简体   繁体   中英

Make whole layout that contains ListView scrollable

I have a LinearLayout that contains ListView . When the ListView is scrolled, the LinearLayout should scroll too.

But now when ListView is scrolled, the RelativeLayout above ListView not scrollable . Instead they always show on top! I tried to add ScrollView in LinearLayout , but get java.lang.IllegalStateException: ScrollView can host only one direct child

Here is what I have tried

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:orientation="vertical"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <RelativeLayout
            style="?android:attr/buttonStyleSmall"
            android:clickable="true"
            android:id="@+id/relativeLayout1"
            android:background="@drawable/boarder_layout"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <TextView
                android:layout_marginTop="12dp"
                android:textSize="15sp"
                android:textColor="@color/darkGreen"
                android:layout_marginLeft="8dp"
                android:id="@+id/editTextDate"
                android:background="@android:color/transparent"
                android:layout_toRightOf="@+id/imgProfilePicture"
                android:text="Post a new update"
                android:padding="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        <ImageView
                android:padding="8dp"
                android:src="@drawable/images"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:id="@+id/imgProfilePicture"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_gravity="center"/>
    </RelativeLayout>

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:layout_marginTop="10dp"
                  android:orientation="horizontal">

        <RelativeLayout
                style="?android:attr/buttonStyleSmall"
                android:id="@+id/relativeLayout2"
                android:clickable="true"
                android:layout_weight="0.5"
                android:background="@drawable/boarder_layout"
                android:layout_marginLeft="10dp"
                android:layout_width="0dp"
                android:layout_height="wrap_content">

            <TextView
                    android:textColor="@color/darkGreen"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="8dp"
                    android:id="@+id/txtView1"
                    android:background="@android:color/transparent"
                    android:layout_toRightOf="@+id/imgFindSomething"
                    android:text="I need something"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

            <ImageView
                    android:tint="@color/limegreen"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:src="@drawable/find_something"
                    android:id="@+id/imgFindSomething"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_gravity="center"/>

        </RelativeLayout>

        <RelativeLayout
                style="?android:attr/buttonStyleSmall"
                android:id="@+id/relativeLayout3"
                android:clickable="true"
                android:layout_marginRight="10dp"
                android:layout_weight="0.5"
                android:layout_marginLeft="10dp"
                android:background="@drawable/boarder_layout"
                android:layout_width="0dp"
                android:layout_height="wrap_content">

            <TextView
                    android:textColor="@color/crimson"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="8dp"
                    android:id="@+id/txtView2"
                    android:layout_width="wrap_content"
                    android:background="@android:color/transparent"
                    android:layout_toRightOf="@+id/imgInvite"
                    android:text="Invite neighbours"
                    android:layout_height="wrap_content"/>

            <ImageView
                    android:tint="@color/limegreen"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:src="@drawable/add_neighbours"
                    android:id="@+id/imgInvite"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_gravity="center"/>
        </RelativeLayout>
    </LinearLayout>

    <ListView android:layout_width="match_parent" android:layout_height="match_parent"
              android:layout_marginLeft="10dp"
              android:id="@+id/listView">
    </ListView>

</LinearLayout>

You need to use a NestedScrollView like so:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:overScrollMode="never"
    android:clipToPadding="false">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                style="?android:attr/buttonStyleSmall"
                android:clickable="true"
                android:id="@+id/relativeLayout1"
                android:background="@drawable/boarder_layout"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_marginTop="12dp"
                    android:textSize="15sp"
            android:textColor="@color/darkGreen"
            android:layout_marginLeft="8dp"
            android:id="@+id/editTextDate"
            android:background="@android:color/transparent"
            android:layout_toRightOf="@+id/imgProfilePicture"
            android:text="Post a new update"
            android:padding="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <ImageView
            android:padding="8dp"
            android:src="@drawable/images"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:id="@+id/imgProfilePicture"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center"/>
</RelativeLayout>

<LinearLayout 
              android:layout_width="match_parent" 
              android:layout_height="wrap_content"
              android:layout_marginTop="10dp"
              android:orientation="horizontal">

    <RelativeLayout
            style="?android:attr/buttonStyleSmall"
            android:id="@+id/relativeLayout2"
            android:clickable="true"
            android:layout_weight="0.5"
            android:background="@drawable/boarder_layout"
            android:layout_marginLeft="10dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content">

        <TextView
                android:textColor="@color/darkGreen"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="8dp"
                android:id="@+id/txtView1"
                android:background="@android:color/transparent"
                android:layout_toRightOf="@+id/imgFindSomething"
                android:text="I need something"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        <ImageView
                android:tint="@color/limegreen"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:src="@drawable/find_something"
                android:id="@+id/imgFindSomething"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_gravity="center"/>

    </RelativeLayout>

    <RelativeLayout
            style="?android:attr/buttonStyleSmall"
            android:id="@+id/relativeLayout3"
            android:clickable="true"
            android:layout_marginRight="10dp"
            android:layout_weight="0.5"
            android:layout_marginLeft="10dp"
            android:background="@drawable/boarder_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content">

        <TextView
                android:textColor="@color/crimson"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="8dp"
                android:id="@+id/txtView2"
                android:layout_width="wrap_content"
                android:background="@android:color/transparent"
                android:layout_toRightOf="@+id/imgInvite"
                android:text="Invite neighbours"
                android:layout_height="wrap_content"/>

        <ImageView
                android:tint="@color/limegreen"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:src="@drawable/add_neighbours"
                android:id="@+id/imgInvite"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_gravity="center"/>
    </RelativeLayout>
</LinearLayout>

    <ListView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:id="@+id/listView">
    </ListView>


    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

I would also suggest replacing your ListView with a RecyclerView for better performance.

Use this code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/primary"
        android:fitsSystemWindows="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:id="@+id/relativeLayout1"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/boarder_layout"
                android:clickable="true">

                <TextView
                    android:id="@+id/editTextDate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="12dp"
                    android:layout_toRightOf="@+id/imgProfilePicture"
                    android:background="@android:color/transparent"
                    android:padding="10dp"
                    android:text="Post a new update"
                    android:textColor="@color/darkGreen"
                    android:textSize="15sp" />

                <ImageView
                    android:id="@+id/imgProfilePicture"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:padding="8dp"
                    android:src="@drawable/images" />
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:orientation="horizontal">

                <RelativeLayout
                    android:id="@+id/relativeLayout2"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="0.5"
                    android:background="@drawable/boarder_layout"
                    android:clickable="true">

                    <TextView
                        android:id="@+id/txtView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="10dp"
                        android:layout_toRightOf="@+id/imgFindSomething"
                        android:background="@android:color/transparent"
                        android:text="I need something"
                        android:textColor="@color/darkGreen" />

                    <ImageView
                        android:id="@+id/imgFindSomething"
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:layout_gravity="center"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="5dp"
                        android:layout_marginBottom="5dp"
                        android:src="@drawable/find_something"
                        android:tint="@color/limegreen" />

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/relativeLayout3"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="0.5"
                    android:background="@drawable/boarder_layout"
                    android:clickable="true">

                    <TextView
                        android:id="@+id/txtView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="10dp"
                        android:layout_toRightOf="@+id/imgInvite"
                        android:background="@android:color/transparent"
                        android:text="Invite neighbours"
                        android:textColor="@color/crimson" />

                    <ImageView
                        android:id="@+id/imgInvite"
                        android:layout_width="25dp"
                        android:layout_height="25dp"
                        android:layout_gravity="center"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="5dp"
                        android:layout_marginBottom="5dp"
                        android:src="@drawable/add_neighbours"
                        android:tint="@color/limegreen" />
                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"></ListView>

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM