Skip to main content
added 1323 characters in body
Source Link
Ibrahim Yildirim
  • 2.8k
  • 2
  • 23
  • 31

IFirst Create a custom class that extends ScrollView, and create a listener that has the method onScrollChanged as here:

public class ProductDetailScrollView extends ScrollView { public interface OnScrollChangedListener { void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt); } private OnScrollChangedListener mOnScrollChangedListener; public void setOnScrollChangedListener(OnScrollChangedListener listener) { mOnScrollChangedListener = listener; } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (mOnScrollChangedListener != null) { mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt); } } public ProductDetailHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ProductDetailHorizontalScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ProductDetailHorizontalScrollView(Context context) { super(context); } } 

Then i create a variable (in the activity) mOnScrollChangedListener, and set it as the listener for mScrollView:

private ScrollViewProductDetailScrollView.OnScrollChangedListener mOnScrollChangedListener = new ProductDetailScrollView.OnScrollChangedListener() { public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) { System.out.println("Scrolled: " + t); //Parallax Effect on scroll if (t > 0) { final float newY = (t / 4.0f) * -1.0f; mProductImageContainer.setY(newY); } else { mProductImageContainer.setY(0.0f); } } }; 

I create a variable mOnScrollChangedListener, and set it as the listener for mScrollView:

private ScrollView.OnScrollChangedListener mOnScrollChangedListener = new ProductDetailScrollView.OnScrollChangedListener() { public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) { System.out.println("Scrolled: " + t); //Parallax Effect on scroll if (t > 0) { final float newY = (t / 4.0f) * -1.0f; mProductImageContainer.setY(newY); } else { mProductImageContainer.setY(0.0f); } } }; 

First Create a custom class that extends ScrollView, and create a listener that has the method onScrollChanged as here:

public class ProductDetailScrollView extends ScrollView { public interface OnScrollChangedListener { void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt); } private OnScrollChangedListener mOnScrollChangedListener; public void setOnScrollChangedListener(OnScrollChangedListener listener) { mOnScrollChangedListener = listener; } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (mOnScrollChangedListener != null) { mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt); } } public ProductDetailHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ProductDetailHorizontalScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ProductDetailHorizontalScrollView(Context context) { super(context); } } 

Then i create a variable (in the activity) mOnScrollChangedListener, and set it as the listener for mScrollView:

private ProductDetailScrollView.OnScrollChangedListener mOnScrollChangedListener = new ProductDetailScrollView.OnScrollChangedListener() { public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) { System.out.println("Scrolled: " + t); //Parallax Effect on scroll if (t > 0) { final float newY = (t / 4.0f) * -1.0f; mProductImageContainer.setY(newY); } else { mProductImageContainer.setY(0.0f); } } }; 
Source Link
Ibrahim Yildirim
  • 2.8k
  • 2
  • 23
  • 31

I see your problem, i have had a lot of trouble with scrollviews myself.. Here is my solution to get the scrollvalue in a scrollview (I added a System.out for you).

I create a variable mOnScrollChangedListener, and set it as the listener for mScrollView:

private ScrollView.OnScrollChangedListener mOnScrollChangedListener = new ProductDetailScrollView.OnScrollChangedListener() { public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) { System.out.println("Scrolled: " + t); //Parallax Effect on scroll if (t > 0) { final float newY = (t / 4.0f) * -1.0f; mProductImageContainer.setY(newY); } else { mProductImageContainer.setY(0.0f); } } }; 

The "t" value in my case is how much the scroll have changed vertically, the "l" is the horizontal scroll, and the "oldl" & "oldt" is the old value of t & l so if you need to know that it's scrolled up or down you can use if(oldt > t) etc..

And you set the listener in your scrollview like this:

mScrollView.setOnScrollChangedListener(mOnScrollChangedListener);