2016-03-01 38 views
0

這就是我設置recyclerview。Android recyclerview無盡滾動listner onScrolled()被叫兩次

mRecyclerViewRides = (RecyclerView) findViewById(R.id.recyclerViewRides); 
    // use this setting to improve performance if you know that changes 
    // in content do not change the layout size of the RecyclerView 
    mRecyclerViewRides.setHasFixedSize(true); 
    // use a linear layout manager 
    mLinearLayoutManager = new LinearLayoutManager(getApplicationContext()); 
    mRecyclerViewRides.setLayoutManager(mLinearLayoutManager); 
    mRideListAdapter = new FindRideListAdapter(getApplicationContext(), mRecyclerViewRides, mRideDetailArrayList, mImageOptions, mImageLoader, this); 
mRecyclerViewRides.setAdapter(mRideListAdapter); 
mRecyclerViewRides.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLinearLayoutManager) { 
     @Override 
     public void onLoadMore(int current_page) { 
      Log.e(TAG,"Scroll Count ==> "+(++mIntScrollCount)); 
      if(!flagFirstLoad) { 
       mOffset = mOffset + mLimit; 
       getRideList(mOffset, mLimit); 
      } 
     } 
    }); 

這是無限循環視圖滾動列表程序的代碼。

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener { 
public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName(); 

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

private int current_page = 1; 

private LinearLayoutManager mLinearLayoutManager; 

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

/** 
* function to reset values of the properties of this class to initial 
*/ 
public void resetValues(){ 
    previousTotal = 0; 
    loading = true; 
    visibleThreshold = 1; 
    firstVisibleItem = 0; 
    visibleItemCount = 0; 
    totalItemCount = 0; 
} 

@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)) { 
     // End has been reached 

     // Do something 
     current_page++; 

     onLoadMore(current_page); 

     loading = true; 
    } 
} 

public abstract void onLoadMore(int current_page);} 

所以,現在每當我滾動到列表onLoadMore的底部()被調用兩次,甚至intialy當我打電話getRideList()首次被越來越調用onScroll。我不明白爲什麼?

回答

0

更新了我的支持庫,現在它的工作正常。

+1

請提及您添加或更新的版本和/或行將會有用。 – Sufian