Android 如何在scrollview中正确设置listview高度?
我在scrollview中使用了listview,并使用以下代码更新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) {
// 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。