29

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!

slorangex
  • 1,334
  • 2
  • 12
  • 25

7 Answers7

55

The problem is within the new support library 23.2.0, so I reverted that to 23.1.1 and it works fine. If I find a solution, or what to change, I will let you know, otherwise I'm leaving this thread open, if someone else finds a better answer.

UPDATE

Ok, so I decided it was time to fix this, as we need to move to new support libraries and I finally found an answer.

The problem was/is that the new LayoutManager is using autoMeasure() and somehow it changed all my match_parent to wrap_content, so here is what you need to do, if you encounter a similar problem.

First create LinearLayoutManager llm = new LinearLayoutManager(getActivity());

then llm.setAutoMeasureEnabled(false);

and finally you set the LinearLayoutManager to your RecyclerView, but do this AFTER recyclerView.setAdapter(yourAdapter);

Here is a quick example:

recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setAutoMeasureEnabled(false);
recyclerView.setLayoutManager(llm);
Hasan Abdullah
  • 2,498
  • 1
  • 19
  • 34
slorangex
  • 1,334
  • 2
  • 12
  • 25
  • 1
    I find it a little ridiculous that these `.setLayoutManager()` and `.setAdapter()` ordering bugs exist – Ed Lee Jan 19 '17 at 06:22
  • 1
    This worked for me thanks. Im using RecyclerView 25.2.0 and I set the layout manager first, then setAutoMeasureEnabled(false) and then the adapter which worked – Ersen Osman Mar 10 '17 at 17:26
  • Works, and I set `setAutoMeasureEnabled` before `setAdapter ` also work. My `RecyclerView` version is v26.1.0 – aotian16 Nov 29 '17 at 02:50
  • 5
    Since API v27.1.0 we need to extend the LinearLayoutManager and override the isAutoMeasureEnabled() function to return false because setAutoMeasureEnabled() method is removed. – Milan Apr 27 '18 at 15:59
  • @anqe1ki11er I tried it too. Its not working. So, ended up implementing [this](https://stackoverflow.com/a/49468092/4632372) solution. – Mohammedsalim Shivani Mar 09 '19 at 09:26
22

To walkaround this bug I changed:

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

with:

recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
MeLean
  • 3,092
  • 6
  • 29
  • 43
1

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

RAINA
  • 802
  • 11
  • 22
1

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
0

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.

netzpurist
  • 1,275
  • 1
  • 11
  • 7
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
WavyGravy
  • 131
  • 1
  • 3
0

I tried all the answers, nothing works for me.

After lot of research find the another way to fix this issue.

Create a custom layout manager and set this custom layout manager into your recyclerview.

Step 1: Create custom layout manager

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class FullWidthLinearLayoutManager : LinearLayoutManager {
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, orientation: Int, reverseLayout: Boolean) : super(
        context,
        orientation,
        reverseLayout
    ) {
    }

    override fun checkLayoutParams(lp: RecyclerView.LayoutParams): Boolean {
        lp.width = width
        return true
    }
}

Step 2: Set this layout manager

val layoutManager = FullWidthLinearLayoutManager(context)
layoutManager.orientation = LinearLayoutManager.VERTICAL
yourRecyclerView.layoutManager = layoutManager
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159