Created
August 19, 2020 16:34
-
-
Save yasukotelin/459ea3ee4b4072104c90777c5c0236e0 to your computer and use it in GitHub Desktop.
ViewPager2 in CoordinatorLayoutでBottomのViewを固定するBehavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.util.AttributeSet | |
import android.view.View | |
import androidx.coordinatorlayout.widget.CoordinatorLayout | |
import com.google.android.material.appbar.AppBarLayout | |
class ViewPagerScrollingViewBehavior(context: Context?, attrs: AttributeSet?) : | |
AppBarLayout.ScrollingViewBehavior(context, attrs) { | |
override fun layoutDependsOn( | |
parent: CoordinatorLayout, | |
child: View, | |
dependency: View | |
): Boolean { | |
return dependency is AppBarLayout | |
} | |
override fun onDependentViewChanged( | |
parent: CoordinatorLayout, | |
child: View, | |
dependency: View | |
): Boolean { | |
val appBarLayout = dependency as AppBarLayout | |
val result = super.onDependentViewChanged(parent, child, dependency) | |
val bottomPadding = calculateBottomPadding(appBarLayout) | |
val paddingChanged = bottomPadding != child.paddingBottom | |
if (paddingChanged) { | |
child.setPadding( | |
child.paddingLeft, | |
child.paddingTop, | |
child.paddingRight, | |
bottomPadding | |
) | |
child.requestLayout() | |
} | |
return paddingChanged || result | |
} | |
private fun calculateBottomPadding(dependency: AppBarLayout): Int { | |
val totalScrollRange = dependency.totalScrollRange | |
return totalScrollRange + dependency.top | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考にした記事🙌
https://blog.masterka.net/archives/1882