gpt4 book ai didi

java - RecyclerView Item 不包裹 ListView 的高度

转载 作者:行者123 更新时间:2023-12-02 02:20:06 26 4
gpt4 key购买 nike

所以我有这个 Activity ,如下所示:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jonas.trainingslog1.WorkoutDataActivity">


<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.constraint.ConstraintLayout
...
</android.support.constraint.ConstraintLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/layout">

</android.support.v7.widget.RecyclerView>


</android.support.constraint.ConstraintLayout>

如您所见,该 Activity 有一个 RecyclerView,其项目如下所示:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">


<ListView
android:id="@+id/lst"
android:layout_width="match_parent"
android:layout_height="wrap_content">


</ListView>

<Button
android:layout_width="match_parent"
android:layout_height="30dp"
app:layout_constraintTop_toBottomOf="@+id/lst"/>

ListView的高度在 RecyclerView项目的高度只能与其内容一样高,因此我将其设置为 wrap_content 。问题是,只有当我设置 RecyclerView 的高度时,这才有效。项目布局为match_parent ,一旦我将其更改为 wrap_content ListView 仅显示一项。我怎么也离不开RecyclerView的高度项目为match_parent因为 RecyclerView 的每一项有大量未使用的空间。

谁知道如何解决这个问题?我不明白 xml 文件的这种行为。我非常感谢帮助。

最佳答案

将其添加到您的 ListView 中...动态设置 ListView 的高度

public static void setListViewHeightBasedOnChildren(final ListView listView) {
listView.post(new Runnable() {
@Override
public void run() {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int listWidth = listView.getMeasuredWidth();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(
View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));


totalHeight += listItem.getMeasuredHeight();
Log.d("listItemHeight " + listItem.getMeasuredHeight(), "********");
}

Log.d("totalHeight " + totalHeight, "********");

ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
listView.setLayoutParams(params);
listView.requestLayout();

}
});
}

将其添加到您的 recyclerview 适配器

关于java - RecyclerView Item 不包裹 ListView 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48623828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com