Android:在Recycler View上实施无限掠夺

时间:2015-09-24 01:07:28

标签: android android-recyclerview infinite-scroll

我的API网址为:https://zappos.amazon.com/mobileapi/v1/search?term=adidas&page=1
页面可以是1,2,3 ..... n
我在每个项目中使用网格布局在我的回收站视图中为10个项目充气 我已经实现了以下代码来处理我的Recycler视图上的无限项加载,但问题是:
在一次滚动之后,它继续触发API 这是我的代码:

EndlessRecyclerView

public abstract class EndlessRecyclerView extends RecyclerView.OnScrollListener {
    public static String TAG = EndlessRecyclerView.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 = 5; // 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 GridLayoutManager gridLayoutManager;

    public EndlessRecyclerView(GridLayoutManager gridLayoutManager) {
        this.gridLayoutManager = gridLayoutManager;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = gridLayoutManager.getItemCount();
        previousTotal = previousTotal + visibleItemCount;
        if(previousTotal>totalItemCount)
        {
            current_page++;
            onLoadMore(current_page);
            previousTotal = 0;
        }
    }



    public abstract void onLoadMore(int current_page);
}

主要活动中的RecyclerView Snippet

recyclerView.setOnScrollListener(new EndlessRecyclerView(gridLayoutManager) {
                @Override
                public void onLoadMore(int current_page) {
                   //async task that fires API and inflate recycler view
            });

1 个答案:

答案 0 :(得分:0)

1:我的解决方案是重写SwipeRefreshLayout(示例为here):

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ensureTarget();

    final int action = MotionEventCompat.getActionMasked(ev);

    if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
        mReturningToStart = false;
    }

    if (!isEnabled() || mReturningToStart || mRefreshing) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            if (canChildScrollUp()) {
            } else {
                setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true);
                mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
                mIsBeingDragged = false;
                final float initialMotionY = getMotionEventY(ev, mActivePointerId);
                if (initialMotionY == -1) {
                    return false;
                }
                mInitialMotionY = initialMotionY;
            }

        case MotionEvent.ACTION_MOVE:
            if (canChildScrollUp()) {
            } else {
                if (mActivePointerId == INVALID_POINTER) {
                    Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id.");
                    return false;
                }

                final float y = getMotionEventY(ev, mActivePointerId);
                if (y == -1) {
                    return false;
                }
                final float yDiff = y - mInitialMotionY;
                if (yDiff > mTouchSlop && !mIsBeingDragged) {
                    mIsBeingDragged = true;
                    mProgress.setAlpha(STARTING_PROGRESS_ALPHA);
                }
                break;
            }

        case MotionEventCompat.ACTION_POINTER_UP:
            if (canChildScrollUp()) {
            } else {
                onSecondaryPointerUp(ev);
                break;
            }

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            if (canChildScrollUp())
            {
                if(mTarget instanceof AbsListView)
                {
                    final AbsListView absListView = (AbsListView) mTarget;

                    int lastPosition=absListView.getLastVisiblePosition();
                    if(lastPosition+1==absListView.getCount())
                    {
                        if(mCanLoadMore)
                        {
                            mCanLoadMore=false;
                            mLoadMoreListener.loadMore();
                        }
                    }
                }else if(mTarget instanceof RecyclerView)
                {
                    final RecyclerView recyclerView=(RecyclerView)mTarget;
                    LinearLayoutManager lLManager=(LinearLayoutManager)recyclerView.getLayoutManager();
                    if((lLManager.findLastVisibleItemPosition()+1)==lLManager.getItemCount())
                    {
                        if(mCanLoadMore)
                        {
                            mCanLoadMore=false;
                            mLoadMoreListener.loadMore();
                        }
                    }
                }
            } else {
                mIsBeingDragged = false;
                mActivePointerId = INVALID_POINTER;
                break;
            }
    }

    return mIsBeingDragged;
}

2:我认为您的解决方案可以这样:

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

    int visibleItemCount=gridLayoutManager.findLastVisibleItemPosition();
    if((visibleItemCount+6)>totalItemCount)
    {
        current_page++;
        onLoadMore(current_page);
    }
}