RecyclerView items don't fill width RecyclerView items don't fill width android android

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

@Overridepublic 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; }


Maybe my answer has a different approach, but it works to occupy the entire width of the screen and not break the elements when passing through the recycling view.

Adapter adapterBalanceInquiry = new Adapter(getActivity(), list);        recyclerView.setHasFixedSize(true);        recyclerView.setNestedScrollingEnabled(false);        recyclerView.setAdapter(adapterBalanceInquiry);        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL, false));        recyclerView.post(new Runnable() {            @Override            public void run() {                try {                    int dx = (recyclerView.getWidth() - recyclerView.getChildAt(0).getWidth());                    recyclerView.scrollBy(-dx, 0);                    LinearSnapHelper snapHelper = new LinearSnapHelper();                    recyclerView.setOnFlingListener(null);                    snapHelper.attachToRecyclerView(recyclerView);                }catch (Exception e){                    e.printStackTrace();                }            }        });

enter image description here