1

I want to have a layout that can scroll and a listview inside it. The listview will expand it's height base on how many items in it. Only the ScrollView outside is scrollable.
This is my code:

<ScrollView
            android:id="@+id/layout_box"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

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

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/layout_height_small"
                    android:gravity="center_vertical" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="left"
                        android:layout_marginLeft="@dimen/layout_margin_medium"
                        android:layout_marginTop="@dimen/layout_margin_medium"
                        android:gravity="center"
                        android:text="@string/list_regist_box_content"
                        android:textSize="@dimen/text_size_medium"
                        android:textStyle="bold" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="#ffffff" >

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

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

                    <TextView
                        android:id="@+id/btn_add_regist_box"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="left"
                        android:layout_marginTop="3dp"
                        android:gravity="center"
                        android:paddingBottom="@dimen/layout_margin_medium"
                        android:paddingTop="@dimen/layout_margin_medium"
                        android:text="@string/add_regist_box"
                        android:textColor="#0F88FF"
                        android:textSize="@dimen/text_size_medium" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="left"
                        android:layout_marginLeft="@dimen/layout_margin_medium"
                        android:layout_marginTop="@dimen/layout_margin_large"
                        android:gravity="center"
                        android:text="@string/amount"
                        android:textSize="@dimen/text_size_medium"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/common_row_height"
                    android:background="@drawable/white_bg_grey_border_bottom"
                    android:gravity="center_vertical"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/list_regist_description"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="@dimen/layout_margin_medium"
                        android:layout_marginRight="@dimen/layout_width_medium"
                        android:gravity="center_vertical"
                        android:text="@string/total"
                        android:textSize="@dimen/text_size_medium" />
                </LinearLayout>
</ScrollView>

But the listview is neither expanded nor scrollable.

Please help!

Ngo Van
  • 857
  • 1
  • 21
  • 37
  • 6
    don't put listview inside scrollview. – Raghunandan May 23 '14 at 15:01
  • Liistview is scrollable if it has more items – Top Cat May 23 '14 at 15:03
  • 1
    A `ListView` is scrollable itself – EdmDroid May 23 '14 at 15:07
  • The most reliable solution, IMHO, is to put your other content into rows of the `ListView`, using a suitable adapter (e.g., my `MergeAdapter`) or header/footer views, and get rid of the `ScrollView`. Beyond that, there are many, many duplicates of this question, as can be seen in the "Related" column on the right side of this page. – CommonsWare May 23 '14 at 15:11

3 Answers3

3

Don't put ListView inside ScrollView - first rule of android clud :) Instead you can use simple LinearLayout and manage you ListView items inside it. Or you can add Header/Footer Views to the ListView and using it without scrollview.

Demigod
  • 5,073
  • 3
  • 31
  • 49
0

Actually, it is possible to put a ListView inside of an ScrollView. In some use cases (e.g. dynamic menus/submenus it's a reasonable solution). However, two caveats apply:

  • The ListView won't have scroll. In general, nested scrolling is not possible in Android. There are some hacks to make it work (mostly by using requestDisallowInterceptTouchEvent()) but it's hard to make them work correctly in all cases.
  • As a consequence, you must indicate the exact height the ListView needs to show all items (via its appropriate LayoutParams). Setting WRAP_CONTENT will not work.
matiash
  • 54,791
  • 16
  • 125
  • 154
0

I used below lines of code to scroll list item inside scroll view. It's working fine for my requirement, i hope it will help you.

Thanks, Murali.M

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            //pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
Murali Mohan
  • 65
  • 2
  • 5