2015-09-05 25 views
0

我有一個回收視圖,我想在用戶到達recycleview結束時運行asynTask。這是我的代碼:android - 如何回收視圖只有滾動下來才能運行任務

recycle.addOnScrollListener(new RecyclerView.OnScrollListener() { 
     @Override 
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
      super.onScrolled(recyclerView, dx, dy); 

      visibleItemCount = recycle.getChildCount(); 
      totalItemCount = mLayoutManager.getItemCount(); 
      firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); 

      if (loadmore) { 
       if (totalItemCount > previousTotal) { 
        loadmore = false; 
        previousTotal = totalItemCount; 
       } 
      } 
      if (!loadmore && (totalItemCount - visibleItemCount) 
        <= (firstVisibleItem + visibleThreshold)) { 
       if(taskrunning!=null && taskrunning==false){ 
        loadmore = false; 
        page = page + 1; 
        new AsyncHttpTask().execute(url); 
       } 

      } 
     } 
    }); 

它的工作原理髮現,當我到達recycleView結束,但是當我scrollup,它再次調用的AsyncTask。

我不想在我滾動時調用asyncTask,我只想向下滾動時調用它。

我該怎麼辦?

回答

0

在我指出的位置將loadmore設置爲true。

recycle.addOnScrollListener(new RecyclerView.OnScrollListener() { 
    @Override 
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
     super.onScrolled(recyclerView, dx, dy); 

     visibleItemCount = recycle.getChildCount(); 
     totalItemCount = mLayoutManager.getItemCount(); 
     firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); 

     if (loadmore) { 
      if (totalItemCount > previousTotal) { 
       loadmore = false; 
       previousTotal = totalItemCount; 
      } 
     } 
     if (!loadmore && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { 
      if(taskrunning!=null && taskrunning==false){ 
       loadmore = true; // SET TO TRUE HERE! 
       page = page + 1; 
       new AsyncHttpTask().execute(url); 
      } 
     } 
    } 
}); 

而在你AsyncTaskonPostExecute部分設置loadmore = false

相關問題