我正在尝试在 scrollView
中添加 listView
和。
问题是我无法以编程方式设置 ListView
高度并正确重绘 ScrollView
,因为它下面的项目在更改高度之前仍处于旧位置。
XML
<ScrollView
android:id="@+id/movie_detail_scroll_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/movie_detail"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false">
...........
<ListView
android:id="@+id/trails_list_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/movie_videos_container"
android:choiceMode="none"/>
<include
android:id="@+id/movie_reviews_container"
layout="@layout/partial_movie_reviews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/trails_list_view"/>
............
</RelativeLayout>
</ScrollView>
我用来调整 listView 大小的方法:
/**
* Sets ListView height dynamically based on the height of the items.
*
* @param listView to be resized
* @return true if the listView is successfully resized, false otherwise
*/
public static boolean setListViewHeightBasedOnItems(ListView listView, ViewGroup container) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
item.measure(0, 0);
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1);
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight;
listView.setLayoutParams(params);
listView.requestLayout();
//redraw the container layout.
container.requestLayout();
return true;
} else {
return false;
}
}
即使重绘 ScrollView
我该怎么办?
最佳答案
这对聊天 100% 有效,即使元素的高度不同也是如此。
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
float px = 500 * (listView.getResources().getDisplayMetrics().density);
item.measure(View.MeasureSpec.makeMeasureSpec((int) px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Get padding
int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight + totalPadding;
listView.setLayoutParams(params);
listView.requestLayout();
//setDynamicHeight(listView);
return true;
} else {
return false;
}
}
关于android - 如何根据 ScrollView 中的项目设置 ListView 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35115788/