Best Android StickyHeader RecyclerView Libraries
There are several options for adding sticky headers in a recyclerview in android. In this article we want to look at some of them with code samples.
(a). SmartHeaderFooterRecyclerView
Easy implements Header & Footer view, Support Liner、Grid、StaggeredLayoutManager, With least modify!
Here are its features:
- No need change anyting with your target adapter
- It does not interfere with your target adapter position.
- It Supports dynamic addition and removal.
- Supports LinearLayoutManager & GridLayoutManager and StaggeredLayoutManager
- No dependencies code build order
Step 1: Installation
Add the library to your project build.gradle:
Step 2: Code
Here is how we use it:
RecyclerView.Adapter targetAdapter = new RecyclerView.Adapter() { ... };
SmartRecyclerAdapter smartRecyclerAdapter = new SmartRecyclerAdapter(targetAdapter);
smartRecyclerAdapter.setFooterView(footerView);
smartRecyclerAdapter.setHeaderView(headerView);
recyclerView.setAdapter(smartRecyclerAdapter);
Links
(b). ShrinkingImageLayout
Android layout with an header image sensible to scroll and touch events.
Step 1: Installation
Install it from jitpack:
Then:
Step 2: Layout
Add XML Layout:
<com.pierfrancescosoffritti.shrinkingimagelayout.ShrinkingImageLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shrinking_image_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Step 3: Code
Then in your code:
shrinkingImageLayout.setupRecyclerView(new RecyclerView(this), new LinearLayoutManager(this), new Adapter(getData()));
To add a header on your recyclerview call the method below:
The header can be any View object.
To get a reference to the header imageview:
On API 21+ the ImageView automatically handles its Z animation when scrolled and touched.
Links
(c). StickyHeaders
Adapter and LayoutManager for Android RecyclerView which enables sticky header positioning. StickyHeaders are section headers in a RecyclerView which are positioned "stickily" to the top of the scrollview during scrolling. A common use-case is an address book where the list of people's last names are grouped into sections by the first letter of the last name, and the header for each section shows that letter..
StickyHeaders uses androidx.recyclerview.* You can use sectioning adapter with a normal androidx.recyclerview.widget.LinearLayoutManager. it works fine, and could be a good way to implement a list like at the root of Android's Settings app.
Step 1: Installation
Install the library using the following statement:
Step 2: Code
To use StickyHeaders, you need to do two things.
- Implement an adapter by subclassing
org.zakariya.stickyheaders.SectioningAdapter
. - Assign a
org.zakariya.stickyheaders.StickyHeaderLayoutManager
to your recyclerview. - When handling modifications to your dataset, never call the
RecyclerView.Adapter::notify
methods, instead, call the corresponding methods inorg.zakariya.stickyheaders.SectioningAdapter::notifySection
. The reason for this is SectioningAdapter maintains internal state, and thenotifySection
methods are tailored for adding and removing sections, adding and removing items from sections, etc.
Links