2016-10-19 2 views
0

다음 코드를 사용하여 무한 스크롤을 구현합니다. 하지만 문제는 onScroll() 메서드가 계속 호출을 계속 수행하고 데이터로드가 무한 루프에 빠지기 때문입니다. 나는 무엇을 여기에서 놓치고 있냐? android : EndlessRecyclerOnScrollListener가 루프를 발생시킵니다.

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener { 

    public int previousTotal = 0; // The total number of items in the dataSet after the last load 
    public boolean loading = true; // True if we are still waiting for the last set of data to load. 
    public int visibleThreshold = 0; // The minimum amount of items to have below your current scroll position before loading more. 
    int firstVisibleItem, visibleItemCount, totalItemCount; 
    public int current_page = 1; 
    private LinearLayoutManager mLinearLayoutManager; 


    public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) { 
     this.mLinearLayoutManager = linearLayoutManager; 
    } 

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

     visibleItemCount = recyclerView.getChildCount(); 
     totalItemCount = mLinearLayoutManager.getItemCount(); 
     firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition(); 

     if (loading) { 
      if (totalItemCount > previousTotal) { 
       loading = false; 
       previousTotal = totalItemCount; 
      } 
     } 
     if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { 

      current_page++; 
      onLoadMore(current_page); 
      loading = true; 
     } 
    } 
    public abstract void onLoadMore(int current_page); 

    @Override 
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
     super.onScrollStateChanged(recyclerView, newState); 
    } 
} 

내 활동

는 :

endlessRecyclerOnScrollListener = new EndlessRecyclerOnScrollListener(mLayoutManager) { 
      @Override 
      public void onLoadMore(int current_page) { 
        loadData() 

      } 
     }; 
     mRecyclerView.addOnScrollListener(endlessRecyclerOnScrollListener); 
+0

다음을 시도해보십시오. https://codentrick.com/load-more-recyclerview-bottom-progressbar/ – compte14031879

답변

0

이 내가 무한 스크롤을했고, 나를 위해 완벽하게 작동하는 방법이다. 감사합니다 ...

private boolean loading = true; 
int pastVisiblesItems, visibleItemCount, totalItemCount; 
LinearLayoutManager mLayoutManager; 


mLayoutManager = new LinearLayoutManager(this); 
mRecyclerView.setLayoutManager(mLayoutManager); 
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
{ 
    @Override 
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
    { 
     if(dy > 0) //check for scroll down 
     { 
      visibleItemCount = mLayoutManager.getChildCount(); 
      totalItemCount = mLayoutManager.getItemCount(); 
      pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition(); 

      if (loading) 
      { 
       if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) 
       { 
        loading = false; 
        Log.v("...", "We reach Last Item!"); 

        // fetch new data here or whatever you want. 
        DoPagination(); 

       } 
      } 
     } 
    } 
}); 
관련 문제