RecyclerView children matches_parent

时间:2017-07-21 12:44:33

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

我的问题与this question非常相似,但与OP不同,我将parent传递给LayoutInflater。我正在使用支持库25.3.1。这是我正在膨胀的布局(将ConstarintLayout更改为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>

我还尝试按照here中的LayoutParams手动设置onCreateViewHolder。我认为它有效,但parent有时会有0宽度和高度(我无法确定原因)导致孩子变得不可见。 debug_screenshot根据this answer RecyclerView onCreateViewHolder我应该在调用 'use strict'; var my_image, my_imageSrc = 'https://cdn.spacetelescope.org/archives/images/large/heic1509a.jpg',//https://cdn.spacetelescope.org/archives/images/large/heic1509a.jpg y = document.getElementById('LargeImage'); function PreloadImage(theElement) { my_image = new Image(); my_image.onload = ShowLargeImage(my_imageSrc); my_image.src = my_imageSrc; } function ShowLargeImage(strImgSource) { if (my_image.complete) { y.src = strImgSource; y.style.display = 'block'; } } function ShowThumbnail() { y.style.display = 'none'; }时测量。

1 个答案:

答案 0 :(得分:0)

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

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()

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