In this tutorial we are going to learn how to implement Header and Footer for ListView and RecyclerView. The header is a View that displays before first item and footer displays after last item. Most of the apps you had seen who is having header and footer. Same thing we are going to implement in this tutorial.
1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.
2. Open build.gradle and include this libraries show below:
1 2 3 4 5 6 |
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:design:23.0.1' compile 'com.android.support:recyclerview-v7:23.0.1' } |
3. Open strings.xml located under res=>values folder and add following strings.
1 2 3 4 5 6 7 8 9 10 |
<resources> <string name="app_name">Header Footer Demo</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="header_view">Header View</string> <string name="footer_view">Footer View</string> <string name="list">List</string> <string name="grid">Grid</string> </resources> |
4. Create new xml file naming colors.xml under res=>values folder and add the following colors.
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#D32F2F</color> <color name="colorPrimaryDark">#C62828</color> <color name="textColorPrimary">#FFFFFF</color> <color name="windowBackground">#FFFFFF</color> <color name="navigationBarColor">#000000</color> <color name="colorAccent">#F44336</color> <color name="blue">#03A9F4</color> </resources> |
5. Create activity_main.xml and add the following code. In this i am using Tab Bar Layout for creating tabs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <!-- AppBar Layout --> <android.support.design.widget.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="fill_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> <!-- Tab Layout for creating tabs --> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="fill_parent" android:elevation="5dp" android:layout_height="wrap_content" app:tabIndicatorColor="@android:color/white" /> </android.support.design.widget.AppBarLayout> <!-- Helps handing the Fragments for each Tab --> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> |
6. Create listview_layout.xml for ListView fragment and add ListView to it.
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:dividerHeight="1px" android:padding="1dp" /> |
7. Similarly create recyclerview_layout.xml for RecyclerView fragment and add RecyclerView to it.
1 2 3 4 5 6 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="1px" /> |
8. Now we need to create custom row for both ListView and RecyclerView. So we can use same xml for both, so create new xml file naming list_item_row.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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="wrap_content"> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/row_item_text" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:padding="25dp" android:text="Item" android:textColor="@android:color/white" android:textSize="18sp" /> </LinearLayout> |
9. Now come to MainActivity.java and add the following code for adding tabs and fragments to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.header_footer_demo.activity; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import com.header_footer_demo.R; import com.header_footer_demo.adapters.ViewPagerAdapter; import com.header_footer_demo.tabs.ListView_Fragment; import com.header_footer_demo.tabs.RecyclerView_Fragment; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); setupViewPager(viewPager); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout); tabLayout.setupWithViewPager(viewPager);//setting tab over viewpager } //Setting View Pager private void setupViewPager(ViewPager viewPager) { ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); adapter.addFrag(new ListView_Fragment(), "List View"); adapter.addFrag(new RecyclerView_Fragment(), "Recycler View"); viewPager.setAdapter(adapter); } } |
10. Create new ViewPagerAdapter.java class for ViewPager tab fragments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.header_footer_demo.adapters; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import java.util.ArrayList; import java.util.List; /** * Created by SONU on 22/05/16. */ public class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>();//fragment arraylist private final List<String> mFragmentTitleList = new ArrayList<>();//title arraylist public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } //adding fragments and title method public void addFrag(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Override public CharSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } |
11. Now, for Header and Footer View we need to create new layout naming header_footer_view.xml and you can put any design over here.
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/header_footer_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:gravity="center" android:padding="25dp" android:text="@string/header_view" android:textColor="@android:color/white" android:textSize="18sp" /> |
12. For adding Header and Footer to ListView is very simple because ListView provided us inbuilt method for adding Header and Footer View to it.
1 |
listView.addHeaderView(headerView); |
1 |
listView.addFooterView(footerView); |
Here, footerView is a View that you want to display as ListView Footer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
private void addHeaderFooterView() { LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); //Header View View headerView = inflater.inflate(R.layout.header_footer_view, null, false); TextView headerTitle = (TextView) headerView.findViewById(R.id.header_footer_title); headerTitle.setText(getActivity().getResources().getString(R.string.header_view));//set the text to Header View listView.addHeaderView(headerView);//Add view to list view as header view //Footer View View footerView = inflater.inflate(R.layout.header_footer_view, null, false); TextView footerTitle = (TextView) footerView.findViewById(R.id.header_footer_title); footerTitle.setText(getActivity().getResources().getString(R.string.footer_view));//set the text to Footer View listView.addFooterView(footerView);//Add view to list view as footer view } |
Note: The above method should be call before notifying adapter.
13. Create a ListView_Fragment.java and add the following code to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package com.header_footer_demo.tabs; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.widget.TextView; import com.header_footer_demo.R; import com.header_footer_demo.adapters.CustomAdapter; import java.util.ArrayList; /** * Created by SONU on 22/05/16. */ public class ListView_Fragment extends Fragment { private static View view; private static ListView listView; public ListView_Fragment() { } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.listview_layout, container, false); setUpListView(); return view; } //Set Up list view private void setUpListView() { listView = (ListView) view .findViewById(R.id.list_view); addHeaderFooterView();//Add header and footer before adding data to list view ArrayList<String> arrayList = new ArrayList<>(); for (int i = 1; i <= 20; i++) { arrayList.add("ITEM " + i);//Adding items to recycler view } CustomAdapter adapter = new CustomAdapter( getActivity(), arrayList); listView.setAdapter(adapter);// set adapter on listview adapter.notifyDataSetChanged(); } //Add header and footer view private void addHeaderFooterView() { LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); //Header View View headerView = inflater.inflate(R.layout.header_footer_view, null, false); TextView headerTitle = (TextView) headerView.findViewById(R.id.header_footer_title); headerTitle.setText(getActivity().getResources().getString(R.string.header_view));//set the text to Header View listView.addHeaderView(headerView);//Add view to list view as header view View footerView = inflater.inflate(R.layout.header_footer_view, null, false); TextView footerTitle = (TextView) footerView.findViewById(R.id.header_footer_title); footerTitle.setText(getActivity().getResources().getString(R.string.footer_view));//set the text to Footer View listView.addFooterView(footerView);//Add view to list view as footer view } } |
14. For ListView we need to create CustomAdapter.java class to inflate custom rows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.header_footer_demo.adapters; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.header_footer_demo.R; import com.header_footer_demo.helper.GenerateRandom_Color; import java.util.ArrayList; /** * Created by SONU on 22/05/16. */ public class CustomAdapter extends BaseAdapter { private Context context; private ArrayList<String> arrayList; private LayoutInflater inflater; public CustomAdapter(Context context, ArrayList<String> arrayList) { this.context = context; this.arrayList = arrayList; this.inflater = LayoutInflater.from(context); } @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int position) { return arrayList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = inflater.inflate(R.layout.list_item_row, parent, false); TextView text = (TextView) convertView.findViewById(R.id.row_item_text); text.setBackgroundColor(GenerateRandom_Color.generateRandomColor()); text.setText(arrayList.get(position)); return convertView; } } |
15. For generating random color for row items, we need to create a class that randomly gives range of colors to set row color. For this create GenerateRandom_Color.java and add the following code to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.header_footer_demo.helper; import android.graphics.Color; import java.util.Random; /** * Created by SONU on 22/05/16. */ public class GenerateRandom_Color { //this method will generate random colors public static int generateRandomColor() { Random rnd = new Random(); return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); } } |
16. Now, create RecyclerView_Fragment.java and add the following code to it. In this code we are toggling between List and Grid type RecyclerView.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
package com.header_footer_demo.tabs; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import com.header_footer_demo.R; import com.header_footer_demo.adapters.RecyclerView_Adapter; import java.util.ArrayList; /** * Created by SONU on 22/05/16. */ public class RecyclerView_Fragment extends Fragment { private static View view; private static RecyclerView recyclerView; private boolean isListType = true; public RecyclerView_Fragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true);//call this method to implement option menu over fragment } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.recyclerview_layout, container, false); setRecyclerView(); return view; } //Setting recycler view private void setRecyclerView() { recyclerView = (RecyclerView) view .findViewById(R.id.recycler_view); recyclerView.setHasFixedSize(true); recyclerView .setLayoutManager(new LinearLayoutManager(getActivity()));//Linear Items ArrayList<String> arrayList = new ArrayList<>(); for (int i = 1; i <= 20; i++) { arrayList.add("ITEM " + i);//Adding items to recycler view } RecyclerView_Adapter adapter = new RecyclerView_Adapter(getActivity(), arrayList); recyclerView.setAdapter(adapter);// set adapter on recycler view } //Toggle recycler view between List and Grid Type private void toggleRecyclerView(boolean isList) { isListType = isList;//set the current type is list or not if (isList) recyclerView .setLayoutManager(new LinearLayoutManager(getActivity()));//Linear Items else recyclerView .setLayoutManager(new GridLayoutManager(getActivity(), 2));//Grid Items } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.menu_main, menu);//Inflate menu onPrepareOptionsMenu(menu);//call prepare menu so that title will change according to current type } @Override public void onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); MenuItem item = menu.findItem(R.id.action_toggle);//find menu id //If current type is not list then set to list else by default is grid only if (!isListType) item.setTitle(getActivity().getResources().getString(R.string.list)); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_toggle: //toggle the recycler view type and change the menu text also if (isListType) { item.setTitle(getActivity().getResources().getString(R.string.list)); toggleRecyclerView(false); } else { item.setTitle(getActivity().getResources().getString(R.string.grid)); toggleRecyclerView(true); } break; } return super.onOptionsItemSelected(item); } } |
17. As you have seen in above point we are using Menu for toggling between List and Grid type RecyclerView. For this we need to create menu_mani.xml under menu directory.
1 2 3 4 5 6 7 |
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_toggle" android:title="@string/grid" app:showAsAction="always" /> </menu> |
18. Create DemoViewHolder.java class for RecyclerView item holder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.header_footer_demo.helper; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; import com.header_footer_demo.R; /** * Created by SONU on 22/05/16. */ public class DemoViewHolder extends RecyclerView.ViewHolder { public TextView title; public DemoViewHolder(View view) { super(view); this.title = (TextView) view.findViewById(R.id.row_item_text); } } |
19. Create RecyclerView_HeaderFooter_Holder.java class for RecyclerView Header and Footer holder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.header_footer_demo.helper; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; import com.header_footer_demo.R; /** * Created by SONU on 01/04/16. */ public class RecyclerView_HeaderFooter_Holder extends RecyclerView.ViewHolder { public TextView title; public RecyclerView_HeaderFooter_Holder(View itemView) { super(itemView); title = (TextView) itemView.findViewById(R.id.header_footer_title); } } |
20. For setting Header and Footer in RecyclerView we need to follow some steps because RecyclerView doesn’t provide any methods to do this directly.
1 2 3 4 |
//Type of data in recycler view private static final int TYPE_HEADER = 0; private static final int TYPE_FOOTER = 1; private static final int TYPE_ITEM = 2; |
1 2 3 4 5 |
@Override public int getItemCount() { return (null != arrayList ? arrayList.size() + 2 : 0);//Add 2 more size to array list for Header and Footer } |
1 2 3 4 5 6 7 8 9 10 |
@Override public int getItemViewType(int position) { //Return item type according to requirement if (isPositionHeader(position)) return TYPE_HEADER; else if (isPositionFooter(position)) return TYPE_FOOTER; return TYPE_ITEM; } |
1 2 3 4 |
//if position is 0 then type is header private boolean isPositionHeader(int position) { return position == 0; } |
1 2 3 4 |
//If position is last then type is footer private boolean isPositionFooter(int position) { return position == arrayList.size() + 1; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@Override public RecyclerView.ViewHolder onCreateViewHolder( ViewGroup viewGroup, int viewType) { //inflate the item according to item type View itemView; //Since we are using same holder for both header and footer so we can return same holder if (viewType == TYPE_HEADER || viewType == TYPE_FOOTER) { itemView = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.header_footer_view, viewGroup, false); return new RecyclerView_HeaderFooter_Holder(itemView); } else if (viewType == TYPE_ITEM) { //inflate your layout and pass it to view holder itemView = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.list_item_row, viewGroup, false); return new DemoViewHolder(itemView); } throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");//Some error occurs then exception occurs } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { //If instance is DemoViewHolder the type is Item do your stuff over here if (holder instanceof DemoViewHolder) { ((DemoViewHolder) holder).title.setBackgroundColor(GenerateRandom_Color.generateRandomColor()); ((DemoViewHolder) holder).title.setText(arrayList.get(position - 1));//For getting data from array use -1 for getting correct data } else if (holder instanceof RecyclerView_HeaderFooter_Holder) { //Else the type is header if (position == 0) ((RecyclerView_HeaderFooter_Holder) holder).title.setText(context.getResources().getString(R.string.header_view));//if position is 0 set title to header view if (position == getItemCount()) ((RecyclerView_HeaderFooter_Holder) holder).title.setText(context.getResources().getString(R.string.footer_view));//if position is equal to total item set title to footer view } } |
Note: For getting item from ArrayList for Item Type we need to get it by arrayList.get(position-1) because last item is Footer.
21. Full code of RecyclerView_Adapter.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
package com.header_footer_demo.adapters; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.header_footer_demo.R; import com.header_footer_demo.helper.DemoViewHolder; import com.header_footer_demo.helper.GenerateRandom_Color; import com.header_footer_demo.helper.RecyclerView_HeaderFooter_Holder; import java.util.ArrayList; /** * Created by SONU on 22/05/16. */ public class RecyclerView_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private ArrayList<String> arrayList; private Context context; //Type of data in recycler view private static final int TYPE_HEADER = 0; private static final int TYPE_FOOTER = 1; private static final int TYPE_ITEM = 2; public RecyclerView_Adapter(Context context, ArrayList<String> arrayList) { this.context = context; this.arrayList = arrayList; } @Override public int getItemCount() { return (null != arrayList ? arrayList.size() + 2 : 0);//Add 2 more size to array list for Header and Footer } @Override public int getItemViewType(int position) { //Return item type according to requirement if (isPositionHeader(position)) return TYPE_HEADER; else if (isPositionFooter(position)) return TYPE_FOOTER; return TYPE_ITEM; } //if position is 0 then type is header private boolean isPositionHeader(int position) { return position == 0; } //If position is last then type is footer private boolean isPositionFooter(int position) { return position == arrayList.size() + 1; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { //If instance is DemoViewHolder the type is Item do your stuff over here if (holder instanceof DemoViewHolder) { ((DemoViewHolder) holder).title.setBackgroundColor(GenerateRandom_Color.generateRandomColor()); ((DemoViewHolder) holder).title.setText(arrayList.get(position - 1));//For getting data from array use -1 for getting correct data } else if (holder instanceof RecyclerView_HeaderFooter_Holder) { //Else the type is header if (position == 0) ((RecyclerView_HeaderFooter_Holder) holder).title.setText(context.getResources().getString(R.string.header_view));//if position is 0 set title to header view if (position == getItemCount()) ((RecyclerView_HeaderFooter_Holder) holder).title.setText(context.getResources().getString(R.string.footer_view));//if position is equal to total item set title to footer view } } @Override public RecyclerView.ViewHolder onCreateViewHolder( ViewGroup viewGroup, int viewType) { //inflate the item according to item type View itemView; //Since we are using same holder for both header and footer so we can return same holder if (viewType == TYPE_HEADER || viewType == TYPE_FOOTER) { itemView = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.header_footer_view, viewGroup, false); return new RecyclerView_HeaderFooter_Holder(itemView); } else if (viewType == TYPE_ITEM) { //inflate your layout and pass it to view holder itemView = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.list_item_row, viewGroup, false); return new DemoViewHolder(itemView); } throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");//Some error occurs then exception occurs } } |
22. Finally, all done run your code.
Thanks. 🙂
Subscribe to us and get the latest news.
4 Comments
shadman
Thursday, January 3rd, 2019where did you add footer and header in RecyclerView. I think it should be inside ‘RecyclerView_Fragment.java’, but couldn’t find in there..
Dr. Droid
Thursday, January 3rd, 2019Hi Shadman,
No the header and footer is in RecyclerView_Adapter.java class.
Thanks
Lokesh
Sunday, September 22nd, 2019What if the items are less?
The footer will be attached to the last view. according to the requirement, the footer should be attached to the bottom of the parent
Dr. Droid
Monday, September 23rd, 2019Hi Lokesh,
If items are less then the footer will come after the last item. But if you want to show it at the bottom of the parent then you have to make a custom view and add it to the bottom of the listview.
Thanks