Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the height of an item row in GridLayoutManager

Current View is

My Recycler Item which inflate in onCreateViewHolder

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:gravity="center"     android:orientation="vertical"     android:padding="16dp">      <ImageView         android:id="@+id/gridListImageView"         android:layout_width="96dp"         android:layout_height="96dp"         android:src="@drawable/a" />      <TextView         android:id="@+id/gridListView_title"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginTop="20dp"         android:text="Large Text"         android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> 

I want to display something like this Which has one row of half the height of recycler View? And add padding to the rest of the space? Can i do this by GridLayoutManager?

And this is my GridLayoutManager

        GridLayoutManager glm = new GridLayoutManager(getActivity(), 2);         recyclerView.setLayoutManager(glm); 
like image 904
Kunal Avatar asked Feb 05 '16 10:02

Kunal


People also ask

How do you set different height for each item in recycler view?

solution: In the item layout xml, use RelativeLayout as root only and ensure the layout whose child view be dynamically added is surrounded with RelativeLayout too.

What does a Recycler view do?

RecyclerView is the ViewGroup that contains the views corresponding to your data. It's a view itself, so you add RecyclerView into your layout the way you would add any other UI element. Each individual element in the list is defined by a view holder object.

What is RecyclerView adapter in android?

The RecyclerView is a ViewGroup that renders any adapter-based view in a similar way. It is supposed to be the successor of ListView and GridView. One of the reasons is that RecyclerView has a more extensible framework, especially since it provides the ability to implement both horizontal and vertical layouts.

What is RecyclerView kotlin?

A RecyclerView is an advanced version of ListView with improved performance. When you have a long list of items to show you can use RecyclerView. It has the ability to reuse its views. In RecyclerView when the View goes out of the screen or not visible to the user it won't destroy it, it will reuse these views.


2 Answers

When inflating layout for your views in adapter, you can set their height programmatically. In order to evaluate proper height to use you can rely on parent ViewGroup (that is the RecyclerView itself). Here it is a sample:

@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {     View itemView = mLayoutInflater.inflate(R.layout.view_item, parent, false);     // work here if you need to control height of your items     // keep in mind that parent is RecyclerView in this case     int height = parent.getMeasuredHeight() / 4;     itemView.setMinimumHeight(height);     return new ItemViewHolder(itemView);         } 

Hope this could help.

like image 74
thetonrifles Avatar answered Sep 29 '22 15:09

thetonrifles


As of support library 25.1.0, ABOVE ANSWER DOESN'T WORK. I suggest below modifications:

    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {         View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);         GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) v.getLayoutParams();         lp.height = parent.getMeasuredHeight() / 4;         v.setLayoutParams(lp);         return new ViewHolder(v);     } 
like image 34
abedfar Avatar answered Sep 29 '22 17:09

abedfar