在android listview中使用onScroll

Posted

技术标签:

【中文标题】在android listview中使用onScroll【英文标题】:using onScroll in android listview 【发布时间】:2014-12-05 06:30:59 【问题描述】:

朋友们,我正在使用列表视图并在滚动到最后时加载内容。但是如果快速滚动,我会遇到错误,例如多次调用相同的 url,任何人都可以帮助我解决这个问题。我已经通过 SCROLL_STATE_IDLE 但不知道如何在我的代码中实现它。即使在不触摸屏幕的情况下它也在运行。我现在想知道如何在空闲状态下停止代码。在下面附上我的代码

int start = 0;
int limit = 3;
loadingMore = false;
listView.setOnScrollListener(new OnScrollListener() 

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) 
            // TODO Auto-generated method stub
                            

        

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) 
            // TODO Auto-generated method stub
            int lastInScreen = firstVisibleItem + visibleItemCount;


            if ((lastInScreen == totalItemCount) ) 

                if(loadingMore == false)                   
                start = lastInScreen - 1;                   
                url = "http://www.dskjhf.com/web-servic/request_response.php?type=get_articles&start="
                        + start + "&end=" + limit;
                grabURL(url);                   
            
        
    );

    protected void grabURL(String url) 
    // TODO Auto-generated method stub
    new BackgroundTask().execute(url);
    loadingMore = true;

【问题讨论】:

【参考方案1】:

检查 onScrollStateChanged 方法。

SCROLL_STATE_IDLE 的值为 0

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
      if(scrollState == 0) Log.d(TAG", "scrolling is idele...");
    
  );

【讨论】:

【参考方案2】:

检查了这个,它会对你有所帮助..对我来说它运作良好

        listView.setOnScrollListener(new OnScrollListener() 
            private int currentVisibleItemCount;
            private int currentScrollState;
            private int currentFirstVisibleItem;
            private int totalItem;
            private LinearLayout lBelow;


            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) 
                // TODO Auto-generated method stub
                this.currentScrollState = scrollState;
                this.isScrollCompleted();               
            

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) 
                // TODO Auto-generated method stub
                this.currentFirstVisibleItem = firstVisibleItem;
                this.currentVisibleItemCount = visibleItemCount;
                this.totalItem = totalItemCount;


            

            private void isScrollCompleted() 
                if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
                        && this.currentScrollState == SCROLL_STATE_IDLE) 
                 /** To do code here*/

                    Page = Page + 1;
                    apiVariables = TCGAPIVariable.getSingletonObject();
                    searchUrl = apiVariables.searchList(cityId, catId, area,
                            keyword, cuisine, type, cost, "" + Page, "20",uId);
                    Log.d("SEARCH_URL", searchUrl.trim());
                    int totalRecord = Integer.parseInt(itemData.get(0)
                            .getTotalRecord());
                    if (totalRecord > totalItem) 
                        if (TCGStaticMethods
                                .isInternetAvailable(SearchActivity.this))
                            new SearchAsynTask(SearchActivity.this, searchUrl,
                                    true).execute();
                     else 
                        LinearLayout llBelow = (LinearLayout) findViewById(R.id.pbSearchLististingBelow);
                        llBelow.setVisibility(View.GONE);
                    
                
            
        );

【讨论】:

以上是关于在android listview中使用onScroll的主要内容,如果未能解决你的问题,请参考以下文章

如何使用json解析android在listview中获取图像

Android:使用 ArrayAdapter 在 ListView 中替换颜色 [重复]

Android SQLite与ListView的简单使用

Android:使用 parsequery 适配器在 listview 中更改 imageview 的图像

在android中使用arrayadapter类自定义listview

在listview中使用意图时如何在android中处理NullPointerException?