Skip to content

Instantly share code, notes, and snippets.

@paraya3636
Created July 20, 2017 06:04
Show Gist options
  • Save paraya3636/4f685d32135690028c41bcf4ae8dc6f4 to your computer and use it in GitHub Desktop.
Save paraya3636/4f685d32135690028c41bcf4ae8dc6f4 to your computer and use it in GitHub Desktop.
Change toolbar alpha by ScrollView
// Notify scroll
class ObservableScrollView : ScrollView {
interface OnScrollChangeListener {
fun OnScrollChangeListener(scrollView: ObservableScrollView, x: Int, y: Int, oldX: Int, oldY: Int)
}
var onScrollChangeListener: OnScrollChangeListener? = null
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : super(context, attrs, defStyleAttr)
override fun onScrollChanged(x: Int, y: Int, oldX: Int, oldY: Int) {
super.onScrollChanged(x, y, oldX, oldY)
onScrollChangeListener?.OnScrollChangeListener(this, x, y, oldX, oldY)
}
}
// CustomView
override fun OnScrollChangeListener(scrollView: ObservableScrollView, x: Int, y: Int, oldX: Int, oldY: Int) {
changeToolbarAlpha(y)
}
private fun changeToolbarAlpha(scrollY: Int) {
val offset = DisplayMetricsUtil.dpToPx(context, 140)
val alpha = scrollY.toFloat() / offset
if (alpha in 0..1) {
toolbarBackgroundView.alpha = alpha
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment