Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HorizontalScrollView - calculate stop position after fling

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

like image 463
jouj Avatar asked Dec 07 '25 04:12

jouj


1 Answers

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());
    }
});
like image 51
NaserShaikh Avatar answered Dec 08 '25 17:12

NaserShaikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!