Match_Parent Width Does Not Work in Recyclerview

match_parent width does not work in RecyclerView

In your adapter where you are inflating the item in onCreateViewHolder, is the second parameter of the inflate call null?.

If so change it to parent which is the first parameter in the onCreateViewHolder function signature.

View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, parent, false);

If you need the second parameter to be null then when you get the view reference on inflating, do the following

View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(lp);
return new RecyclerViewHolder(rootView);

Item in RecyclerView not filling it's width match_parent

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);

RecyclerView item width layout_width=“match_parent” does not match parent

I fixed the problem:

1- Get the screen size (width).

2- make the ViewHolder width same as screen size.

Below my code if any one need it.

WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
int height = windowManager.getDefaultDisplay().getHeight();
view.setLayoutParams(new RecyclerView.LayoutParams(width, RecyclerView.LayoutParams.MATCH_PARENT));

RecyclerView items using ConstraintLayout are not filling the entire width of the screen even though match_parent is specified

Your code here:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipeViewHolder {
return RecipeViewHolder(
RecipeListEntryBinding.inflate(
LayoutInflater.from(
parent.context
)
)
)
}

does not inflate the view in the context of the parent view, so some layout parameters will not be treated as you expect. In this case, it can't have a width of match_parent if it doesn't know who its parent is. Change it to:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipeViewHolder {
return RecipeViewHolder(
RecipeListEntryBinding.inflate(
LayoutInflater.from(
parent.context
),
parent,
false
)
)
}

Also, a tip. You don't need to create a companion object just to hold your DiffCallback. And you don't need a property to hold an object either. For cleaner code, instead of

companion object {
private val DiffCallback = object : DiffUtil.ItemCallback<Recipe>() {
//...
}
}

you could put

private object DiffCallback: DiffUtil.ItemCallback<Recipe>() {
//...
}

View in RecyclerView does not match parent width

RecyclerView is very crappy when it comes to properly positioning it and/or its child views.
Try

@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final View v = inflater.inflate(R.layout.item, parent, false);

// match_parent won't work for RecyclerView (sigh)
final ViewGroup.LayoutParams lp = v.getLayoutParams();
lp.width = parent.getWidth();
lp.height = parent.getHeight();
v.setLayoutParams(lp);

return new ViewHolder(v);
}

which basically replaces match_parent for both height & width within the child view.
For your particular case you would only set the width then.

Unable to make RecyclerView Width as match parent

I found it:

All I had to change the following line of code in my custom adapter as follows:

View view = inflater.inflate(R.layout.record_logs_card, null);

to

View view = inflater.inflate(R.layout.record_logs_card, parent, false);`

CardView layout_width=match_parent does not match parent RecyclerView width

The docs for inflate:

Inflate a new view hierarchy from the specified xml resource. Throws
InflateException if there is an error.

Parameters

resource ID for an XML layout resource to load (e.g.,
R.layout.main_page) root

view to be the parent of the
generated hierarchy (if attachToRoot is true), or else simply an
object that provides a set of LayoutParams values for root of the
returned hierarchy (if attachToRoot is false.)

attachToRoot Whether
the inflated hierarchy should be attached to the root parameter? If
false, root is only used to create the correct subclass of
LayoutParams for the root view in the XML. Returns The root View of
the inflated hierarchy. If root was supplied and attachToRoot is true,
this is root; otherwise it is the root of the inflated XML file.

It is important here to not supply true, but do supply the parent:

LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_listitem, parent, false);

Supplying the parent View lets the inflater know what layoutparams to use. Supplying the false parameter tells it to not attach it to the parent just yet. That is what the RecyclerView will do for you.

Recycler View not filling the width

This is because you are added the onCreateViewHolder under the Adapter is as like below

 @Override
public MyRecyclerAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_event_row, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}

Change it as like below:

@Override
public MyRecyclerAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_event_row, parent, false);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}


Related Topics



Leave a reply



Submit