android 中的 ScrollView 内的 ListView
ListView inside ScrollView in android
当我 运行 时,listview
位于 scrollview
内,应用列表滚动仅限于第一项。我认为 Scrillview 覆盖了列表视图的滚动,这是我的代码:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:padding="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/client_detail"
android:text="CLIENT INFORMATION"
android:textColor="@color/colorPrimaryDark"
android:background="@drawable/login_selector"/>
<RelativeLayout
android:id="@+id/rl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:visibility="gone"
android:layout_below="@+id/client_detail">
/* HERE IS MY SOME OTHER LAYOUT */
</RelativeLayout>
<Button
android:text="CONTACT DETAILS"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/login_selector"
android:textColor="@color/colorPrimaryDark"
android:layout_below="@+id/rl1"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:id="@+id/contactDetails" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/contactDetails"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
需要一些帮助,在此先感谢!
您需要使用 ExpandableHeightListView。
当您将 ListView
与 ScrollView
一起使用时,只有 ListView
的一行可见,因此您必须根据元素以编程方式设置 ListView 的高度在 ListView
里面。你可以做一个像这样的函数-
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
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, ViewGroup.LayoutParams.WRAP_CONTENT));
}
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
那你在创建的listview上使用这个方法-
HourAdapter adapter1=new HourAdapter(getActivity(),day,start,end,x);
listview.setAdapter(adapter1);
setListViewHeightBasedOnChildren(listview);
即使 ListView
的行具有可变高度
也能正常工作
当我 运行 时,listview
位于 scrollview
内,应用列表滚动仅限于第一项。我认为 Scrillview 覆盖了列表视图的滚动,这是我的代码:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:padding="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/client_detail"
android:text="CLIENT INFORMATION"
android:textColor="@color/colorPrimaryDark"
android:background="@drawable/login_selector"/>
<RelativeLayout
android:id="@+id/rl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:visibility="gone"
android:layout_below="@+id/client_detail">
/* HERE IS MY SOME OTHER LAYOUT */
</RelativeLayout>
<Button
android:text="CONTACT DETAILS"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/login_selector"
android:textColor="@color/colorPrimaryDark"
android:layout_below="@+id/rl1"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:id="@+id/contactDetails" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/contactDetails"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
需要一些帮助,在此先感谢!
您需要使用 ExpandableHeightListView。
当您将 ListView
与 ScrollView
一起使用时,只有 ListView
的一行可见,因此您必须根据元素以编程方式设置 ListView 的高度在 ListView
里面。你可以做一个像这样的函数-
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
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, ViewGroup.LayoutParams.WRAP_CONTENT));
}
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
那你在创建的listview上使用这个方法-
HourAdapter adapter1=new HourAdapter(getActivity(),day,start,end,x);
listview.setAdapter(adapter1);
setListViewHeightBasedOnChildren(listview);
即使 ListView
的行具有可变高度