3

With HorizontalScrollView, how do I detect when the scrolling stops after user has flinged the view ? Or alternatively, how do I know at what position the scroll will be when it stops (then I could use it with onScrollChanged).

I have inherited HorizontalScrollView. What I'd like is to have the view behave naturally when user interacts with it but when it stops to update some of its children views - if I do it while the view is scrolling it causes small lags during scroll.

Thanks

Ganapathy C
  • 5,989
  • 5
  • 42
  • 75
jouj
  • 31
  • 1
  • 2

2 Answers2

3

I've written a custom class for HorizontalScrollView which has an interface with scroll stop method, which gives the scrollX position when scrolling stopped. here's the code:

public class CustomMCSDHorizontalScroll extends HorizontalScrollView {

    private Runnable scrollerTask;
    private int initialPosition;
    private int newCheck = 100;


    public interface onScrollStopedListner{
        void onScrollStoped();
    }

    private onScrollStopedListner onScrollStoped;

    public CustomMCSDHorizontalScroll(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        scrollerTask = new Runnable() {
            @Override
                public void run() {
                    // TODO Auto-generated method stub
                    int newPosition = getScrollX();

                    if ( initialPosition - newPosition == 0 ) {
                        if ( onScrollStoped != null ) {
                            onScrollStoped.onScrollStoped();
                        }
                    } else {
                        initialPosition = getScrollX();
                        CustomMCSDHorizontalScroll.this.postDelayed(
                            scrollerTask, newCheck);
                    }

                }
        };
    }

    public void setOnScrollStopListner (CustomMCSDHorizontalScroll.
            onScrollStopedListner listener) {
        onScrollStoped = listener;
    }

    public void startScrollerTask(){
        initialPosition = getScrollX();
        CustomMCSDHorizontalScroll.this.postDelayed(scrollerTask, newCheck);
    }

}

Here's the ontouch() method:

@Override
public boolean onTouch(View v, MotionEvent event) {

    switch (v.getId()) {
        case R.id.mHorizontalScrollViewMain:
            if (event.getAction() == KeyEvent.ACTION_UP) {
                hsvUpperTab.startScrollerTask();
            }
            break;
        case R.id.mHorizontalScrollViewSub:
            if (event.getAction() == KeyEvent.ACTION_UP) {
                hsvLowerTab.startScrollerTask();
            }
        default:
            break;
    }
    return false;
}

Here's the onScrollStop() method:

hsvLowerTab.setOnScrollStopListner(new onScrollStopedListner() {
    @Override
    public void onScrollStoped() {
        // TODO Auto-generated method stub
        getLowerTabScrolled(hsvLowerTab.getScrollX());
    }
});

hsvUpperTab.setOnScrollStopListner(new onScrollStopedListner() {
    @Override
    public void onScrollStoped() {
        // TODO Auto-generated method stub
        getUpperTabScrolled(hsvUpperTab.getScrollX());
    }
});
Diego Pino
  • 11,278
  • 1
  • 55
  • 57
NaserShaikh
  • 1,576
  • 2
  • 23
  • 39
2

You can use a scroller for the same.

Steps for using a scroller.

  1. Subclass HorizontalScrollView to have a custom view and override the methods onTouchEvent,onInterceptTouchEvent to return false.
  2. Implement OnGestureListener in the activity.
  3. Pass the onFling motion event to the scroller method

    fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY)

  4. Following scroller methods can be used:

getFinalX() getFinalY()

OR

Scan this isFinished() value constantly to find if the fling is over.

Manish Khot
  • 3,027
  • 2
  • 29
  • 37