Android recyclerview无限滚动列表器onScrolled()被调用两次

时间:2016-03-01 06:03:18

标签: android android-recyclerview endlessscroll

这就是我设置recyclerview的方式。

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()被调用两次,甚至是当我第一次调用getRideList()onScroll被调用时。我不明白为什么?

1 个答案:

答案 0 :(得分:0)

更新了我的支持库,现在工作正常。