Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Item in RecyclerView not filling it's width match_parent

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!

like image 638
slorangex Avatar asked Mar 09 '16 23:03

slorangex


3 Answers

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);
like image 131
slorangex Avatar answered Nov 07 '22 12:11

slorangex


To walkaround this bug I changed:

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

with:

recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
like image 34
MeLean Avatar answered Nov 07 '22 12:11

MeLean


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

like image 1
RAINA Avatar answered Nov 07 '22 14:11

RAINA