How to have variable row height in recycliewiew

I am trying to create a recyclerview in a navigation box with a heading displaying profile information. I would like to have a header height greater than other line elements. below is my header layout

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="200dp" android:background="@color/green" android:orientation="vertical" android:weightSum="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="56dp" android:orientation="vertical" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:textColor="#ffffff" android:text="XXXXX" android:textSize="32sp" android:textStyle="bold" /> </LinearLayout> </RelativeLayout> 

When I set this to the header of the recycler view, the height of 200dp is not reflected in the user interface

  RecyclerView.Adapter<NavDrawerListViewHolder> adapter = new NavDrawerListAdapter(this,TITLES,this); navList.setAdapter(adapter); 

Below is the adapter for recyclerview:

 public class NavDrawerListAdapter extends RecyclerView.Adapter<NavDrawerListViewHolder> { private static final int TYPE_HEADER = 0; private static final int TYPE_ITEM = 1; Context mContext; String[] mCharacterList; IListItemClickListener mListener; int holderViewType=0; public NavDrawerListAdapter(Context context, String[] characters, IListItemClickListener clickListener) { mContext = context; mCharacterList = characters; mListener = clickListener; } @Override public NavDrawerListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { NavDrawerListViewHolder viewHolder; holderViewType = viewType; if(viewType == TYPE_HEADER) { View v = LayoutInflater.from(mContext).inflate(R.layout.header,null); viewHolder = new NavDrawerListViewHolder(v,TYPE_HEADER,mListener); } else { View v = LayoutInflater.from(mContext).inflate(R.layout.nav_item_row,null); viewHolder = new NavDrawerListViewHolder(v,TYPE_ITEM,mListener); } return viewHolder; } @Override public void onBindViewHolder(NavDrawerListViewHolder holder, int position) { if(holderViewType == TYPE_HEADER) { Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "Roboto-Thin.ttf"); holder.name.setTypeface(typeface); } else { holder.characterName.setText(mCharacterList[position - 1]); } } @Override public int getItemViewType(int position) { if (position == 0) { return TYPE_HEADER; } return TYPE_ITEM; } @Override public int getItemCount() { return mCharacterList.length + 1; } 
+5
source share
1 answer

Replace:

 LayoutInflater.from(mContext).inflate(R.layout.header,null); 

with:

 LayoutInflater.from(mContext).inflate(R.layout.header, parent, false); 

and the same for all your other inflate() calls. If you know the parent container for the bloated layout, always send it to the inflate() call. In particular, RelativeLayout needs this if it is the root view of the layout bloat.

Also, consider using getLayoutInflater() in an Activity instead of LayoutInflater.from() . Maybe if you pass Activity to from() , you get equivalent results. However, in general, you always want to use a LayoutInflater that knows about Activity and its theme to get the right results.

+14
source

All Articles