1

I created a class that extends ListView so that I can override onScrollChanged so that I can tell is the list has been scrolled up or down. However everytime it gets called, all the values are 0.

Doing this for a regular ScrollView returns me the values I am looking for so does this not work the same as ScrollView?

here is my wrapper class

public class ImageScrollView extends ListView {

    OnScrollChangedListener listener;

    public interface OnScrollChangedListener{
        public void onScrollChangedListener(int l, int t, int oldl, int oldt);
    }

    public ImageScrollView(Context context) {
        super(context);
    }

    public ImageScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onScrollChanged(int l, int t, int oldl, int oldt){

        if(listener != null){
            listener.onScrollChangedListener(l,t,oldl,oldt);
        }
    }
}
tyczj
  • 66,691
  • 50
  • 172
  • 271
  • I think, you should use "AbsListView.OnScrollListener" instead of implementing it explicitly. So you just need to implement it next to your class name. & implement same code inside those overridden methods. – VVB Jul 21 '14 at 11:37

1 Answers1

3

For listview, onScrollChanged always returns all values 0, so does getScrollY() etc, apparently. Depending on what you need, you'll have to implement onScrollListener interface, which will get called back thru onScroll and onScrollStateChanged. None of these functions will give you the actual scroll value, for that you'll have to compute it,

for instance in

onScroll(AbsListView list ...)
{ 
    View c = list.getChildAt(0);

    if (c == null) {
        return;
    }

    //assuming all list items have same height
    int scrolly = -c.getTop() + list.getPaddingTop() + list.getFirstVisiblePosition() * c.getHeight(); 

    // some code
}

see the following post from which i got most answers Android getting exact scroll position in ListView

Community
  • 1
  • 1
tangerine
  • 796
  • 1
  • 7
  • 14