Reputation: 887
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!
Upvotes: 1
Views: 10228
Reputation: 65
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();
}
Upvotes: 0
Reputation: 55370
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:
requestDisallowInterceptTouchEvent()
) but it's hard to make them work correctly in all cases.LayoutParams
). Setting WRAP_CONTENT
will not work.Upvotes: 0
Reputation: 5635
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.
Upvotes: 3