Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using RecyclerView and I'm trying to make the width item of RecyclerView match_parent since I'm using

LinearLayoutManager.HORIZONTAL

RecyclerView items after running

 <android.support.v7.widget.RecyclerView
            android:id="@+id/listOffersHits"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:layout_margin="2dp"
    card_view:cardCornerRadius="2dp">
    <ImageView
        android:id="@+id/offerImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:background="@drawable/item_selector"
        android:scaleType="fitXY"
        android:src="@drawable/offer"
        />

</android.support.v7.widget.CardView>

LayoutManager

   LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL, false);
    listOffersHits.setLayoutManager(linearLayoutManager);

ViewHolder

View view= LayoutInflater.from(parent.getContext())
                .inflate(R.layout.custom_layout, parent, false);
        ViewOffersHolder viewHolder=new ViewOffersHolder(view);
        return viewHolder;
like image 307
Diyaa Avatar asked Aug 25 '15 07:08

Diyaa


People also ask

What is Item view in RecyclerView?

A RecyclerView. ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView.

How implement load more RecyclerView in android?

How is this implemented? Typically in a simple RecyclerView, we load elements to the adapter from a Data Structure. In order to show the loading icon view at the bottom of the RecyclerView, we need to first add a NULL element to the end of the Data Structure.

Which RecyclerView method is called when getting the size of the data set?

getItemCount() : RecyclerView calls this method to get the size of the data set. For example, in an address book app, this might be the total number of addresses. RecyclerView uses this to determine when there are no more items that can be displayed.

Where do you add the android onClick attribute to make items in a RecyclerView respond to clicks select all that apply?

Where do you add the android:onClick attribute to make items in a RecyclerView respond to clicks? In the layout file that displays the RecyclerView, add it to the element. Add it to the layout file for an item in the row.


2 Answers

Calling to GridLayoutManager instead of LinearLayoutManager and put one column is most simple

From your xml:

<androidx.recyclerview.widget.RecyclerView
        app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        app:spanCount="1"
...

Or from Kotlin:

recyclerView.layoutManager = GridLayoutManager(this,1)

Or from Java:

recyclerView.setLayoutManager(new GridLayoutManager (this, 1);
like image 67
JoséAntonio Campillo Avatar answered Sep 21 '22 11:09

JoséAntonio Campillo


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));
like image 33
Diyaa Avatar answered Sep 22 '22 11:09

Diyaa