Mahesh
Mahesh

Reputation: 2892

How to get direction of scroll in Horizontal scroll view android?

I want know how to get direction of scroll in horizontal scrollview ,like if is scroll to left or right the i should immediately know about that , like there is something like onScrollChnageListener and there have method onLeftScroll or on onRightScroll

Upvotes: 1

Views: 2884

Answers (2)

Pirate
Pirate

Reputation: 545

You can find it out like following:

private GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {

        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

            if(e1.getX() > e2.getX())
                isScrollingTowardsLeft = true;
            else
                isScrollingTowardsLeft = false;
            return false;
        };
};

Upvotes: 2

user2229834
user2229834

Reputation:

Little bit of logic worked for me.You can simply take two pointers[firstPointerCurrentValue,firstPointerLastValue](.... dont't panic firstPointer is the variable that always points to the first visible item in list) and match them for new and updated values.`

int firstPointerCurrentValue = 0;
int firstPointerLastValue = 0;

........

lv.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView alv, int scrollState) {

            checkValue(firstPointerCurrentValue, firstPointerLastValue);
            firstPointerLastValue = firstPointerCurrentValue;
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            firstPointerCurrentValue = firstVisibleItem;

        }
    });

......

boolean checkValue(int firstPointerCurrentValue, int firstPointerLastValue) {

    if (firstPointerCurrentValue > firstPointerLastValue) {
        Toast.makeText(getBaseContext(), "DOWN ", Toast.LENGTH_SHORT)
         .show();
        ab.hide();
    } else if (firstPointerCurrentValue < firstPointerLastValue) {
         Toast.makeText(getBaseContext(), "UP ",
         Toast.LENGTH_SHORT).show();

    } else {
        // Toast.makeText(getBaseContext(), "No Move ", Toast.LENGTH_SHORT)
        // .show();
    }

    return false;
}

Upvotes: 0

Related Questions