The property of CardView layout_width="match_parent" is not taken into account

0

I am implementing an application where I show information from a database in a RecyclerView and in the use CardView (This RecyclerView is in a fragment), my problem is that the CardView does not respect the property "match_parent" as shown in the image

My CardView code is this:

<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/cardView"  
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:clickable="true"
        android:focusable="true"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardBackgroundColor="#e2e2e2"
        card_view:layout_constraintEnd_toEndOf="parent"
        card_view:layout_constraintStart_toStartOf="parent"
        card_view:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.CardView>

Searching the forum I found some related questions where they said that I would add a parent to the CardView, by doing it solves the problem but that does not have onclick every element of the ReciclerView and I do not understand why.

Another solution I found was adding:

WindowManager windowManager = (WindowManager)v.getContext().getSystemService(Context.WINDOW_SERVICE);
        int width = windowManager.getDefaultDisplay().getWidth();
        v.setLayoutParams(new RecyclerView.LayoutParams(width, RecyclerView.LayoutParams.MATCH_PARENT));

This also solved me halfway since I remove the margin of the CardView as shown in the image

After that I tried to add margin to the RecyclerView by inflating the view but it was a failure.

I would really appreciate it if someone could help me out, thanks ...

    
asked by Alberto zt 10.04.2018 в 06:22
source

1 answer

0

In the onCreateViewHolder method of your Adapter, you probably have something like this:

    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_participant,null,false);

Change the null to parent so it looks like this:

view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_participant,parent,false);
    
answered by 10.12.2018 в 03:42