2

I have a RecyclerView, in which the item views are created by inflating a layout within a custom view, with a ConstraintLayout at its root.

When I set the child view to have width wrap_content it works fine.

But when I set the second child view to have width 0dp ("match_constraint"), strange things happen.

Here's my item layout:

<androidx.constraintlayout.widget.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="wrap_content"
>

<TextView
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="TextView"
    />

<TextView
    android:id="@+id/myTextView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:textColor="@color/text_color_dark_primary"
    android:textSize="15sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/myTextView"
    tools:text="TextView"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

When myTextView2 has width wrap_content, all is good. But when I make it 0dp, all hell breaks loose.

The issue only occurs on the second TextView - the first TextView can have width wrap_content or 0dp, and both work as expected. But when the second TextView has width 0dp, different issues arise:

myTextView: wrap_content, myTextView1: wrap_content - no issue

myTextView: 0dp, myTextView1: wrap_content - no issue

myTextView: 0dp, myTextView1: 0dp - no items show, infinite scroll

myTextView: wrap_content, myTextView1: 0dp - myTextView shows as a small column on the left of the screen, the rest of the width is blank

Are there any fixes?

Luke Needham
  • 3,373
  • 1
  • 24
  • 41
  • try setting left and right constraints as well. – karan Feb 08 '19 at 12:55
  • add this as well: app:layout_constraintBottom_toBottomOf="parent" - @KaranMer the left right constraints are not needed whe you define start/end instead – Nikos Hidalgo Feb 08 '19 at 12:58
  • also try adding `app:layout_constraintWidth_default="wrap"` to your textview after setting width to 0dp – karan Feb 08 '19 at 12:58
  • Adding `app:layout_constraintWidth_default="wrap"` makes the TextView wrap, exactly as it does with width set to `wrap_content`. But I need width to fill the parent – Luke Needham Feb 08 '19 at 13:28

3 Answers3

7

The issue arose because I was using a custom view, instead of inflating a layout. The solution is to programmatically specify layout constraints in onCreateViewHolder, as described here: https://stackoverflow.com/a/48123513/6007104

Specifically:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // no need for a LayoutInflater instance—
    // the custom view inflates itself
    CustomView itemView = new CustomView(parent.getContext());
    // manually set the CustomView's size - this is what I was missing
    itemView.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
    ));
    return new ViewHolder(itemView);
}
Luke Needham
  • 3,373
  • 1
  • 24
  • 41
0

Try this

<?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="wrap_content">

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="TextView" />
</android.support.constraint.ConstraintLayout>

It might be issue with you forgot to add app:layout_constraintBottom_toBottomOf="parent"

Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
0

i tested your layout and reproduce your problem: my solution, hope it works : when you inflate your recyclerview item layout in onCreateViewHolder change null to parent as second parameter override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RvViewHolder { val rootView = inflater.inflate(R.layout.item_temp_recyclerview, parent, false) return RvViewHolder(rootView) }

java_version:

public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int type) {
            View itemView = layoutInflater.inflate(R.layout.recyclerview_item_layout, parent, false);
            return new ViewHolder(itemView);
        }

then you can set width of both child views to 0dp

tip: when you set width to match_constraint or 0dp you don't need app:layout_constraintHorizontal_bias