找出ListView是否滚动到最后?

时间:2014-08-28 07:57:32

标签: android

通过以下操作,我可以检查滚动列表视图是否完成。现在我想检查列表视图滚动到完成。如何举办此活动?

@Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (prevVisibleItem != firstVisibleItem) {
            if (prevVisibleItem < firstVisibleItem)
                Log.e(getClass().toString(), "DOWN");
            else
                Log.e(getClass().toString(), "UP");

            prevVisibleItem = firstVisibleItem;
            /*
            * CHECK SCROLLING BUTTOM FINISH
            */

            if((firstVisibleItem + visibleItemCount) ==  totalItemCount)
            {
                Log.i("Info", "Scroll Bottom FINISH" );
            }

            /*
            * CHECK SCROLLING UP FINISH
            */
        }
    }

3 个答案:

答案 0 :(得分:3)

if(firstVisibleItem == 0 && !isFirstTime){
 //it is top of the list
}

在您的班级isFirstTime中定义一个变量,并将其初始化为true。当用户滚动列表时 检查

if (firstVisibleItem  != 0 )
{
    isFirstTime =false;
}

这意味着用户滚动列表,第一个元素消失,如果再次

firstVisibleItem  == 0

表示列表向上滚动并完成。

答案 1 :(得分:0)

使用 OnSwipeTouchListener.java:

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

    public boolean onTouch(final View view, final MotionEvent motionEvent) {

        super.onTouch(view, motionEvent);
        return gestureDetector.onTouchEvent(motionEvent);

    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }
    public void onSwipeRight() {}
    public void onSwipeLeft() {}
    public void onSwipeTop() {}
    public void onSwipeBottom() {}
}

用法:

imageView.setOnTouchListener(new OnSwipeTouchListener() {

    public void onSwipeTop() {
        Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeRight() {
        Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeLeft() {
        Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
    }
    public void onSwipeBottom() {
        Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
    }

});

答案 2 :(得分:0)

这可能会有所帮助。我在stackoverflow上搜索了你的问题,我认为这个答案正是你问题的答案。滚动完成检测。 Click Here for Full Code Answer

private void isScrollCompleted() {
    if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
        /*** In this way I detect if there's been a scroll which has completed ***/
        /*** do the work! ***/
    }
}