Recycleview not showing match parent

Viewed 166

XML:

<androidx.recyclerview.widget.RecyclerView
   android:visibility="visible"
   android:id="@+id/itemshowRecylerview"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:listitem="@layout/singleitemforrecylerview"/>

Java Code:

itemshowRecylerview.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        itemshowRecylerview.setHasFixedSize(true);

Viewholder Code:

 @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.singleitemforrecylerview, null,false);
        return new ItemDetailsAdapter.ViewHolder(view);
    }

enter image description here

2 Answers

You need to change your custom adapter class, pass parent instead of null in your inflate method

@NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.singleitemforrecylerview, parent,false);
        return new ItemDetailsAdapter.ViewHolder(view);
    }

You have use this type of RecyclerView set orientation and layoutManager in RecyclerView and working code shown in below.

  <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
           app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                        app:spanCount="1">
                    </androidx.recyclerview.widget.RecyclerView>
       recyclerView.setAdapter(yourAdapter);
Related