【问题标题】:RecyclerView children match_parentRecyclerView 孩子 match_parent
【发布时间】:2017-07-21 12:44:33
【问题描述】:

我的问题与this question 非常相似,但与OP 不同的是,我将parent 传递给LayoutInflater。我正在使用支持库 25.3.1。这是我正在膨胀的布局(将 ConstraintLayout 更改为 LinearLayout 没有帮助):

<?xml version="1.0" encoding="utf-8"?>
<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="72dp"
    android:background="?android:selectableItemBackground"
    android:clickable="true"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp">


    <TextView
        android:id="@+id/list_text_primary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:singleLine="true"
        android:textAppearance="@style/TextAppearance.ListPrimary"
        app:layout_constraintBottom_toTopOf="@+id/list_text_secondary"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        tools:ignore="Deprecated"
        tools:text="list_text_primary"/>

    <TextView
        android:id="@+id/list_text_secondary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textAppearance="@style/TextAppearance.ListSecondary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/list_text_primary"
        tools:ignore="Deprecated"
        tools:text="list_text_secondary"/>


</android.support.constraint.ConstraintLayout>

我还尝试在onCreateViewHolder 中手动设置LayoutParams,如here 所述。我认为它有效,但不时parent 的宽度和高度为 0(我无法查明原因),导致孩子变得不可见。 根据this answer 的说法,RecyclerView 应该在我调用 onCreateViewHolder 的时候已经测量过了。

【问题讨论】:

标签: android android-layout android-recyclerview android-support-library


【解决方案1】:

如果你粘贴你的适配器代码,那会更好,但你可以试试这个。

WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Point size = new Point();
            windowManager.getDefaultDisplay().getSize(size);
            holder.yourItemBinding.getRoot().setLayoutParams(new RecyclerView.LayoutParams(size.x,
                    RecyclerView.LayoutParams.WRAP_CONTENT));

替换

holder.yourItemBinding.getRoot()

如果您不使用布局绑定,请使用您的根视图对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 2014-08-21
    相关资源
    最近更新 更多