How to Set the Height of an Item Row in Gridlayoutmanager

How to set the height of an item row in GridLayoutManager

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.

How to make an item's height in GridLayoutManager adjust relatively to another item?

I changed my type1 layout to this and now it stretches only as high as its siblings.

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/recommendations_tracks_item_background">

<ImageView
android:id="@+id/cover"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="1"
android:adjustViewBounds="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintLeft_toRightOf="@id/cover"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent">

<TextView
android:id="@+id/title"
style="title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/category_title"
android:textColor="@color/white"
android:gravity="center"
android:maxLines="1"
android:ellipsize="end"/>

<TextView
android:id="@+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/text_list_item_subtitle"
android:textColor="@color/white"
android:gravity="center"
android:maxLines="1"
android:ellipsize="end"/>

</LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

GridLayoutManager gives same height to item in the same lines

You need to use a StaggeredGridLayoutManager to have different heights for each item, ie to have height as wrap content. Replace your GridLayoutManager with the below StaggeredGridLayoutManager

StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);

Sample Image

How to set equal row height in RecyclerView?

You can set the height of each item inside your ViewHolder constructor:

TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, mHeight); 
itemView.setLayoutParams(params);

where mHeight is an integer value of the desired item height.

For your code it'll look like this:

private int mHeight = 100;

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView userName;
public CardView cardView;

public MyViewHolder(View view) {
super(view);
userName = (TextView) view.findViewById(R.id.user_temp);

TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, mHeight);
view.setLayoutParams(params);

}
}


Related Topics



Leave a reply



Submit