我想要一个有2个ListView的Fragment。我不希望listView滚动而只是想让整个片段滚动。下面是代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<TextView
android:id="@+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:paddingTop="15dp"
android:text="Monday"
android:textColor="#000"
android:textSize="55sp" />
<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/day"
android:gravity="center_horizontal"
android:text="27 April, 2014"
android:textColor="#000"
android:textSize="45sp" />
<ListView
android:id="@+id/home_list_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/date"
android:layout_margin="10dp"
android:background="@color/list_home_time_bg"
android:divider="@android:color/white"
android:dividerHeight="20.0sp" />
<ListView
android:id="@+id/home_list_stime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/home_list_time"
android:layout_margin="10dp"
android:background="@color/list_home_stime_bg"
android:divider="@android:color/white"
android:dividerHeight="20.0sp" />
</RelativeLayout>
当我运行此代码时,第二个listView获得了自己的滚动条,有没有办法阻止它并使整个片段滚动?
答案 0 :(得分:10)
停止列表视图滚动使用 -
listView.setScrollContainer(false);
滚动片段将其放入ScrollView -
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- everything you already have -->
</ScrollView>
如需更多检查 - how-to-get-a-non-scrollable-listview。
注意:
如果您将 ListView放在ScrollView 中,则所有 ListView 都不会拉伸到其完整高度。以下是解决此问题的方法。
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
要使用此方法,只需在此方法中传递ListView:
ListView list1 = (ListView) view.findViewById(R.id.home_list_time);
setListViewHeightBasedOnChildren(list1);
ListView list2 = (ListView) view.findViewById(R.id.home_list_stime);
setListViewHeightBasedOnChildren(list2);