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.