我正在尝试在我的UI底部实现嵌套的ListView,但我的数组中只有一个项目正在填充屏幕。我已经发现,如果我设置了我的高度属性,我可以显示我的所有项目(即" 175dp"代替" Wrap_Content")。问题是列表大小不是静态的,所以我需要在运行时调整ListView的高度。
我已经在互联网上挖掘了StackOverflow,但我似乎无法找到解决方案。
谢谢!
我片段中的代码:
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(rootView.getContext(),
android.R.layout.simple_list_item_1, list);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rootView.findViewById(R.id.list_linear).getLayoutParams();
LinearLayout manager = (LinearLayout) rootView.findViewById(R.id.list_linear);
lp.height = 45 * values.length;
manager.setLayoutParams(lp);
listView.setAdapter(adapter);
我的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BBDEFB"
android:padding="3dp"
android:id="@+id/movie_description_container">
<ImageView
android:layout_width="match_parent"
android:layout_height="225dp"
android:id="@+id/movie_detail_hero_image"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:gravity="top"
android:src="@drawable/sample_1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="movie title"
android:id="@+id/movie_detail_title"
android:layout_alignBottom="@+id/movie_detail_hero_image"
android:backgroundTint="@color/background_floating_material_dark"
android:textSize="36dp"
android:textColor="#BBDEFB"
android:background="#a3000000"
android:layout_above="@id/movie_detail_hero_image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:paddingLeft="18dp"/>
<ImageView
android:layout_width="175dp"
android:layout_height="215dp"
android:id="@+id/movie_detail_poster"
android:src="@drawable/sample_0"
android:layout_below="@+id/movie_detail_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="12dp"
android:gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="date"
android:textSize="22dp"
android:id="@+id/movie_detail_date"
android:layout_centerVertical="true"
android:layout_below="@id/movie_detail_hero_image"
android:layout_alignRight="@+id/movie_detail_hero_image"
android:layout_marginRight="12dp"
android:layout_marginTop="32dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Rating:"
android:textSize="22dp"
android:id="@+id/movie_detail_rating"
android:layout_below="@+id/movie_detail_date"
android:layout_alignRight="@+id/movie_detail_date"
android:layout_alignEnd="@+id/movie_detail_date"
android:layout_marginTop="6dp" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:numStars="4"
android:nestedScrollingEnabled="true"
android:maxWidth="77dp"
android:layout_below="@+id/movie_detail_rating"
android:layout_alignRight="@+id/movie_detail_rating"
android:layout_alignEnd="@+id/movie_detail_rating"
android:layout_marginTop="3dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Synopsis"
android:id="@+id/movie_detail_synopsis_header"
android:layout_below="@+id/movie_detail_poster"
android:layout_alignLeft="@+id/movie_detail_poster"
android:layout_alignStart="@+id/movie_detail_poster" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="description"
android:id="@+id/movie_detail_synopsis"
android:layout_below="@+id/movie_detail_synopsis_header"
android:layout_alignLeft="@+id/movie_detail_synopsis_header"
android:layout_alignStart="@+id/movie_detail_synopsis_header"
android:layout_margin="6dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton_Favorite"
android:layout_below="@+id/ratingBar"
android:layout_alignRight="@+id/ratingBar"
android:src="@drawable/ic_favorite_white_24dp"
android:maxWidth="45dp"
android:maxHeight="45dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_linear"
android:layout_below="@id/movie_detail_synopsis">
<ListView
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/list_view2"/>
</LinearLayout>
答案 0 :(得分:0)
实际上,您可以使用此方法将listview的高度设置为childers的大小
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}