Home >Java >javaTutorial >How to Add Headers and Footers to a RecyclerView in Android 5.0?
Adding Header and Footer to a RecyclerView in Android 5.0
The RecyclerView in Android 5.0 provides a convenient way to display a list of items. However, adding headers and footers to the list can be a bit tricky. Here's how you can achieve this:
Adding a Header:
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); headerPlaceHolder = inflater.inflate(R.layout.header_layout, null, false);
layouManager.addView(headerPlaceHolder, 0);
Adding a Footer:
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); footerPlaceHolder = inflater.inflate(R.layout.footer_layout, null, false);
layouManager.addView(footerPlaceHolder);
Considerations for Different LayoutManagers:
GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { if (position == 0 || position == data.size()) { return gridLayoutManager.getSpanCount(); } else { return 1; } } }; gridLayoutManager.setSpanSizeLookup(spanSizeLookup);
Note:
By following these steps, you can easily add headers and footers to your RecyclerView in Android 5.0 and above.
The above is the detailed content of How to Add Headers and Footers to a RecyclerView in Android 5.0?. For more information, please follow other related articles on the PHP Chinese website!