English | 简体中文
Unified fragment visibility library for Android.
Support Fragment usage scenarios:
- Declare the Fragment in the xml file, or dynamically load the Fragment through add or replace in the code.
- Use Fragment in FragmentContainerView/FrameLayout, Use show and hide to control the display and hide Fragment.
- Use Fragment in FragmentContainerView/FrameLayout, Use show and hide to control the display and hide Fragment, and use setMaxLifecycle to control the life cycle of fragment.
- Use Fragment in ViewPager, Behavior is BEHAVIOR_SET_USER_VISIBLE_HINT or BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT.
- Use Fragment in ViewPager2.
- Multi-level nested Fragment.
repositories {
mavenCentral()
}
dependencies {
implementation 'cc.taylorzhang:fragment-visibility:1.0.0'
}
class YourFragment : VisibilityFragment() {
override fun onVisible() {
super.onVisible()
// Called when the fragment is visible.
}
override fun onInvisible() {
super.onInvisible()
// Called when the Fragment is not visible.
}
override fun onVisibleFirst() {
super.onVisibleFirst()
// Called when the fragment is visible for the first time.
// You can load data here for lazy loading.
}
override fun onVisibleExceptFirst() {
super.onVisibleExceptFirst()
// Called when the fragment is visible except first time.
}
}
class YourFragment : VisibilityFragment() {
fun yourFunction() {
if (isVisibleToUser()) {
// do something
}
}
}
Apache license 2.0 © Taylor Zhang