简体   繁体   English

滚动视图中的Android ListView

[英]Android listview in scrollview

Hi i have try insert listview in scrollview but i have this problem: 嗨,我尝试在scrollview中插入listview,但是我有这个问题: 在此处输入图片说明

the space that scrollview reserve to listview is little, i want that scrollview is than the screen , i want scrollview is visible only when listview becomes larger than the screen How i do? 滚动视图保留给列表视图的空间很小,我希望滚动视图比屏幕小,我希望滚动视图仅在列表视图变得大于屏幕时才可见。我该怎么办?

this is code: 这是代码:

<?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>

How i do this? 我该怎么做?

More solutions can be found here: Android list view inside a scroll view 在此处可以找到更多解决方案: 滚动视图内的Android列表视图

This is a common issue that a lot of developer face. 这是许多开发人员面临的常见问题。 The issue is you are stacking a scrollable view inside another scrollable view. 问题是您正在将一个可滚动视图堆叠在另一个可滚动视图中。 One solution to remove the listview from the scrollview. 一种从滚动视图中删除列表视图的解决方案。

Another is the following code: 另一个是以下代码:

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM