Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在scrollview中正确设置listview高度?_Android_Listview - Fatal编程技术网

Android 如何在scrollview中正确设置listview高度?

Android 如何在scrollview中正确设置listview高度?,android,listview,Android,Listview,我在scrollview中使用了listview,并使用以下代码更新listview高度: // method to expand the height of listview public void updateListViewHeight(ListView list) { CustomeAdapter myListAdapter = (CustomeAdapter) list.getAdapter(); if (myListAdapter == null) {

我在scrollview中使用了listview,并使用以下代码更新listview高度:

// method to expand the height of listview
public void updateListViewHeight(ListView list) {
    CustomeAdapter myListAdapter = (CustomeAdapter) list.getAdapter();
    if (myListAdapter == null) {
        return;
    }
    // get listview height
    int totalHeight = 0;
    // int adapterCount = myListAdapter.getCount();
    for (int size = 0; size < Math.max(shoutsList.size(), postsList.size()); size++) {
        View listItem = myListAdapter.getView(size, null, list);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    // Change Height of ListView
    ViewGroup.LayoutParams params = list.getLayoutParams();
    params.height = totalHeight
            + (list.getDividerHeight() * (shoutsList.size() - 1));
    list.setLayoutParams(params);
}

问题是,当用户向下滚动时,我从服务器请求新数据,但当我更新listview高度时,它会自动滚动并自动请求数据。有一个问题,为什么在ScrollView中使用listview?ListView本身是可滚动的…实际上,ListView在片段中,我将其包含在scrollview中。我不想编辑这个片段,那么,我可以用什么替代listview?RecyclerView不会遇到与listview相同的问题。它允许包裹内容高度,并确保固定高度属性设置为false。