71

我有以下要求:

  • 首先,从服务器获取第 2 页的数据,并将项目填充到 ListView 中。

考虑到场景中的上一页和下一页都可用,因此添加了以下代码:

 if(prevPageNo > 0){
    mListViewActual.setOnScrollListener(this);
 }

 if(nextPageNo > 0){
    mListViewActual.setOnScrollListener(this);
 }

我应该在以下方法上检测向上滚动和向下滚动的条件:

  1. 无效 onScroll(AbsListView 视图,int firstVisibleItem,int visibleItemCount,int totalItemCount)
  2. 无效 onScrollStateChanged(AbsListView 视图,int scrollState)

在检测到操作:向上滚动和向下滚动之后,将使用 prev page no 或 next page no 调用服务,以获取要在 Listview 中填充的项目。

任何输入都会有所帮助。

通过以下链接,但它没有返回正确的向上/向下滚动动作:

链接 1 链接 2

4

17 回答 17

94

尝试使用 setOnScrollListener 并使用 scrollState 实现 onScrollStateChanged

setOnScrollListener(new OnScrollListener(){
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
      // TODO Auto-generated method stub
    }
    public void onScrollStateChanged(AbsListView view, int scrollState) {
      // TODO Auto-generated method stub
      final ListView lw = getListView();

       if(scrollState == 0) 
      Log.i("a", "scrolling stopped...");


        if (view.getId() == lw.getId()) {
        final int currentFirstVisibleItem = lw.getFirstVisiblePosition();
         if (currentFirstVisibleItem > mLastFirstVisibleItem) {
            mIsScrollingUp = false;
            Log.i("a", "scrolling down...");
        } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
            mIsScrollingUp = true;
            Log.i("a", "scrolling up...");
        }

        mLastFirstVisibleItem = currentFirstVisibleItem;
    } 
    }
  });
于 2013-06-10T05:43:10.033 回答
63

这是上述一些解决方案的工作修改版本。

添加另一个类 ListView:

package com.example.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;

public class ListView extends android.widget.ListView {

    private OnScrollListener onScrollListener;
    private OnDetectScrollListener onDetectScrollListener;

    public ListView(Context context) {
        super(context);
        onCreate(context, null, null);
    }

    public ListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        onCreate(context, attrs, null);
    }

    public ListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        onCreate(context, attrs, defStyle);
    }

    @SuppressWarnings("UnusedParameters")
    private void onCreate(Context context, AttributeSet attrs, Integer defStyle) {
        setListeners();
    }

    private void setListeners() {
        super.setOnScrollListener(new OnScrollListener() {

            private int oldTop;
            private int oldFirstVisibleItem;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (onScrollListener != null) {
                    onScrollListener.onScrollStateChanged(view, scrollState);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (onScrollListener != null) {
                    onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
                }

                if (onDetectScrollListener != null) {
                    onDetectedListScroll(view, firstVisibleItem);
                }
            }

            private void onDetectedListScroll(AbsListView absListView, int firstVisibleItem) {
                View view = absListView.getChildAt(0);
                int top = (view == null) ? 0 : view.getTop();

                if (firstVisibleItem == oldFirstVisibleItem) {
                    if (top > oldTop) {
                        onDetectScrollListener.onUpScrolling();
                    } else if (top < oldTop) {
                        onDetectScrollListener.onDownScrolling();
                    }
                } else {
                    if (firstVisibleItem < oldFirstVisibleItem) {
                        onDetectScrollListener.onUpScrolling();
                    } else {
                        onDetectScrollListener.onDownScrolling();
                    }
                }

                oldTop = top;
                oldFirstVisibleItem = firstVisibleItem;
            }
        });
    }

    @Override
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    public void setOnDetectScrollListener(OnDetectScrollListener onDetectScrollListener) {
        this.onDetectScrollListener = onDetectScrollListener;
    }
}

还有一个界面:

public interface OnDetectScrollListener {

    void onUpScrolling();

    void onDownScrolling();
}

最后如何使用:

com.example.view.ListView listView = (com.example.view.ListView) findViewById(R.id.list);
listView.setOnDetectScrollListener(new OnDetectScrollListener() {
    @Override
    public void onUpScrolling() {
        /* do something */
    }

    @Override
    public void onDownScrolling() {
        /* do something */
    }
});

在您的 XML 布局中:

<com.example.view.ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

这是我的第一个话题,请不要苛责我。=)

于 2013-12-12T10:24:26.263 回答
47

这是一个简单的实现:

lv.setOnScrollListener(new OnScrollListener() {
        private int mLastFirstVisibleItem;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

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

            if(mLastFirstVisibleItem<firstVisibleItem)
            {
                Log.i("SCROLLING DOWN","TRUE");
            }
            if(mLastFirstVisibleItem>firstVisibleItem)
            {
                Log.i("SCROLLING UP","TRUE");
            }
            mLastFirstVisibleItem=firstVisibleItem;

        }
    });

如果您需要更高的精度,可以使用此自定义 ListView 类:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

/**
 * Created by root on 26/05/15.
 */
public class ScrollInterfacedListView extends ListView {


    private OnScrollListener onScrollListener;
    private OnDetectScrollListener onDetectScrollListener;

    public ScrollInterfacedListView(Context context) {
        super(context);
        onCreate(context, null, null);
    }

    public ScrollInterfacedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        onCreate(context, attrs, null);
    }

    public ScrollInterfacedListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        onCreate(context, attrs, defStyle);
    }

    @SuppressWarnings("UnusedParameters")
    private void onCreate(Context context, AttributeSet attrs, Integer defStyle) {
        setListeners();
    }

    private void setListeners() {
        super.setOnScrollListener(new OnScrollListener() {

            private int oldTop;
            private int oldFirstVisibleItem;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (onScrollListener != null) {
                    onScrollListener.onScrollStateChanged(view, scrollState);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (onScrollListener != null) {
                    onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
                }

                if (onDetectScrollListener != null) {
                    onDetectedListScroll(view, firstVisibleItem);
                }
            }

            private void onDetectedListScroll(AbsListView absListView, int firstVisibleItem) {
                View view = absListView.getChildAt(0);
                int top = (view == null) ? 0 : view.getTop();

                if (firstVisibleItem == oldFirstVisibleItem) {
                    if (top > oldTop) {
                        onDetectScrollListener.onUpScrolling();
                    } else if (top < oldTop) {
                        onDetectScrollListener.onDownScrolling();
                    }
                } else {
                    if (firstVisibleItem < oldFirstVisibleItem) {
                        onDetectScrollListener.onUpScrolling();
                    } else {
                        onDetectScrollListener.onDownScrolling();
                    }
                }

                oldTop = top;
                oldFirstVisibleItem = firstVisibleItem;
            }
        });
    }

    @Override
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    public void setOnDetectScrollListener(OnDetectScrollListener onDetectScrollListener) {
        this.onDetectScrollListener = onDetectScrollListener;
    }


    public interface OnDetectScrollListener {

        void onUpScrolling();

        void onDownScrolling();
    }

}

使用示例:(不要忘记将其添加为您的 layout.xml 中的 Xml 标记)

scrollInterfacedListView.setOnDetectScrollListener(new ScrollInterfacedListView.OnDetectScrollListener() {
            @Override
            public void onUpScrolling() {
               //Do your thing
            }

            @Override
            public void onDownScrolling() {

             //Do your thing
            }
        });
于 2014-06-03T08:34:21.503 回答
12

发布所有方法后,识别用户何时从第一个元素向上滚动或从最后一个元素向下滚动时存在问题。这是检测向上/向下滚动的另一种方法:

        listView.setOnTouchListener(new View.OnTouchListener() {
            float height;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                float height = event.getY();
                if(action == MotionEvent.ACTION_DOWN){
                    this.height = height;
                }else if(action == MotionEvent.ACTION_UP){
                    if(this.height < height){
                        Log.v(TAG, "Scrolled up");
                    }else if(this.height > height){
                        Log.v(TAG, "Scrolled down");
                    }
                }
                return false;
            }
        });
于 2015-08-02T18:49:20.157 回答
9
            ListView listView = getListView();
            listView.setOnScrollListener(new OnScrollListener() {

                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {

                    view.setOnTouchListener(new OnTouchListener() {
                        private float mInitialX;
                        private float mInitialY;

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            switch (event.getAction()) {
                                case MotionEvent.ACTION_DOWN:
                                    mInitialX = event.getX();
                                    mInitialY = event.getY();
                                    return true;
                                case MotionEvent.ACTION_MOVE:
                                    final float x = event.getX();
                                    final float y = event.getY();
                                    final float yDiff = y - mInitialY;
                                    if (yDiff > 0.0) {
                                        Log.d(tag, "SCROLL DOWN");
                                        scrollDown = true;
                                        break;

                                    } else if (yDiff < 0.0) {
                                        Log.d(tag, "SCROLL up");
                                        scrollDown = true;
                                        break;

                                    }
                                    break;
                            }
                            return false;
                        }
                    });
于 2013-06-17T01:52:27.547 回答
8

我的解决方案完美地为每个滚动方向提供了确切的值。 distanceFromFirstCellToTop包含从第一个单元格到父视图顶部的确切距离。我保存这个值,previousDistanceFromFirstCellToTop并在滚动时将它与新值进行比较。如果它较低,那么我向上滚动,否则,我向下滚动。

private int previousDistanceFromFirstCellToTop;

listview.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        View firstCell = listview.getChildAt(0);
        int distanceFromFirstCellToTop = listview.getFirstVisiblePosition() * firstCell.getHeight() - firstCell.getTop();

        if(distanceFromFirstCellToTop < previousDistanceFromFirstCellToTop)
        {
           //Scroll Up
        }
        else if(distanceFromFirstCellToTop  > previousDistanceFromFirstCellToTop)
        {
           //Scroll Down
        }
        previousDistanceFromFirstCellToTop = distanceFromFirstCellToTop;
    }
});

对于 Xamarin 开发人员,解决方案如下:

注意:不要忘记在 UI 线程上运行

listView.Scroll += (o, e) =>
{
    View firstCell = listView.GetChildAt(0);
    int distanceFromFirstCellToTop = listView.FirstVisiblePosition * firstCell.Height - firstCell.Top;

    if (distanceFromFirstCellToTop < previousDistanceFromFirstCellToTop)
    {
        //Scroll Up
    }
    else if (distanceFromFirstCellToTop > previousDistanceFromFirstCellToTop)
    {
        //Scroll Down
    }
    previousDistanceFromFirstCellToTop = distanceFromFirstCellToTop;
};
于 2015-01-13T14:29:39.463 回答
4

只需将滚动侦听器设置为您的列表视图。

如果您有页眉或页脚,您也应该检查可见计数。如果它增加,则意味着您正在向下滚动。(如果有页脚而不是页眉,则反转它)

如果您的列表视图中没有任何页眉或页脚,您可以删除检查可见项目计数的行。

listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (mLastFirstVisibleItem > firstVisibleItem) {
                Log.e(getClass().toString(), "scrolling up");
            } else if (mLastFirstVisibleItem < firstVisibleItem) {
                Log.e(getClass().toString(), "scrolling down");
            } else if (mLastVisibleItemCount < visibleItemCount) {
                Log.e(getClass().toString(), "scrolling down");
            } else if (mLastVisibleItemCount > visibleItemCount) {
                Log.e(getClass().toString(), "scrolling up");
            }
            mLastFirstVisibleItem = firstVisibleItem;
            mLastVisibleItemCount = visibleItemCount;
        }

        public void onScrollStateChanged(AbsListView listView, int scrollState) {
        }
    });

并有这个变量

int mLastFirstVisibleItem;
int mLastVisibleItemCount;
于 2014-02-19T08:59:36.757 回答
4

我使用了这个更简单的解决方案:

setOnScrollListener( new OnScrollListener() 
{

    private int mInitialScroll = 0;

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) 
    {
        int scrolledOffset = computeVerticalScrollOffset();

        boolean scrollUp = scrolledOffset > mInitialScroll;
        mInitialScroll = scrolledOffset;
    }


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {


    }

}
于 2014-05-31T13:20:56.663 回答
4

为了也检测较大元素的滚动,我更喜欢 onTouch 侦听器:

listview.setOnTouchListener(new View.OnTouchListener() {

        int scrollEventListSize = 5; 
        float lastY;
        // Used to correct for occasions when user scrolls down(/up) but the onTouchListener detects it incorrectly. We will store detected up-/down-scrolls with -1/1 in this list and evaluate later which occured more often
        List<Integer> downScrolledEventsHappened;

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

            float diff = 0;
            if(event.getAction() == event.ACTION_DOWN){
                lastY = event.getY();
                downScrolledEventsHappened = new LinkedList<Integer>();
            }
            else if(event.getAction() == event.ACTION_MOVE){
                diff = event.getY() - lastY;
                lastY = event.getY();

                if(diff>0)
                    downScrolledEventsHappened.add(1);
                else 
                    downScrolledEventsHappened.add(-1);

               //List needs to be filled with some events, will happen very quickly
                if(downScrolledEventsHappened.size() == scrollEventListSize+1){
                    downScrolledEventsHappened.remove(0);
                    int res=0;
                    for(int i=0; i<downScrolledEventsHappened.size(); i++){
                        res+=downScrolledEventsHappened.get(i);
                    }

                    if (res > 0) 
                        Log.i("INFO", "Scrolled up");
                    else 
                        Log.i("INFO", "Scrolled down");
                }
            }
            return false; // don't interrupt the event-chain
        }
    });
于 2015-12-29T06:50:53.440 回答
3

存储 firstVisibleItem 并在下一个 onScroll 检查新的 firstVisibleItem 是否小于或大于前一个。

示例伪代码(未测试):

int lastVisibleItem = 0;
boolean isScrollingDown = false;

void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    if (firstVisibleItem > lastVisibleItem) {
        isScrollingDown = true;
    }
    else {
        isScrollingDown = false;
    }
    lastVisibleItem = firstVisibleItem;
}
于 2013-05-28T12:18:37.103 回答
1

在android listview上检测向上/向下滚动的简单方法

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){
  if(prevVisibleItem != firstVisibleItem){
    if(prevVisibleItem < firstVisibleItem)
      //ScrollDown
    else
      //ScrollUp

  prevVisibleItem = firstVisibleItem;
}

不要忘记

yourListView.setOnScrollListener(yourScrollListener);
于 2014-02-06T03:11:53.053 回答
1

出于某种原因,Android 文档没有涵盖这一点,甚至在文档中也没有使用该方法......我花了一段时间才找到它。

要检测您的滚动是否在顶部,您将使用它。

public boolean checkAtTop() 
{
    if(listView.getChildCount() == 0) return true;
    return listView.getChildAt(0).getTop() == 0;
}

这将检查您的滚动条是否位于顶部。现在,为了做到这一点,您必须将您拥有的孩子的数量传递给它,并检查该数字。您可能必须计算一次屏幕上有多少人,然后从您的孩子人数中减去。我从来没有这样做过。希望这可以帮助

于 2013-06-10T01:08:16.440 回答
1

关于在listview中检测向上或向下滚动的技巧,您只需在ListView的OnScrollListener中的onScroll函数上调用此函数即可。

private int oldFirstVisibleItem = -1;
    private protected int oldTop = -1;
    // you can change this value (pixel)
    private static final int MAX_SCROLL_DIFF = 5;

    private void calculateListScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (firstVisibleItem == oldFirstVisibleItem) {
            int top = view.getChildAt(0).getTop();
            // range between new top and old top must greater than MAX_SCROLL_DIFF
            if (top > oldTop && Math.abs(top - oldTop) > MAX_SCROLL_DIFF) {
                // scroll up
            } else if (top < oldTop && Math.abs(top - oldTop) > MAX_SCROLL_DIFF) {
                // scroll down
            }
            oldTop = top;
        } else {
            View child = view.getChildAt(0);
            if (child != null) {
                oldFirstVisibleItem = firstVisibleItem;
                oldTop = child.getTop();
            }
        }
    }
于 2013-11-20T08:43:04.480 回答
1

这些方法不能用于直接检测滚动方向。有很多方法可以找到方向。下面解释了一种此类方法的简单代码(未经测试):

公共类 ScrollTrackingListView 扩展 ListView {

    私人布尔readyForMeasurement = false;
    私有布尔 isScrollable = null;
    私人浮动 prevDistanceToEnd = -1.0;
    私有 ScrollDirectionListener 监听器 = null;

    公共 ScrollTrackingListView(上下文上下文){
        超级(上下文);
        在里面();
    }

    public Sc​​rollTrackingListView(上下文上下文,AttributeSet attrs){
        超级(上下文,属性);
        在里面();
    }

    public Sc​​rollTrackingListView(上下文上下文,AttributeSet attrs,int defStyle){
        超级(上下文,属性,defStyle);
        在里面();
    }

    私人无效初始化(){
        ViewTreeObserver 观察者 = getViewTreeObserver();
        观察者.addOnGlobalLayoutListener(globalLayoutListener);
        setOnScrollListener(scrollListener);
    }

    私有 ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener
            = 新 ViewTreeObserver.OnGlobalLayoutListener() {

        @覆盖
        公共无效 onGlobalLayout() {
            准备好测量=真;
            计算距离结束();
        }

    };

    公共无效 registerScrollDirectionListener(ScrollDirectionListener 监听器) {
        scrollDirectionListener = 监听器;
    }

    公共无效 unregisterScrollDirectionListener() {
        滚动方向监听器 = null;
    }

    私有 OnScrollListener 滚动监听器
            = 新的 OnScrollListener() {

        @覆盖
        公共无效 onScrollStateChanged(AbsListView absListView,int i){
            计算距离结束();
        }

        @覆盖
        公共无效 onScroll(AbsListView absListView,int i,int i1,int i2){
            // 没做什么
        }

    };

    私人无效计算距离结束(){

        如果(准备好测量){

            // 我正在使用布局的高度、水平滚动条和
            // 内容连同向下滚动偏移量

            // computeVerticalScrollExtent 用于计算滚动条轨道内拇指的长度。
            // 拇指的长度是视图高度和内容长度的函数。
            int verticalScrollExtent = computeVerticalScrollExtent();
            int verticalScrollOffset = computeVerticalScrollOffset();
            int verticalScrollRange = computeVerticalScrollRange();
            int Horizo​​ntalScrollBarHeight = getHorizo​​ntalScrollbarHeight();

            /**
             * 1. 让“R”代表垂直滚动条的范围。这对应于内容的长度
             * 在视图中。
             * 2. 让“E”代表垂直滚动条的范围。范围是一个常数值并且是
             *(可能)等于与视图高度成比例的值。
             * 3. 偏移量“o”表示用户可见范围内的当前位置。它可以采取
             * 从“0 到 E”的值。
             *
             * 现在 DistanceToEnd 使用这三个值计算如下:
             *
             * DistanceToEnd = (R - o) / E
             *
             * DistanceToEnd 将保存 NumberOfScreenToEnd 单位的值。
             *
             */

            浮动距离ToEnd =
                    ((float)(verticalScrollRange - verticalScrollOffset))/((float)(verticalScrollExtent));

            如果(prevDistanceToEnd == -1){
                 prevDistanceToEnd = distanceToEnd;
            } 别的 {
                 如果(听众!= null){
                     如果(距离到结束> prevDistanceToEnd){
                        // 用户向上滚动
                         listener.onScrollingUp();
                     } 别的 {
                        // 用户向上滚动
                         listener.onScrollingDown();
                    }
                 }
                 prevDistanceToEnd = distanceToEnd;
            }

            if(isScrollable == null) {
                // 检查视图高度是否小于屏幕(即不启用滚动)
                if((horizo​​ntalScrollBarHeight + verticalScrollExtent) >= verticalScrollRange) {
                    isScrollable = Boolean.FALSE;
                } 别的 {
                    isScrollable = Boolean.TRUE;
                }
            }

        }

    }

    公共接口 ScrollDirectionListener {

        公共无效 onScrollingUp();

        公共无效 onScrollingDown();

    }

}

这个想法是计算distanceToEnd。如果 distanceToEnd 增加,用户向上滚动,如果它减少,用户向下滚动。这也将为您提供到列表末尾的确切距离。

如果您只是想知道用户是向上还是向下滚动,您可以覆盖onInterceptTouchEvent以检测滚动方向,如下所示:

    @覆盖
    公共布尔 onInterceptTouchEvent(MotionEvent 事件){
        开关 (event.getAction()) {
            案例 MotionEvent.ACTION_DOWN:
                mInitialX = event.getX();
                mInitialY = event.getY();
                返回真;
            案例 MotionEvent.ACTION_MOVE:
                最终浮点数 x = event.getX();
                最终浮动 y = event.getY();
                最终浮点 yDiff = y - mInitialY; // yDiff 小于 0.0 表示向下滚动,而 yDiff 大于 0.0 表示向上滚动。如果我尝试添加小于或大于符号,预览将拒绝显示它。
                if(yDiff 小于 0.0) listener.onScrollingDown();
                else if(yDiff 大于 0.0) listener.onScrollingUp();
                休息;
        }
        返回 super.onInterceptTouchEvent(event);
    }

于 2013-06-10T02:34:52.950 回答
1

在android GridView中的向上/向下滚动事件上加载更多项目的简单方法

    grid.setOnScrollListener(new AbsListView.OnScrollListener() {
                private int mLastFirstVisibleItem;
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    // TODO Auto-generated method stub
                    Log.d("state",String.valueOf(scrollState));


                    if(scrollState == 0)
                        Log.i("a", "scrolling stopped...");


                    if (view.getId() == grid.getId()) {

                        final int currentFirstVisibleItem = grid.getLastVisiblePosition();
                        mLastFirstVisibleItem = grid.getFirstVisiblePosition();

                        if (currentFirstVisibleItem > mLastFirstVisibleItem) {
                            mIsScrollingUp = false;
                            if(!next.contains("null")){

                           //Call api to get products from server

                             }


                            Log.i("a", "scrolling down...");
                        } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
                            mIsScrollingUp = true;
                            Log.i("a", "scrolling up...");
                        }

                        mLastFirstVisibleItem = currentFirstVisibleItem;
                    }

                }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                Log.d("on scroll","");
            }
        });
于 2019-12-13T09:16:36.310 回答
0

这是我首先要尝试的:

1)使用这些方法创建一个接口(我们称之为 OnScrollTopOrBottomListener):

无效 onScrollTop();

无效 onScrollBottom();

2) 在列表的适配器中,添加一个成员实例,键入您创建的接口并提供一个 setter 和 getter。

3) 在适配器的 getView() 实现中,检查 position 参数是 0 还是 getCount() - 1。还要检查 OnScrollTopOrBottomListener 实例是否为空。

4) 如果位置为0,则调用onScrollTopOrBottomListener.onScrollTop()。如果 position 为 getCount() - 1,则调用 onScrollTopOrBottomListener.onScrollBottom()。

5) 在您的 OnScrollTopOrBottomListener 实现中,调用适当的方法来获取所需的数据。

希望在某种程度上有所帮助。

-布兰登

于 2013-06-10T02:54:27.360 回答
0

我在使用一些 ListView 的单元格大小很大的示例时遇到了问题。所以我找到了一个解决我的问题的方法,它可以检测到你手指的最轻微移动。我已经尽可能地简化了,如下所示:

private int oldScrolly;


@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

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

            View view = absListView.getChildAt(0);
            int scrolly = (view == null) ? 0 : -view.getTop() + absListView.getFirstVisiblePosition() * view.getHeight();
            int margin = 10;

            Log.e(TAG, "Scroll y: " + scrolly + " - Item: " + firstVisibleItem);


            if (scrolly > oldScrolly + margin) {
                Log.d(TAG, "SCROLL_UP");
                oldScrolly = scrolly;
            } else if (scrolly < oldScrolly - margin) {
                Log.d(TAG, "SCROLL_DOWN");
                oldScrolly = scrolly;
            }
        }
    });

PD:我使用 MARGIN 来检测滚动,直到你遇到那个 margin 。当我显示或隐藏视图并避免它们闪烁时,这可以避免出现问题。

于 2015-05-12T16:06:24.397 回答