Android: how to tell if a view is scrolling

10,478

Solution 1

I've played around a little bit with a WebView and didn't find a way to track the scrolling, but do you definitely need it to be a WebView? If you're using a ListView or a GridView, you can implement the ListView.OnScrollListener methods, notably onScrollStateChanged and onScroll. The former is particularly useful as it gives you updates on what the list is doing, even for your specific example of the user 'flinging' the list and it continues to scroll for some time afterwards. An example of how you would track that:

public class List13 extends ListActivity implements ListView.OnScrollListener {

(...)

    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
        case OnScrollListener.SCROLL_STATE_IDLE:
            //List is idle
            break;
        case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
            //List is scrolling under the direct touch of the user
            break;
        case OnScrollListener.SCROLL_STATE_FLING:
            //The user did a 'fling' on the list and it's still scrolling
            break;
        }
    }
}

I lifted that straight out of the code samples Google provides, example List13 in the "Api Demos" project. If you want to have a closer look at that, import it into Eclipse by going to File -> New -> Android Project, Create Project from Existing Source, and navigate to (your SDK folder)/platforms/(the version you're working with)/samples/ApiDemos. If you can't find it, you might not have downloaded the sample apps when you set up your SDK. You can do that in Eclipse by going to Window -> Android SDK and AVD Manager, then under "Available Packages" on the left.

Solution 2

Try the following method

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

it will give you the last scrolled position and new scrolled position after scrolling.

Solution 3

I think you might want to do this with a View rather than an Activity. A View has a method named onScrollChanged that sounds like what you want. If that's not it, you might also want to look into the computeScroll, scrollBy and scrollTo methods of the same class.

Share:
10,478
Dave
Author by

Dave

Updated on June 04, 2022

Comments

  • Dave
    Dave almost 2 years

    in iPhone, this would be simple---each view has a scrollViewDidScroll method. I am trying to find the equivalent in Android.

    My code works, but it isn't giving me what I want. I need to execute code the entire duration that a view is scrolling. So, even though I use OnGestureListener's onScroll method, it only fires when the finger is on the screen (it's not really named correctly---it should be called "onSlide" or "onSwipe", focusing on the gesture rather than the UI animation). It does not continue to fire if the user flicks the view and it is still scrolling for a few moments after the user lifts his finger.

    is there a method that is called at every step of the scroll?

    public class Scroll extends Activity implements OnGestureListener {
    
    public  WebView         webview;
    
    public  GestureDetector gestureScanner;
    
    public  int         currentYPosition;
    public  int         lastYPosition;
    
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
    
        webview = new WebView(this);
        setContentView(webview);
        webview.loadUrl("file:///android_asset/Scroll.html");
    
        gestureScanner = new GestureDetector(this);
    
        currentYPosition = 0;
        lastYPosition = 0;
    
    }
    
    
    public boolean onTouchEvent(final MotionEvent me) {
        return gestureScanner.onTouchEvent(me);
    }
    
    
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        // I do stuff here.
        return true;
    }
    
  • Dave
    Dave about 14 years
    Thanks.... these WebViews seem to act completely different than any other Android view. This is the second question that I've asked in this forum that has resulted in: "this would make sense, but WebView seems to be an undocumented exception to normal Android conventions." Extremely frustrating. Beware the WebViews!