I am trying to create a recyclerview in a nav drawer with the header showing the profile information. I would like to have a header height more than the other row 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 200dp height is not reflecting in the UI
RecyclerView.Adapter<NavDrawerListViewHolder> adapter = new NavDrawerListAdapter(this,TITLES,this);
navList.setAdapter(adapter);
Below is the adapter for the 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;
}
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 layout being inflated, always supply it to the inflate()
call. In particular, RelativeLayout
needs this, if it is the root view of the layout being inflated.
Also, consider using getLayoutInflater()
on the Activity
instead of LayoutInflater.from()
. It may be that if you pass the Activity
into from()
that you will get equivalent results. However, in general, you always want to use a LayoutInflater
that knows about the Activity
and its theme, so you get the right results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With