0

Here is my XML layout. I have a list view inside my scrollview. I am setting the height manually if I change the listview height to wrap_content it is not working. And also if the data grows beyond the limit it is hiding. I am seeing many answers to solve this. What is the best option to solve it for my case?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:focusableInTouchMode="true" >

    <LinearLayout
        android:id="@+id/store_scroller"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="#c6c6c6"
            android:padding="10dp" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                layout_alignParentLeft="true"
                android:text="Stores"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/merchant_count_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="0 Results" />
        </RelativeLayout>
        <Gallery
            android:id="@+id/gallery1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:gravity="left"
            android:listSelector="@color/gridviewlistselector"
            android:paddingLeft="10dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:spacing="10dp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="#c6c6c6"
            android:padding="10dp" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                layout_alignParentLeft="true"
                android:text="Products"
                android:textSize="20dp" />

            <Spinner
                android:id="@+id/filter_spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:layout_toRightOf="@+id/textView1" />

            <TextView
                android:id="@+id/product_count_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="0 Results" />
        </RelativeLayout>

        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="4800dp"
            android:layout_weight="1"
            android:drawSelectorOnTop="true"
            android:listSelector="@color/gridviewlistselector"
            android:orientation="vertical" />
    </LinearLayout>
</LinearLayout>

</ScrollView>
intrepidkarthi
  • 3,104
  • 8
  • 42
  • 75
  • 3
    Please watch Android's principal designers [discuss the best practices](http://www.youtube.com/watch?v=wDBM6wVEO70) with the UI and ListViews, this includes why you should never try to combine Views that scroll in the same direction. – Sam Dec 14 '12 at 17:42
  • The reason is I have a horizontal gallery and a vertical listview inside that. When I scroll up, the total page should be scrolled up including the gallery. How can I solve my case then? – intrepidkarthi Dec 14 '12 at 17:56
  • The only way you can do this is if the gallery and ListView share the screen without scrolling. If one item is too big and both of them aren't visible then, you should focus on a different design entirely. (I understand what you want, but this creates a horrible user experience and it's a unpleasent thing to code.) – Sam Dec 14 '12 at 18:05

2 Answers2

4

Short answer: You can't (shouldn't) have a ListView wrapped in a ScrollView.

Long answer: As Romain Guy (UI-guru at Google themselves) once said:

Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.

Quote via "How can I put a ListView into a ScrollView without it collapsing?".

Community
  • 1
  • 1
karllindmark
  • 6,031
  • 1
  • 26
  • 41
  • The reason is I have a horizontal gallery and a vertical listview inside that. When I scroll up, the total page should be scrolled up including the gallery. How can I solve my case then? – intrepidkarthi Dec 14 '12 at 17:56
  • Try using listView headers and inside the header have a horizontal view pager. – Anand Nandakumar Aug 28 '13 at 00:24
0

Instead using List View in ScrollView you can make separate layout where you can define your desired components. then you can make that layout haeder of the ListView. You can use following code:

LayoutInflater inflater = LayoutInflater.from(this);
View LTop = inflater.inflate(R.layout.header_tolistview, null);
listview.addHeaderView(LTop); 
Naveed Ali
  • 2,609
  • 1
  • 22
  • 37