Item in RecyclerView not filling it's width match_parent

Viewed 13166

I have a problem with my RecyclerView and its child items. They do not extend through the whole android:layout_width="match_parent" nor fill_parent. I've tried both. The thing is, this work perfectly and with no changes, it got ruined somehow.

I am showing this in a FragmentDialog and the child items only expand like wrap_content when I scroll the view up and down, they are fully expanded but as soon as I click on them (I call notifyDataSetChanged()) they shrink again.

Here is a picture of a properly filled item and below it's how they are when they load or when I notifyData. Also the StickHeaderAdapter that I use, does not show headers until I click on one of the items (this work previously and I changed nothing on that part either).

recyclerview child problem

Here is the code for the child row:

<?xml version="1.0" encoding="utf-8"?>
<carbon.widget.LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:carbon_rippleColor="@color/green"
    app:carbon_rippleStyle="background">

<carbon.widget.TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/block_name"
    android:padding="@dimen/block_row_padding"
    android:layout_weight="1"
    android:textSize="@dimen/block_row_text_size"
    android:textColor="@color/black"
    app:carbon_rippleColor="@color/green"
    app:carbon_rippleStyle="background"/>

<RelativeLayout
    android:id="@+id/download_layout"
    android:layout_width="64dp"
    android:layout_height="match_parent"
    android:layout_marginRight="24dp"
    android:layout_marginEnd="24dp"
    android:visibility="gone">


    <carbon.widget.ProgressBar
        android:id="@+id/downloading_bar"
        app:carbon_progressStyle="circular_indeterminate"
        app:carbon_barWidth="5dp"
        app:carbon_tint="@color/green"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_margin="8dp"
        android:visibility="invisible"/>

    <carbon.widget.TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/block_progress_download"
        android:textSize="12sp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:visibility="invisible" />

</RelativeLayout>

And here is the part of the adapter code where I create ViewHolder:

@Override
public BlockAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.block_row, parent, false);
    return new ViewHolder(view);
}

This is the binding code if it helps:

@Override
public void onBindViewHolder(BlockAdapter.ViewHolder holder, int position) {
    if (blockData.isDownloading()) {
        holder.download_layout.setVisibility(View.VISIBLE);
        holder.download_bar.setVisibility(View.VISIBLE);
        if (blockData.getProgress() != null) {
            holder.download_progress.setVisibility(View.VISIBLE);
            holder.download_progress.setText(String.format("%d%%", blockData.getProgress()));
        }
    } else {
        holder.download_layout.setVisibility(View.GONE);
        holder.download_bar.setVisibility(View.GONE);
        holder.download_progress.setVisibility(View.GONE);
    }

    if (selected) {
        holder.itemView.setBackgroundColor(mContext.getResources().getColor(R.color.green));
        holder.block_name.setTextColor(mContext.getResources().getColor(R.color.white));
    } else {
        holder.itemView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        holder.block_name.setTextColor(mContext.getResources().getColor(R.color.black));
    }

}

I don't know why this work in the morning and changed later...

Things I tried:

  • Change match_parent for fill_parent
  • Set RecyclerView.LayoutParams to LayoutParams.MATCH_PARENT
  • Change width programatically
  • Change createViewHolder from ..., parent, false); to ..., null); and add Params seperatelly
  • Logged the width (sometimes it's like 390, when I scroll it gets to 990 and when I click back to 390)
  • Setup StickHeaders sooner, but they are all set when they get created and header is there, it's just invisible.

ANY HELP IS APPRECIATED!

6 Answers

To walkaround this bug I changed:

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity());

with:

recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));

Update your Recycle view dependency to the latest.. able to get rid of that doing soo.. - i updated to 1.3.0

For me, the problem was MATCH_PARENT was not working properly. So what did the trick for me is, just setting item width to its parent's width while inflating the view:

val binding = ItemNewsVerticalBinding.inflate(inflater, parent, false)
binding.root.layoutParams.width = parent.width

I had the same problem. It happens only to some view, and it is reproducible.

Overriding setAutoMeasureEnabled to return false didn't work, for whatever reason.

I use androidx.recyclerview:recyclerview:1.1.0

I worked around this issue by resetting the view's width to match the parent's width, i.e.,

this.itemView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
this.itemView.requestLayout();

at the end of binding the ViewHolder. this.itemView is the ViewHolder's root view in this case, i.e. androidx.recyclerview.widget.RecyclerView.ViewHolder.itemView.

I ran into this problem as well. I was able to fix it simply by avoiding use of "MATCH_PARENT" for width, and instead setting the layout_constraintStart_toStartOf and layout_constraintEnd_toEndOf to "0", and setting the width to match constraints.

Related