Ask Your Question
3

How to include a fixed header to RecyclerView?

asked 2021-07-13 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-10-17 09:00:00 +0000

djk gravatar image

To include a fixed header to RecyclerView, you can follow these steps:

  1. Create a layout file for the header view that you want to add to the RecyclerView. This header layout must be included in the RecyclerView's adapter.

  2. In your RecyclerView's adapter, override the getItemViewType() method to return a unique ID for the header view.

  3. In the onCreateViewHolder() method of the adapter, based on the viewType parameter, you can return a ViewHolder for the header view.

  4. In the onBindViewHolder() method of the adapter, based on the viewType parameter, you can bind data for the header view.

  5. In your RecyclerView's layout file, add a LinearLayout or RelativeLayout containing two child views - your header view and the RecyclerView.

  6. Set the RecyclerView's layoutManager to a LinearLayoutManager.

  7. Override the onScrolled() method of the RecyclerView's scroll listener and check if the first visible item is the header view. If yes, then set the height of the header view to 0 so that it appears fixed.

  8. In the same onScrolled() method, check if the scroll position changes so that the header size can be adjusted again.

With these steps, you can add a fixed header view to your RecyclerView that will remain visible even when the user scrolls through the list.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-13 11:00:00 +0000

Seen: 9 times

Last updated: Oct 17 '21