Android 在scrollview中添加listview

Android 在scrollview中添加listview,android,listview,scrollview,android-linearlayout,android-relativelayout,Android,Listview,Scrollview,Android Linearlayout,Android Relativelayout,我想有一个滚动列表,其中还包括列表视图。我到处都做了研究,但还没有找到解决方案:根据下面的代码,当我添加ScrollView时,listview的高度变小了。我希望ListView的高度是基于内容的动态的,并且使整个页面可以滚动。我如何做到这一点?或者有没有办法创建动态线性/相对列表UI?请建议 XML代码: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.andr

我想有一个滚动列表,其中还包括列表视图。我到处都做了研究,但还没有找到解决方案:根据下面的代码,当我添加ScrollView时,listview的高度变小了。我希望ListView的高度是基于内容的动态的,并且使整个页面可以滚动。我如何做到这一点?或者有没有办法创建动态线性/相对列表UI?请建议

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="@dimen/activity_horizontal_margin"
    android:orientation="vertical" >

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin">

        <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/kollege_name"
        android:width="170dp" />

        <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="right"
        android:text="@string/grade_value"
        android:width="120dp" />

        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:text="@string/code_value" />
        </RelativeLayout>

        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />


        <TextView
        android:id="@+id/textView4"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/your_fit"
        android:textSize="12sp" />

        <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin" >

        <TextView
            android:id="@+id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tuition"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"

            android:text="@string/tuition_value" />

        </RelativeLayout>


        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin" >

        <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/placements"
        android:textSize="12sp" />

        <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView12"
        android:text="@string/EXTC"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView13"
        android:text="@string/IT"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView14"
        android:text="@string/COMP"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView15"
        android:text="@string/MECH"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView16"
        android:text="@string/INST"
        android:layout_marginTop="3dp" />
        </RelativeLayout>

        <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@android:color/darker_gray" />


        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin" >

        <TextView
        android:id="@+id/textView18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/placements"
        android:textSize="12sp" />

        <TextView
        android:id="@+id/textView19"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView18"
        android:text="@string/EXTC"
        android:layout_marginTop="3dp" />

        <TextView
        android:id="@+id/textView20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView19"
        android:text="@string/IT"
        android:layout_marginTop="3dp" />
        </RelativeLayout>
        </LinearLayout>

        </ScrollView>

</LinearLayout>

您不应该将listview放在scrollview中。让您的listview取消滚动视图,或将其他视图作为页眉和页脚添加到listview

从文件中引用

永远不要将ScrollView与ListView一起使用,因为ListView负责自己的垂直滚动。最重要的是,这样做会破坏ListView中处理大型列表的所有重要优化,因为它有效地迫使ListView显示其整个项目列表,以填充ScrollView提供的无限容器

您可以将视图作为页眉和页脚添加到listview中。页眉和页脚将滚动


您不应该将listview放在scrollview中。让您的listview取消滚动视图,或将其他视图作为页眉和页脚添加到listview

从文件中引用

永远不要将ScrollView与ListView一起使用,因为ListView负责自己的垂直滚动。最重要的是,这样做会破坏ListView中处理大型列表的所有重要优化,因为它有效地迫使ListView显示其整个项目列表,以填充ScrollView提供的无限容器

您可以将视图作为页眉和页脚添加到listview中。页眉和页脚将滚动


您不应将listview与在滚动视图中一起使用。因为每个视图都有自己的滚动。我也会遇到此问题。如果您想在布局中同时设置这两个视图,只需设置listview权重。

您不应将listview与在滚动视图中一起使用。因为每个视图都有自己的滚动。我也会遇到此问题。如果您想在布局中同时设置这两个视图,只需您可以设置listview权重。

最好在listview适配器内设置不同的视图类型,或者使用listview页脚和页眉

您还可以使用此方法解决ScrollView中ListView面临的问题

public static void updateListViewHeight(ListView myListView) {
     ListAdapter myListAdapter = myListView.getAdapter();
     if (myListAdapter == null) {            
              return;
     }
    //get listview height
    int totalHeight = 0;
    int adapterCount = myListAdapter.getCount()
    for (int size = 0; size < adapterCount ; size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    //Change Height of ListView 
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
    myListView.setLayoutParams(params);
}

最好在ListView适配器内设置不同的视图类型,或者使用ListView页脚和页眉

您还可以使用此方法解决ScrollView中ListView面临的问题

public static void updateListViewHeight(ListView myListView) {
     ListAdapter myListAdapter = myListView.getAdapter();
     if (myListAdapter == null) {            
              return;
     }
    //get listview height
    int totalHeight = 0;
    int adapterCount = myListAdapter.getCount()
    for (int size = 0; size < adapterCount ; size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    //Change Height of ListView 
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
    myListView.setLayoutParams(params);
}

我很确定你不必在scrollView中包装listView。我很确定你不必在scrollView中包装listView。嗨,Satheesh,我是Android的新手。你能帮我解决XML文件中的问题吗?谢谢…嗨,Satheesh,我对安卓相当陌生。你能帮我解决XML文件中的问题吗?谢谢…这个脚本解决了这个问题。顺便问一下,这个脚本是否适用于所有android API,还是特定于某些API?我需要使应用程序从SDK-10开始兼容。它应该可以正常工作,但请记住,ListView将始终是固定视图,并在设置适配器时完全加载。这不适用于某些行不同的ListView。例如:我有一个listview,在adapter中,我更改第一行的行布局。对于我的lw,此方法无法正常工作。因此,当列表项的TextView中有多行文本时,我还需要一个解决方案。急需解决方案。此脚本解决了此问题。顺便问一下,这个脚本是否适用于所有android API,还是特定于某些API?我需要使应用程序从SDK-10开始兼容。它应该可以正常工作,但请记住,ListView将始终是固定视图,并在设置适配器时完全加载。这不适用于某些行不同的ListView。例如:我有一个listview,在adapter中,我更改第一行的行布局。对于我的lw,此方法无法正常工作。因此,当列表项的TextView中有多行文本时,我还需要一个解决方案。我们急需一个解决方案。