1

My problem is very similar to this question, however unlike it's OP I'm passing the parent to the LayoutInflater. I'm using support library 25.3.1. Here's the layout that I'm inflating (Changing the ConstraintLayout to LinearLayout doesn't help):

<?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>

I've also tried to set the LayoutParams manually in onCreateViewHolder as described here. I thought it worked but from time to time the parent will have a 0 width and height (I'm unable to pinpoint the reason) causing the child to become invisible. debug_screenshot According to this answer the RecyclerView should already me measured by the time onCreateViewHolder is called.

AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
  • I am having the same issue. Did you find a solution? – amp Nov 08 '18 at 14:50
  • Does this answer your question? [View in RecyclerView does not match parent width](https://stackoverflow.com/questions/30251626/view-in-recyclerview-does-not-match-parent-width) – AdamHurwitz Aug 19 '20 at 23:00

1 Answers1

0

If you paste your adapter code, that would have been better, but you can try this.

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

Replace

holder.yourItemBinding.getRoot()

with your root view object if you are not using layout binding.

Debanjan
  • 2,817
  • 2
  • 24
  • 43