Reputation: 3989
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?
Upvotes: 3
Views: 1320
Reputation: 3989
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);
}
Upvotes: 7
Reputation:
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
Upvotes: 0
Reputation: 6622
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"
Upvotes: 0