1

I have ListView and ScrollView in one activity. Now, the problem is that ListView and ScollView does not work together. I have many controls in activity so I need to use ListView.

If ScrollView is there then ListView's height decreased as you can see in image.

Please help me to prevent this problem. Problem with ListView and ScrollView together

My Code :

<?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="match_parent"
    android:orientation="vertical"
    android:padding="20dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="20dp" >

        <TextView
            android:id="@+id/tvRQPoint"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textColor="@color/green"
            android:textSize="32sp" />

        <TextView
            android:id="@+id/tvRQQText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/questiontext"
            android:textColor="@color/blue"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tvRQQuestion"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/imgRQImage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:contentDescription="@string/imgdesc" />

        <ListView
            android:id="@+id/lvRQOptions"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:divider="#b5b5b5"
            android:dividerHeight="1dp" />

        <TextView
            android:id="@+id/tvRQAnswer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp" />

        <Button
            android:id="@+id/btnRQNext"
            android:layout_width="fill_parent"
            android:layout_height="35dp"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:text="@string/next" >
        </Button>

        <Button
            android:id="@+id/btnRQEndTest"
            android:layout_width="fill_parent"
            android:layout_height="35dp"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:background="@drawable/blue_bg"
            android:text="@string/endtest"
            android:textColor="@color/white" >
        </Button>
    </LinearLayout>

</ScrollView>
Steph Sharp
  • 11,462
  • 5
  • 44
  • 81
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111
  • 1
    Refer this [android-list-view-inside-a-scroll-view](http://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view) – Chirag Ghori Mar 08 '14 at 10:17
  • @BlackTiger, thank you. Let me try it. – Jeeten Parmar Mar 08 '14 at 10:36
  • @BlackTiger, it is working fine. But now problem is that when any item contains content of 2 or more lines, It hides some part of listview. How to prevent this problem ? – Jeeten Parmar Mar 10 '14 at 08:17
  • See first answer in provided link and try to set this `ListView list = (ListView) view.findViewById(R.id.ls); setListViewHeightBasedOnChildren(list);` – Chirag Ghori Mar 10 '14 at 08:26
  • I said it is working fine by that answer. Problem is that if any Item of listview has content of more than 2 lines, then it is hiding some part of ListView. – Jeeten Parmar Mar 10 '14 at 08:28

4 Answers4

0

Never use a ListView inside a ScrollView. ListView itself already has a built-in scroll so you should use it, as the approach you've used goes against the Android's design.

If you can't remove the ScrollView, you probably should consider simplifying your design or redesign it in another way.

nKn
  • 13,691
  • 9
  • 45
  • 62
0

One thing you can do is use only the listView and add the above elements as a header view, and the below elements as a footer view.

List view has properties: addHeaderView(View view); addFooterView(View view)

Which you can set before setting the list's adapter

Makerhack
  • 231
  • 1
  • 9
0

Hi as @nKn said its not good to use ListView inside ScrollView and it also gives bad performance if worked, ListView has its own scrolling property you do not need to use scrollview in it, create seperate layout for listview and other stuff and align layouts the way u want.It worked very well for me.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:overScrollMode="ifContentScrolls"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical" >

<RelativeLayout
    android:id="@+id/listContainingLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/buttonsLayout"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:overScrollMode="ifContentScrolls"
    android:scrollbarStyle="insideOverlay"
    android:scrollbars="vertical" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" >
    </ListView>
</RelativeLayout>

<LinearLayout
    android:id="@+id/buttonsLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button1" />

    <ImageButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:src="@drawable/button2" />

    <ImageButton
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button3" />
</LinearLayout>

I suggest to align your buttons according to priority if you want your buttons fixed on screen then above format can be used but if u want buttons to come after list then align buttonsLayout belowlistContainingLayout

Hope it helps,Cheers!...

priyanka_rao
  • 465
  • 1
  • 4
  • 20
-1

ListView already has scrolling feature. That's why is make problem inside ScrollView. But try give hardcoded width to your ListView. May this solve problem

<ListView android:id="@+id/lvRQOptions" android:layout_width="fill_parent" android:layout_height="300dp" android:layout_marginTop="8dp" android:divider="#b5b5b5" android:dividerHeight="1dp" />

Mobi
  • 645
  • 3
  • 6
  • 14