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

Leave a Comment