检测滚动向上和在ListView中向下滚动 [英] Detect Scroll Up & Scroll down in ListView

查看:257
本文介绍了检测滚动向上和在ListView中向下滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要求:

  • 在第一,对于页面中没有数据:2是来自服务器和放大器进账;该项目 填充在一个ListView。
  • At first, data for page no: 2 is fetched from the server & the items are populated in a ListView.

考虑,无论是$ P $光伏页及放大器;下页中提供了一个方案中,以下code已被添加

Considering that both the prev page & next page are available in a scenario, the following code has been added:

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

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

什么条件我应该把检测滚动向上和向下滚动上下面的方法:

What conditions should I put to detect scroll up & scroll down on the following methods:

  1. 无效onScroll(AbsListView观点,诠释firstVisibleItem,INT visibleItemCount,INT totalItemCount)
  2. 无效onScrollStateChanged(AbsListView观点,诠释scrollState)
  1. void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
  2. void onScrollStateChanged(AbsListView view, int scrollState)

在行动:滚动向上和向下滚动检测,因此服务将被调用时的preV页面没有或下一页没有,获取的项目被填充到列表视图。

After the action: scroll up & scroll down is detected , accordingly a service will be called with either the prev page no or next page no , to fetch the items to be populated in the Listview.

任何投入将是有益的。

通过以下链接走了,但它不是返回正确的向上滚动/向下滚动操作:

Gone through the following links but its not returning the correct scroll up / scroll down action:

<一个href="http://stackoverflow.com/questions/9629872/how-to-detect-if-a-listview-is-scrolling-up-or-down-in-android">link 1 <一href="http://stackoverflow.com/questions/12114963/detecting-the-scrolling-direction-in-the-adapter-up-down">link 2

推荐答案

尝试使用setOnScrollListener并实现onScrollStateChanged与scrollState

try using the setOnScrollListener and implement the onScrollStateChanged with scrollState

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中向下滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆