Skip to main content
fixed many indents and spaces, removed redundant containers
Source Link
Miha_x64
  • 6.4k
  • 1
  • 45
  • 67
<RelativeLayout<android.support.v7.widget.RecyclerView  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" >  <android.support.v7.widget.RecyclerView  android:id="@+id/my_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> <layout_height="match_parent" /RelativeLayout>> 
<LinearLayout<android.support.v7.widget.CardView  xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"  xmlns:app="http://schemas.android.com/apk/res-auto"> <android.support.v7.widget.CardViewauto" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardElevation="2dp">    <TextView   android:id="@+id/txtHeader"   android:gravity="center"   android:textColor="#000000"   android:textSize="@dimen/abc_text_size_large_material"   android:background="#DCDCDC"   android:layout_width="match_parent"   android:layout_height="50dp" />   </android.support.v7.widget.CardView> </LinearLayout> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:card_view="http://schemas.android.com/apk/res-auto"  xmlns:app="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout  android:layout_width="match_parent"orientation="vertical"   android:layout_height="wrap_content"layout_width="match_parent"   android:orientation="vertical">layout_height="wrap_content">     <android.support.v7.widget.CardView   android:layout_width="match_parent"   android:layout_height="wrap_content"   app:cardElevation="1dp"> <TextView android:id="@+id/txtName" android:text="abc" android:layout_width="match_parent" android:layout_height="wrap_content" />    </android.support.v7.widget.CardView>  </LinearLayout> </LinearLayout> 
public class Header extends ListItem {  private String header; public Header(){}  public String getHeader() {   return header;  }  public void setHeader(String header) {   this.header = header;  } } 
public class ContentItem extends ListItem {  private String name;  private String rollnumber;  @Override  public String getName() {   return name;  }  @Override  public void setName(String name) {   this.name = name;  }  public String getRollnumber() {   return rollnumber;  }  public void setRollnumber(String rollnumber) {   this.rollnumber = rollnumber;  } } 
public class ListItem {  private String name; public ListItem(){}  public String getName() {   return name;  }  public void setName(String name) {   this.name = name;  }  private int id;  public int getId() {   return id;  }  public void setId(int id) {   this.id = id;  } } 
public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {  private static final int TYPE_HEADER = 0;  private static final int TYPE_ITEM = 1;    //Header header;  List<ListItem> list;  public MyRecyclerAdapter(List<ListItem> headerItems)   {   this.list = headerItems; }  @Override  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if LayoutInflater inflater = LayoutInflater.from(viewType==TYPE_HEADERparent.getContext());   if (viewType == TYPE_HEADER) {   View v = LayoutInflater.from(parent.getContext())inflater.inflate(R.layout.header, parent, false);   return new VHHeader(v); }   } else { {  View v = LayoutInflater.from(parent.getContext())inflater.inflate(R.layout.list, parent, false);   return new VHItem(v);   } // return null; throw new IllegalArgumentException();  }  @Override  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)  {  {  if (holder instanceof VHHeader) { {  // VHHeader VHheader = (VHHeader)holder;   Header currentItem = (Header) list.get(position);   VHHeader VHheader = (VHHeader)holder;   VHheader.txtTitle.setText(currentItem.getHeader()); }   } else if (holder instanceof VHItem)  {  ContentItem currentItem = (ContentItem) list.get(position);   VHItem VHitem = (VHItem)holder;   VHitem.txtName.setText(currentItem.getName());   }  }  @Override public int getItemViewType(int position) {   if (isPositionHeader(position))   return TYPE_HEADER;   return TYPE_ITEM;  }  private boolean isPositionHeader(int position)   {     return list.get(position) instanceof Header;    }  @Override  public int getItemCount() {   return list.size();  }    class VHHeader extends RecyclerView.ViewHolder{   TextView txtTitle;   public VHHeader(View itemView) {   super(itemView);   this.txtTitle = (TextView) itemView.findViewById(R.id.txtHeader);   }  }  class VHItem extends RecyclerView.ViewHolder{   TextView txtName;   public VHItem(View itemView) {   super(itemView);   this.txtName = (TextView) itemView.findViewById(R.id.txtName);   }  } } 
public class MainActivity extends AppCompatActivity   {  RecyclerView recyclerView;  List<List<ListItem>> arraylist;  MyRecyclerAdapter adapter;    @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);   recyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);   LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); adapter = new MyRecyclerAdapter(getList()); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(adapter);    }  private ArrayList<ListItem> getList() {   ArrayList<ListItem> arrayList=newarrayList = new ArrayList<>();   for(int j=0;j<=4;j++j = 0; j <= 4; j++) {   Header header=newheader = new Header();   header.setHeader("header"+j);   arrayList.add(header);   for (int i = 0; i <= 3; i++) {   ContentItem item = new ContentItem();   item.setRollnumber(i + "");   item.setName("A" + i);   arrayList.add(item);   }   }  return arrayList;  } } 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" >  <android.support.v7.widget.RecyclerView  android:id="@+id/my_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </RelativeLayout> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardElevation="2dp"> <TextView android:id="@+id/txtHeader" android:gravity="center" android:textColor="#000000" android:textSize="@dimen/abc_text_size_large_material" android:background="#DCDCDC" android:layout_width="match_parent" android:layout_height="50dp" /> </android.support.v7.widget.CardView> </LinearLayout> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout  android:layout_width="match_parent"   android:layout_height="wrap_content"   android:orientation="vertical">   <android.support.v7.widget.CardView   android:layout_width="match_parent"   android:layout_height="wrap_content"   app:cardElevation="1dp"> <TextView android:id="@+id/txtName" android:text="abc" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v7.widget.CardView>  </LinearLayout> </LinearLayout> 
public class Header extends ListItem { private String header; public Header(){} public String getHeader() { return header; } public void setHeader(String header) { this.header = header; } } 
public class ContentItem extends ListItem { private String name; private String rollnumber; @Override public String getName() { return name; } @Override public void setName(String name) { this.name = name; } public String getRollnumber() { return rollnumber; } public void setRollnumber(String rollnumber) { this.rollnumber = rollnumber; } } 
public class ListItem { private String name; public ListItem(){} public String getName() { return name; } public void setName(String name) { this.name = name; } private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } } 
public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private static final int TYPE_HEADER = 0; private static final int TYPE_ITEM = 1; //Header header; List<ListItem> list; public MyRecyclerAdapter(List<ListItem> headerItems)  { this.list = headerItems; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if(viewType==TYPE_HEADER) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false); return new VHHeader(v); }  else {  View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false); return new VHItem(v); } // return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)  {  if(holder instanceof VHHeader) {  // VHHeader VHheader = (VHHeader)holder; Header currentItem = (Header) list.get(position); VHHeader VHheader = (VHHeader)holder; VHheader.txtTitle.setText(currentItem.getHeader()); }  else if(holder instanceof VHItem) {  ContentItem currentItem = (ContentItem) list.get(position); VHItem VHitem = (VHItem)holder; VHitem.txtName.setText(currentItem.getName()); } } public int getItemViewType(int position) { if(isPositionHeader(position)) return TYPE_HEADER; return TYPE_ITEM; } private boolean isPositionHeader(int position)  {   return list.get(position) instanceof Header;   } @Override public int getItemCount() { return list.size(); } class VHHeader extends RecyclerView.ViewHolder{ TextView txtTitle; public VHHeader(View itemView) { super(itemView); this.txtTitle = (TextView)itemView.findViewById(R.id.txtHeader); } } class VHItem extends RecyclerView.ViewHolder{ TextView txtName; public VHItem(View itemView) { super(itemView); this.txtName = (TextView)itemView.findViewById(R.id.txtName); } } } 
public class MainActivity extends AppCompatActivity   { RecyclerView recyclerView; List<List<ListItem>> arraylist; MyRecyclerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView)findViewById(R.id.my_recycler_view); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); adapter = new MyRecyclerAdapter(getList()); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(adapter);   } private ArrayList<ListItem> getList() { ArrayList<ListItem> arrayList=new ArrayList<>(); for(int j=0;j<=4;j++) { Header header=new Header(); header.setHeader("header"+j); arrayList.add(header); for (int i = 0; i <= 3; i++) { ContentItem item = new ContentItem(); item.setRollnumber(i + ""); item.setName("A" + i); arrayList.add(item); } } return arrayList; } } 
<android.support.v7.widget.RecyclerView  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools" android:id="@+id/my_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" /> 
<android.support.v7.widget.CardView  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardElevation="2dp">    <TextView   android:id="@+id/txtHeader"   android:gravity="center"   android:textColor="#000000"   android:textSize="@dimen/abc_text_size_large_material"   android:background="#DCDCDC"   android:layout_width="match_parent"   android:layout_height="50dp" />   </android.support.v7.widget.CardView> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:card_view="http://schemas.android.com/apk/res-auto"  xmlns:app="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content">    <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardElevation="1dp"> <TextView android:id="@+id/txtName" android:text="abc" android:layout_width="match_parent" android:layout_height="wrap_content" />    </android.support.v7.widget.CardView> </LinearLayout> 
public class Header extends ListItem {  private String header;  public String getHeader() {   return header;  }  public void setHeader(String header) {   this.header = header;  } } 
public class ContentItem extends ListItem {  private String name;  private String rollnumber;  @Override  public String getName() {   return name;  }  @Override  public void setName(String name) {   this.name = name;  }  public String getRollnumber() {   return rollnumber;  }  public void setRollnumber(String rollnumber) {   this.rollnumber = rollnumber;  } } 
public class ListItem {  private String name;  public String getName() {   return name;  }  public void setName(String name) {   this.name = name;  }  private int id;  public int getId() {   return id;  }  public void setId(int id) {   this.id = id;  } } 
public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {  private static final int TYPE_HEADER = 0;  private static final int TYPE_ITEM = 1;    //Header header;  List<ListItem> list;  public MyRecyclerAdapter(List<ListItem> headerItems) {   this.list = headerItems; }  @Override  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  LayoutInflater inflater = LayoutInflater.from(parent.getContext());   if (viewType == TYPE_HEADER) {   View v = inflater.inflate(R.layout.header, parent, false);   return new VHHeader(v);   } else { View v = inflater.inflate(R.layout.list, parent, false);   return new VHItem(v);   }  throw new IllegalArgumentException();  }  @Override  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {  if (holder instanceof VHHeader) { // VHHeader VHheader = (VHHeader)holder;   Header currentItem = (Header) list.get(position);   VHHeader VHheader = (VHHeader)holder;   VHheader.txtTitle.setText(currentItem.getHeader());   } else if (holder instanceof VHItem)  ContentItem currentItem = (ContentItem) list.get(position);   VHItem VHitem = (VHItem)holder;   VHitem.txtName.setText(currentItem.getName());   }  }  @Override public int getItemViewType(int position) {   if (isPositionHeader(position))   return TYPE_HEADER;   return TYPE_ITEM;  }  private boolean isPositionHeader(int position) {   return list.get(position) instanceof Header;  }  @Override  public int getItemCount() {   return list.size();  }    class VHHeader extends RecyclerView.ViewHolder{   TextView txtTitle;   public VHHeader(View itemView) {   super(itemView);   this.txtTitle = (TextView) itemView.findViewById(R.id.txtHeader);   }  }  class VHItem extends RecyclerView.ViewHolder{   TextView txtName;   public VHItem(View itemView) {   super(itemView);   this.txtName = (TextView) itemView.findViewById(R.id.txtName);   }  } } 
public class MainActivity extends AppCompatActivity {  RecyclerView recyclerView;  List<List<ListItem>> arraylist;  MyRecyclerAdapter adapter;    @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);   recyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);   LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); adapter = new MyRecyclerAdapter(getList()); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(adapter);  }  private ArrayList<ListItem> getList() {   ArrayList<ListItem> arrayList = new ArrayList<>();   for(int j = 0; j <= 4; j++) {   Header header = new Header();   header.setHeader("header"+j);   arrayList.add(header);   for (int i = 0; i <= 3; i++) {   ContentItem item = new ContentItem();   item.setRollnumber(i + "");   item.setName("A" + i);   arrayList.add(item);   }   }  return arrayList;  } } 
Source Link

Going to show you to make header with items in a Recycler view.Recycler view with Header

Step 1- Add dependency into your gradle file.

compile 'com.android.support:recyclerview-v7:23.2.0' // CardView compile 'com.android.support:cardview-v7:23.2.0' 

Cardview is used for decoration purpose.

Step2- Make three xml files. One for main activity.Second for Header layout.Third for list item layout.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </RelativeLayout> 

header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardElevation="2dp"> <TextView android:id="@+id/txtHeader" android:gravity="center" android:textColor="#000000" android:textSize="@dimen/abc_text_size_large_material" android:background="#DCDCDC" android:layout_width="match_parent" android:layout_height="50dp" /> </android.support.v7.widget.CardView> </LinearLayout> 

list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardElevation="1dp"> <TextView android:id="@+id/txtName" android:text="abc" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v7.widget.CardView> </LinearLayout> </LinearLayout> 

Step 3- Create three bean classes.

Header.java

public class Header extends ListItem { private String header; public Header(){} public String getHeader() { return header; } public void setHeader(String header) { this.header = header; } } 

ContentItem.java

public class ContentItem extends ListItem { private String name; private String rollnumber; @Override public String getName() { return name; } @Override public void setName(String name) { this.name = name; } public String getRollnumber() { return rollnumber; } public void setRollnumber(String rollnumber) { this.rollnumber = rollnumber; } } 

ListItem.java

public class ListItem { private String name; public ListItem(){} public String getName() { return name; } public void setName(String name) { this.name = name; } private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } } 

Step 4- Create an adapter named MyRecyclerAdapter.java

public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private static final int TYPE_HEADER = 0; private static final int TYPE_ITEM = 1; //Header header; List<ListItem> list; public MyRecyclerAdapter(List<ListItem> headerItems) { this.list = headerItems; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if(viewType==TYPE_HEADER) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false); return new VHHeader(v); } else { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false); return new VHItem(v); } // return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if(holder instanceof VHHeader) { // VHHeader VHheader = (VHHeader)holder; Header currentItem = (Header) list.get(position); VHHeader VHheader = (VHHeader)holder; VHheader.txtTitle.setText(currentItem.getHeader()); } else if(holder instanceof VHItem) { ContentItem currentItem = (ContentItem) list.get(position); VHItem VHitem = (VHItem)holder; VHitem.txtName.setText(currentItem.getName()); } } public int getItemViewType(int position) { if(isPositionHeader(position)) return TYPE_HEADER; return TYPE_ITEM; } private boolean isPositionHeader(int position) { return list.get(position) instanceof Header; } @Override public int getItemCount() { return list.size(); } class VHHeader extends RecyclerView.ViewHolder{ TextView txtTitle; public VHHeader(View itemView) { super(itemView); this.txtTitle = (TextView)itemView.findViewById(R.id.txtHeader); } } class VHItem extends RecyclerView.ViewHolder{ TextView txtName; public VHItem(View itemView) { super(itemView); this.txtName = (TextView)itemView.findViewById(R.id.txtName); } } } 

Step 5- In MainActivity add the following code:

public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; List<List<ListItem>> arraylist; MyRecyclerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView)findViewById(R.id.my_recycler_view); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); adapter = new MyRecyclerAdapter(getList()); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(adapter); } private ArrayList<ListItem> getList() { ArrayList<ListItem> arrayList=new ArrayList<>(); for(int j=0;j<=4;j++) { Header header=new Header(); header.setHeader("header"+j); arrayList.add(header); for (int i = 0; i <= 3; i++) { ContentItem item = new ContentItem(); item.setRollnumber(i + ""); item.setName("A" + i); arrayList.add(item); } } return arrayList; } } 

The function getList() is dynamically generating the data for the headers and for list items.