如何在水平滚动视图android中获取滚动方向?

时间:2012-12-07 05:57:46

标签: android horizontalscrollview

我想知道如何在水平滚动视图中获取滚动的方向,就像是向左或向右滚动我应该立即知道,就像 onScrollChnageListener 之类的东西,并且有方法< strong> onLeftScroll 或 onRightScroll

2 个答案:

答案 0 :(得分:2)

你可以像下面这样找到它:

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;
        };
};

答案 1 :(得分:0)

一点点逻辑对我有用。你可以简单地拿两个指针[firstPointerCurrentValue,firstPointerLastValue](....不要恐慌firstPointer是总是指向列表中第一个可见项的变量)并且匹配它们用于新的和更新的值。

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;
}