Android: 检测 ScrollView 何时停止滚动

我在 Android 中使用的是 ScrollView,它的可见部分与 Scrollview中的一个单元大小相同。每个“细胞”都是相同的高度。因此,我试图做的是抢到位后,ScrollView已经滚动。

目前,我正在检测当用户已经触摸了 ScrollView和当他们已经开始滚动和工作了从那里,但它是相当错误。它还需要在用户只是轻轻一弹,它就会滚动然后减速时工作。

在 iPhone 上有一个类似于 didDecelerate的函数,当 ScrollView完成滚动时,我可以做任何我想要的代码。Android 有这种东西吗?或者有没有什么代码可以让我找到更好的方法呢?

我查看了 Android 的文档,没有发现任何类似的东西。

97154 次浏览

Try taking a look at this question here on StackOverflow - it's not exactly the same as your question, but it gives an idea on how you can manage the scroll event of a ScrollView.

Basicly you need to create your own CustomScrollView by extending ScrollView and override onScrollChanged(int x, int y, int oldx, int oldy). Then you need to reference this in your layout file instead of the standard ScrollView like com.mypackage.CustomScrollView.

I think this has come up in the past. AFAIK, you can't easily detect that. My suggestion is that you take a look at ScrollView.java (that's how we do things in Android land :)) and figure out how you can extend the class to provide the functionality you are looking for. This is what I would try first:

 @Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
if (mScroller.isFinished()) {
// do something, for example call a listener
}
}
        this.getListView().setOnScrollListener(new OnScrollListener(){
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}


@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if( firstVisibleItem + visibleItemCount >= totalItemCount )
// Last item is shown...


}

Hope the snippet help :)

For a simple case like you described, you can probably get away with overriding fling method in your custom scroll view. Fling method gets called to perform "deceleration" every time user raises his finger from the screen.

So what you should do is something like this:

  1. Subclass ScrollView.

    public class MyScrollView extends ScrollView {
    
    
    private Scroller scroller;
    private Runnable scrollerTask;
    
    
    //...
    
    
    public MyScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    
    
    scroller = new Scroller(getContext()); //or OverScroller for 3.0+
    scrollerTask = new Runnable() {
    @Override
    public void run() {
    scroller.computeScrollOffset();
    scrollTo(0, scroller.getCurrY());
    
    
    if (!scroller.isFinished()) {
    MyScrollView.this.post(this);
    } else {
    //deceleration ends here, do your code
    }
    }
    };
    //...
    }
    }
    
  2. Subclass fling method and DO NOT call superclass implementation.

    @Override
    public void fling(int velocityY) {
    scroller.fling(getScrollX(), getScrollY(), 0, velocityY, 0, 0, 0, container.getHeight());
    post(scrollerTask);
    
    
    //add any extra functions you need from android source code:
    //show scroll bars
    //change focus
    //etc.
    }
    
  3. Fling will not trigger if the user stops scrolling before raising up his finger (velocityY == 0). In case you want to intercept this sort of events aswell, override onTouchEvent.

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
    boolean eventConsumed = super.onTouchEvent(ev);
    if (eventConsumed && ev.getAction() == MotionEvent.ACTION_UP) {
    if (scroller.isFinished()) {
    //do your code
    }
    }
    return eventConsumed;
    }
    

NOTE Although this works, overriding fling method might be a bad idea. It is public, but its barely designed for subclassing. Right now it does 3 things - it initiates fling for private mScroller, handles possible focus changes and shows scroll bars. This might change in future android release. For instance, private mScroller instance changed its class from Scroller to OvershootScroller between 2.3 and 3.0. You have to keep in mind all this small differences. In any case, be ready for unforeseen consequences in the future.

I subclassed (Horizontal)ScrollView and did something like this:

@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
if (Math.abs(x - oldX) > SlowDownThreshold) {
currentlyScrolling = true;
} else {
currentlyScrolling = false;
if (!currentlyTouching) {
//scrolling stopped...handle here
}
}
super.onScrollChanged(x, y, oldX, oldY);
}

I used a value of 1 for the SlowDownThreshold since it always seems to be the difference of the last onScrollChanged event.

In order to make this behave correctly when dragging slowly, I had to do this:

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
currentlyTouching = true;
}
return super.onInterceptTouchEvent(event);
}


@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
currentlyTouching = false;
if (!currentlyScrolling) {
//I handle the release from a drag here
return true;
}
}
return false;
}

I recently had to implement the function you described. What i did was to have a Runnable checking out if the ScrollView had stopped scrolling by comparing the value returned by getScrollY() when the onTouchEvent is first triggered with the value returned after a time defined by the variable newCheck.

See code below (working solution):

public class MyScrollView extends ScrollView{


private Runnable scrollerTask;
private int initialPosition;


private int newCheck = 100;
private static final String TAG = "MyScrollView";


public interface OnScrollStoppedListener{
void onScrollStopped();
}


private OnScrollStoppedListener onScrollStoppedListener;


public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);


scrollerTask = new Runnable() {


public void run() {


int newPosition = getScrollY();
if(initialPosition - newPosition == 0){//has stopped


if(onScrollStoppedListener!=null){


onScrollStoppedListener.onScrollStopped();
}
}else{
initialPosition = getScrollY();
MyScrollView.this.postDelayed(scrollerTask, newCheck);
}
}
};
}


public void setOnScrollStoppedListener(MyScrollView.OnScrollStoppedListener listener){
onScrollStoppedListener = listener;
}


public void startScrollerTask(){


initialPosition = getScrollY();
MyScrollView.this.postDelayed(scrollerTask, newCheck);
}


}

Then i have:

scroll.setOnTouchListener(new OnTouchListener() {


public boolean onTouch(View v, MotionEvent event) {


if (event.getAction() == MotionEvent.ACTION_UP) {


scroll.startScrollerTask();
}


return false;
}
});
scroll.setOnScrollStoppedListener(new OnScrollStoppedListener() {


public void onScrollStopped() {


Log.i(TAG, "stopped");


}
});

BTW i used i few ideas from other replies to do this in my app. Hope this helps. Any questions feel free to ask. Cheers.

My approach for this question is to use a timer to check for the following 2 "events".

1) onScrollChanged() stopped being called

2) User's finger is lift from the scrollview

public class CustomScrollView extends HorizontalScrollView {


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


Timer ntimer = new Timer();
MotionEvent event;


@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
checkAgain();
super.onScrollChanged(l, t, oldl, oldt);
}


public void checkAgain(){
try{
ntimer.cancel();
ntimer.purge();
}
catch(Exception e){}
ntimer = new Timer();
ntimer.schedule(new TimerTask() {


@Override
public void run() {


if(event.getAction() == MotionEvent.ACTION_UP){
// ScrollView Stopped Scrolling and Finger is not on the ScrollView
}
else{
// ScrollView Stopped Scrolling But Finger is still on the ScrollView
checkAgain();
}
}
},100);


}


@Override
public boolean onTouchEvent(MotionEvent event) {
this.event = event;
return super.onTouchEvent(event);
}
}

Here is yet another fix to the, IMHO, missing OnEndScroll event bug in the ScrollView.

Its inspired by hambonious answer. Simply drop this class into your project (change package to match your own) and use the below xml

package com.thecrag.components.ui;


import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;


public class ResponsiveScrollView extends ScrollView {


public interface OnEndScrollListener {
public void onEndScroll();
}


private boolean mIsFling;
private OnEndScrollListener mOnEndScrollListener;


public ResponsiveScrollView(Context context) {
this(context, null, 0);
}


public ResponsiveScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}


public ResponsiveScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}


@Override
public void fling(int velocityY) {
super.fling(velocityY);
mIsFling = true;
}


@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);
if (mIsFling) {
if (Math.abs(y - oldY) < 2 || y >= getMeasuredHeight() || y == 0) {
if (mOnEndScrollListener != null) {
mOnEndScrollListener.onEndScroll();
}
mIsFling = false;
}
}
}


public OnEndScrollListener getOnEndScrollListener() {
return mOnEndScrollListener;
}


public void setOnEndScrollListener(OnEndScrollListener mOnEndScrollListener) {
this.mOnEndScrollListener = mOnEndScrollListener;
}


}

again changing the package name to match your project

<com.thecrag.components.ui.ResponsiveScrollView
android:id="@+id/welcome_scroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/welcome_scroll_command_help_container"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/welcome_header_text_thecrag"
android:layout_margin="6dp">
....
</com.thecrag.components.ui.ResponsiveScrollView>

I've made some improvements to ZeroG's answer. Mainly cancellation of excess task calls and implementing the whole thing as a private OnTouchListener, so all the scroll detection code would be in one place.

Paste the following code into your own ScrollView implementation:

private class ScrollFinishHandler implements OnTouchListener
{
private static final int SCROLL_TASK_INTERVAL = 100;


private Runnable    mScrollerTask;
private int         mInitialPosition = 0;


public ScrollFinishHandler()
{
mScrollerTask = new Runnable() {


public void run() {


int newPosition = getScrollY();
if(mInitialPosition - newPosition == 0)
{//has stopped


onScrollStopped(); // Implement this on your main ScrollView class
}else{
mInitialPosition = getScrollY();
ExpandingLinearLayout.this.postDelayed(mScrollerTask, SCROLL_TASK_INTERVAL);
}
}
};
}


@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_UP)
{


startScrollerTask();
}
else
{
stopScrollerTask();
}


return false;
}


}

And then in your ScrollView implementation:

setOnTouchListener( new ScrollFinishHandler() );

There are some great answers here, but my code can detect when scrolling stops without having to extend ScrollView class. every view instance can call getViewTreeObserver(). when Holding this instance of ViewTreeObserver you can add a OnScrollChangedListener using the function addOnScrollChangedListener().

declare the following:

private ScrollView scrollListener;
private volatile long milesec;


private Handler scrollStopDetector;
private Thread scrollcalled = new Thread() {


@Override
public void run() {
if (System.currentTimeMillis() - milesec > 200) {
//scroll stopped - put your code here
}
}
};

and in your onCreate (or another place) add:

    scrollListener = (ScrollView) findViewById(R.id.scroll);


scrollListener.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {


@Override
public void onScrollChanged() {
milesec = System.currentTimeMillis();
scrollStopDetector.postDelayed(scrollcalled, 200);
}
});

you might want to take longer or slower time between this checks, but when scrolling this listner gets called really fast so it will work very fast.

My approach is determine scrolling state by a timestamp changed each time the onScrollChanged() is called. It's very easy to determine when is start and end of scrolling. You can also change threshold ( I use 100ms ) to fix sensitivity.

public class CustomScrollView extends ScrollView {
private long lastScrollUpdate = -1;


private class ScrollStateHandler implements Runnable {


@Override
public void run() {
long currentTime = System.currentTimeMillis();
if ((currentTime - lastScrollUpdate) > 100) {
lastScrollUpdate = -1;
onScrollEnd();
} else {
postDelayed(this, 100);
}
}
}


@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (lastScrollUpdate == -1) {
onScrollStart();
postDelayed(new ScrollStateHandler(), 100);
}


lastScrollUpdate = System.currentTimeMillis();
}


private void onScrollStart() {
// do something
}


private void onScrollEnd() {
// do something
}


}

Here's my solution which includes scroll tracking and scroll ending:

public class ObservableHorizontalScrollView extends HorizontalScrollView {
public interface OnScrollListener {
public void onScrollChanged(ObservableHorizontalScrollView scrollView, int x, int y, int oldX, int oldY);
public void onEndScroll(ObservableHorizontalScrollView scrollView);
}


private boolean mIsScrolling;
private boolean mIsTouching;
private Runnable mScrollingRunnable;
private OnScrollListener mOnScrollListener;


public ObservableHorizontalScrollView(Context context) {
this(context, null, 0);
}


public ObservableHorizontalScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}


public ObservableHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}


@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = ev.getAction();


if (action == MotionEvent.ACTION_MOVE) {
mIsTouching = true;
mIsScrolling = true;
} else if (action == MotionEvent.ACTION_UP) {
if (mIsTouching && !mIsScrolling) {
if (mOnScrollListener != null) {
mOnScrollListener.onEndScroll(this);
}
}


mIsTouching = false;
}


return super.onTouchEvent(ev);
}


@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);


if (Math.abs(oldX - x) > 0) {
if (mScrollingRunnable != null) {
removeCallbacks(mScrollingRunnable);
}


mScrollingRunnable = new Runnable() {
public void run() {
if (mIsScrolling && !mIsTouching) {
if (mOnScrollListener != null) {
mOnScrollListener.onEndScroll(ObservableHorizontalScrollView.this);
}
}


mIsScrolling = false;
mScrollingRunnable = null;
}
};


postDelayed(mScrollingRunnable, 200);
}


if (mOnScrollListener != null) {
mOnScrollListener.onScrollChanged(this, x, y, oldX, oldY);
}
}


public OnScrollListener getOnScrollListener() {
return mOnScrollListener;
}


public void setOnScrollListener(OnScrollListener mOnEndScrollListener) {
this.mOnScrollListener = mOnEndScrollListener;
}


}

Here is yet another solution, quite simple and clean in my opinion, naturally inspired by answers above. Basically once user ended gesture check if getScrollY() is still changing, after a brief delay (here 50ms).

public class ScrollViewWithOnStopListener extends ScrollView {


OnScrollStopListener listener;


public interface OnScrollStopListener {
void onScrollStopped(int y);
}


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


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


@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
checkIfScrollStopped();
}


return super.onTouchEvent(ev);
}


int initialY = 0;


private void checkIfScrollStopped() {
initialY = getScrollY();
this.postDelayed(new Runnable() {
@Override
public void run() {
int updatedY = getScrollY();
if (updatedY == initialY) {
//we've stopped
if (listener != null) {
listener.onScrollStopped(getScrollY());
}
} else {
initialY = updatedY;
checkIfScrollStopped();
}
}
}, 50);
}


public void setOnScrollStoppedListener(OnScrollStopListener yListener) {
listener = yListener;
}
}

My solution is a variation of Lin Yu Cheng's great solution and also detects when scrolling has started and stopped.

Step 1. Define a HorizontalScrollView and OnScrollChangedListener:

CustomHorizontalScrollView scrollView = (CustomHorizontalScrollView) findViewById(R.id.horizontalScrollView);


horizontalScrollListener = new CustomHorizontalScrollView.OnScrollChangedListener() {
@Override
public void onScrollStart() {
// Scrolling has started. Insert your code here...
}


@Override
public void onScrollEnd() {
// Scrolling has stopped. Insert your code here...
}
};


scrollView.setOnScrollChangedListener(horizontalScrollListener);

Step 2. Add the CustomHorizontalScrollView class:

public class CustomHorizontalScrollView extends HorizontalScrollView {
public interface OnScrollChangedListener {
// Developer must implement these methods.
void onScrollStart();
void onScrollEnd();
}


private long lastScrollUpdate = -1;
private int scrollTaskInterval = 100;
private Runnable mScrollingRunnable;
public OnScrollChangedListener mOnScrollListener;


public CustomHorizontalScrollView(Context context) {
this(context, null, 0);
init(context);
}


public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(context);
}


public CustomHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}


private void init(Context context) {
// Check for scrolling every scrollTaskInterval milliseconds
mScrollingRunnable = new Runnable() {
public void run() {
if ((System.currentTimeMillis() - lastScrollUpdate) > scrollTaskInterval) {
// Scrolling has stopped.
lastScrollUpdate = -1;
//CustomHorizontalScrollView.this.onScrollEnd();
mOnScrollListener.onScrollEnd();
} else {
// Still scrolling - Check again in scrollTaskInterval milliseconds...
postDelayed(this, scrollTaskInterval);
}
}
};
}


public void setOnScrollChangedListener(OnScrollChangedListener onScrollChangedListener) {
this.mOnScrollListener = onScrollChangedListener;
}


public void setScrollTaskInterval(int scrollTaskInterval) {
this.scrollTaskInterval = scrollTaskInterval;
}


//void onScrollStart() {
//    System.out.println("Scroll started...");
//}


//void onScrollEnd() {
//    System.out.println("Scroll ended...");
//}


@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mOnScrollListener != null) {
if (lastScrollUpdate == -1) {
//CustomHorizontalScrollView.this.onScrollStart();
mOnScrollListener.onScrollStart();
postDelayed(mScrollingRunnable, scrollTaskInterval);
}


lastScrollUpdate = System.currentTimeMillis();
}
}
}

this is an old thread but I'd like to add a shorter solution I came up with:

 buttonsScrollView.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->


handler.removeCallbacksAndMessages(null)


handler.postDelayed({
//YOUR CODE TO BE EXECUTED HERE
},1000)
}

Naturally there's a 1000 milliseconds delay. Adjust that if you need to.