Android: Listview inside ScrollView

久未见 提交于 2019-12-31 03:56:10

问题


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!


回答1:


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.




回答2:


You have to just replace your <ScrollView ></ScrollView> with this Custom ScrollView like <com.tmd.utils.VerticalScrollview > </com.tmd.utils.VerticalScrollview >

try this link...https://stackoverflow.com/a/11554684/6334037

Works for me..




回答3:


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.



回答4:


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();
    }


来源:https://stackoverflow.com/questions/23832533/android-listview-inside-scrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!