RecyclerView items don't fill width
When inflating a View
from a LayoutInflater
, you need to pass a parent parameter in order for layout_*
attributes to be used. That's because these attributes need to create the correct LayoutParams
class. That means that you can't use inflate(R.layout.*, null)
, but must instead pass a ViewGroup
for the second parameter. In most cases, you also want to use the three-parameter version of the method and pass false
as the third parameter. If this is omitted or true
then the View
is immediately added to the parent, which causes problems in places like onCreateViewHolder()
because the framework is designed to perform this operation later instead. For more details, see this answer.
In your case, you have the line
View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location,null );
You should change it to
View _v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_location, viewGroup, false );
You should create View
like this
@Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
// create a new view
View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.card_view, viewGroup, false);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}