您好我在scrollview中尝试插入listview但我有这个问题:
滚动视图保留到列表视图的空间很小,我希望滚动视图比屏幕更好,我想滚动视图只有当listview变得比屏幕大时才可见?我是怎么做的?这是代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="@+id/activity_lista__eventi"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="14dp"
android:paddingRight="14dp"
android:paddingTop="@dimen/activity_vertical_margin"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.example.fra87.eudroid.activity_class.Lista_Eventi">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<SearchView
android:layout_width="match_parent"
android:layout_height="50dp"
android:queryHint="Evento"
android:id="@+id/cercaEvento"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/linearLayoutEventi">
<ListView
android:id="@+id/listViewEventi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/cercaEvento"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
我是怎么做到的?
答案 0 :(得分:0)
可在此处找到更多解决方案:Android list view inside a scroll view
这是许多开发人员面临的常见问题。问题是您在另一个可滚动视图中堆叠可滚动视图。从scrollview中删除listview的一种解决方案。
另一个是以下代码:
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();
}