Data from the adapter is displayed using the GridLayoutManager.

MainActivity.java

private RecyclerView mRecyclerView; mRecyclerView = (RecyclerView) findViewById(R.id.list); mRecyclerView.setLayoutManager(new GridLayoutManager(this, 7)); 

main.xml

 <android.support.v7.widget.RecyclerView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" android:clipToPadding="false"> </android.support.v7.widget.RecyclerView> 

In item.xml template for item

 <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/cv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" card_view:cardCornerRadius="5dp" card_view:cardElevation="2dp" card_view:contentPadding="10dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="@string/text" android:layout_centerVertical="true" android:textColor="@color/primary_text_default_material_light" android:textSize="16sp"/> </RelativeLayout> </android.support.v7.widget.CardView> 

If you write this way, then the cell will naturally stretch to fit the text, leaving a void at the bottom of the screen.

enter image description here

If you write android:layout_width="match_parent" , one cell will stretch to full screen.

enter image description here

Is it possible to make it so that all the cells fit on one screen and fill in all the spaces on the screen?

  • one
    there is no easy way. You need to calculate the height of the cell in advance, and set the calculated height in onCreateViewHolder to your View - Vladyslav Matviienko
  • The screens are different for everyone. If you pre-set a certain size, then on some screens there will be a void below, and on others it will scroll down - Artsait
  • one
    no, count, for example, when launching an application - Vladyslav Matviienko
  • one
    By GridLayoutManager, I found very little information. I do not understand how this can be done. Can I programmatically change the size of the GridLayoutManager cell? Not in the item.xml file, or do you mean to resize exactly CardView in the adapter for example? - Artsait 2:49 pm
  • one
    Yes, right in the adapter - Vladyslav Matviienko

1 answer 1

Found the answer

  @Override public Holder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = mInflater.inflate(R.layout.item_layout, parent, false); int height = parent.getMeasuredHeight() / 7; itemView.setMinimumHeight(height); return new Holder(itemView); } 

And remove in android.support.v7.widget.RecyclerView padding