Created
November 13, 2019 09:26
-
-
Save DipaliShah/253859174c083ebd0f8604b69509f528 to your computer and use it in GitHub Desktop.
Add ParallaxVertical Imageview inside in Simple ScrollView
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
1. A Custom ScrollView Class to listen to scroll changes | |
class ParallaxScrollView : ScrollView { | |
private var scrollViewListener: ScrollViewListener? = null | |
interface ScrollViewListener { | |
fun onScrollChanged(scrollView: TestScrollView, x: Int, y: Int, oldx: Int, oldy: Int) | |
} | |
constructor(context: Context) : super(context) | |
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) | |
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, | |
attrs, | |
defStyle) | |
fun setScrollViewListener(scrollViewListener: ScrollViewListener) { | |
this.scrollViewListener = scrollViewListener | |
} | |
override fun onScrollChanged(x: Int, y: Int, oldx: Int, oldy: Int) { | |
super.onScrollChanged(x, y, oldx, oldy) | |
if (scrollViewListener != null) { | |
scrollViewListener!!.onScrollChanged(this, x, y, oldx, oldy) | |
} | |
} | |
} | |
2. Set Listener in your activity or Fragment to listen to the scroll changes | |
scrollView.setScrollViewListener(this) | |
3. Apply Translation Effect on ImageView | |
override fun onScrollChanged(scrollView: TestScrollView, x: Int, y: Int, oldx: Int, oldy: Int) { | |
val scrollY = scrollView.getScrollY() | |
// Add parallax effect | |
image_view.setTranslationY(scrollY * 0.8f) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment