2

I would like to know what is the right way to implement a footer in RecyclerView which can be used to display different views. Few examples are as below

  • View for network error with a button to reload.
  • View when data is loading from server with a progress bar.
  • View when there is no more data available.
  • And so on.

I came across https://stackoverflow.com/a/29168617/371557 which mentions that I have to Override getItemViewType and inflate different views in onCreateViewHolder

What I do not understand is how will RecyclerView know which ViewType it has to request for? How can I force the RecyclerView to show the ViewType?

Community
  • 1
  • 1
rakesh kashyap
  • 1,418
  • 22
  • 41
  • Check this out - http://takeoffandroid.com/android-customview/header-and-footer-layout-for-recylerview/ – Veeresh Charantimath Sep 26 '16 at 12:05
  • That link helped me to understand how to display header and footer. But I still did not understand how to force RecyclerView to show different footer view based on few conditions. – rakesh kashyap Sep 26 '16 at 12:24

1 Answers1

2

RecyclerView is the new view more powerfull than listview, you can make a footer in this but it's for me not the right way. I recommend to create a layout include your RecyclerView and add a dedicated view like Snackbar or a custom footer.

For your question, the RecyclerView knows what to display with the getItemViewType(int positionItem) method.

For example, if the item in position 6 must be shown in the list, getItemViewType return the id of view in onCreateViewHolder(ViewGroup viewGroup, int id) method and recycle the view which could already be used in list for saving memory.

(Sorry for my poor English :)

J-Jamet
  • 847
  • 8
  • 17