LinearStickyList is a simple StickyHeader UI component based on ScrollView and LinearLayout. It scrolls quickly because there is no rebinding that causes lag when scrolling from off-screen to on-screen, as with RecyclerView. It is best suited for UI requirements where the number of elements in the list is small(at most about 20) and many Views must be placed in each element.
・This project uses AGP8.7.3(Gradle 8.9). Please build with the latest Android Studio.
・If your app uses the latest Android development environment, please install it as described here.
Add the following to the build.gradle file in the project root:
repositories {
maven("https://jitpack.io")
}
Add the following to your application's build.gradle file:
implementation("com.github.s-y-droid:LinearStickyList:v1.0.2")
Step 1: Place the LinearStickyListFragment on your screen and call the setup() method.
linearStickyListFragment.setup(
list = listOf(
P1CellFragment.newInstance(),
P2CellFragment.newInstance(),
// etc..
)
)
The list specifies all the Fragments that make up the LinearStickyListFragment.
Step 2: Implement the cell fragment by inheriting LinearStickyListCellFragmentBase.
class P1CellFragment : LinearStickyListCellFragmentBase() {
companion object {
fun newInstance() = P1CellFragment()
}
override fun isStickyHeader() = true
// etc..
}
Override isStickyHeader(). If you want to make the cell a StickyHeader, set it to true.
For more details, search the source code for "Note #".
Note #1 About setup of LinearStickyListFragment
Note #2 How can a parent safely obtain an instance of a Cell fragment?
Note #3 About Cell fragment implementation
Note #4 How to emit an event from a Cell fragment to its parent
Note #5 Customizing the scrollbar
When calling the setup() method of the LinearStickyListFragment, you can specify scrollbar options.
data class LinearStickyListScrollbarOptions(
val isShowScrollbar : Boolean = true,
val widthDp : Float = 4.0f,
val drawableResId : Int = R.drawable.linear_sticky_list_defalut_scrollbar,
val isFadeOut : Boolean = true,
val fadeOutAlphaAnimationTimeMs : Long = 600L,
val fadeOutInactivityTimeMs : Long = 1200L
)
If you do not specify any options, a design and functionality similar to ScrollView will be displayed. For details on parameters, see "Note #5" in the source code.
LinearStickyListCellFragmentBase has an onDistanceFromDisplayArea() method that can be optionally overridden.
class P1CellFragment : LinearStickyListCellFragmentBase() {
override fun onDistanceFromDisplayArea(
isOnScreen: Boolean,
distancePx: Float
) {
// Implementing trigger detection to free memory and stop/resume animation
}
}
This method is called back from LinearStickyListFragment.
isOnScreen : Whether the cell is visible on screen or not
distancePx : If isOnScreen is false, the distance away from the screen (unit: Px)
A callback will be made to tell you how far it is from the screen. Its purpose is to stop the animation of off-screen Cell or to free memory, etc. Please override if you need it.
I hope this library will help you reduce your development time even a little. We welcome your comments and impressions. If you have any ideas to improve the product, please join us as a contributor.
Have a good development life!