8

I want to keep track of the ScrollY on a Scrollview (no, I can't use a grid or a list)

I am using the ViewTreeObserver with an onScrollChanges Listener to it. However, it gets fired multiple times. Even worse: when it gets called 3 of 4 times, the scrollY of the scrollview is 0 and 1 time I get the correct amount. However, it's not like every 4th call is correct, the moment when the correct value is fired is random, and I need to be able to listen to the scrollposition 0.

Here is the Code I use to observer my Scrollview:

mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { Log.d("StupidScrollView", "Scroll positionY: " + mScrollView.getScrollY()); } }); 

Sample output:

 D/StupidScrollView﹕ Scroll positionY: 0 D/StupidScrollView﹕ Scroll positionY: 0 D/StupidScrollView﹕ Scroll positionY: 0 D/StupidScrollView﹕ Scroll positionY: 77 

I have to mention, I am in a ViewPager and my fragments contain a Scrollview which needs to be observed. It somehow sounds like some issue related to that.

1
  • I am facing the exact same situation, in my custom view I have an ViewTreeObserver, and I need to check getY() value and just like your case, every 2nd or 3rd values is right. Can you guide me here please? Thanks. Commented Jun 26, 2015 at 4:50

2 Answers 2

8

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).

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); } } }; 

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); 
Sign up to request clarification or add additional context in comments.

8 Comments

Hi, thanks for your Answer, but the Default Scrollview has no OnScrollChangedListener. From what package do you import Scrollview? do you maybe have a lib or implemented something on your own? As i can see ProductDetailScrollView is a custom Class so i guess your missing this.
Hi Angelo, yeah that's right. Iam using a custom class called productDetailScrollView. I thought the standard one would have it too, but looking at it now i see i miss some things in my answer.. I will update my answer :)
Thanks, isn't exactly the Solution i went after but pointed me into the right direction!
@Angelo I am facing the exact same situation, in my custom view I have an ViewTreeObserver, and I need to check getY() value and just like your case, every 2nd or 3rd values is right. Can you guide me here ? Thanks.
facing the same. @ShahrozKhan91 have you found a solution?
|
0

I had the same issue. I replaced my ScrollView with android.support.v4.widget.NestedScrollView.

Then implemented NestedScrollView.OnScrollChangeListener.

Attached the Listener to my NestedScrollView.

And now I'm detecting scroll changes. :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.