检测向上滚动并在ListView中向下滚动

我有以下要求:

  • 首先,页面号:2的数据从服务器获取,并且项目被填充到一个ListView中。
  • 考虑到prev页面和下一页面在场景中都可用,所以添加了以下代码:

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

    我应该检测什么条件向上滚动并向下滚动以下方法:

  • void onScroll(AbsListView视图,int firstVisibleItem,int visibleItemCount,int totalItemCount)
  • void onScrollStateChanged(AbsListView视图,int scrollState)
  • 在操作之后:检测到向上滚动并向下滚动,因此将使用前导页面编号或下一个页面编号来调用服务,以获取要在Listview中填充的项目。

    任何投入都将有所帮助。

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

    链接1链接2


    尝试使用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;
        } 
        }
      });
    

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

    添加另一个类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"/>
    

    这是我的第一个话题,不要苛刻地评判我。 =)


    这是一个简单的实现:

    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
                }
            });
    
    链接地址: http://www.djcxy.com/p/69029.html

    上一篇: Detect Scroll Up & Scroll down in ListView

    下一篇: Android heterogeneous gridview like pinterest?