当我们滚动到Android中的RecyclerView中的最后位置时通知

时间:2017-04-10 09:47:01

标签: android scroll android-recyclerview position

我的应用有一个水平滚动的回收视图。 当我滚动列表并到达最后位置时,我想触发另一个事件。

3 个答案:

答案 0 :(得分:1)

只需在recyclerview上实现addOnScrollListener()。然后在滚动侦听器内部实现下面的代码。

RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if (mIsLoading)
            return;
        int visibleItemCount = mLayoutManager.getChildCount();
        int totalItemCount = mLayoutManager.getItemCount();
        int pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
        if (pastVisibleItems + visibleItemCount >= totalItemCount) {
            //End of list
        }
    }
};

答案 1 :(得分:0)

 private RecyclerView.OnScrollListener nRecyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        int visibleItemCount = notificationLayoutManager.getChildCount();
        int totalItemCount = notificationLayoutManager.getItemCount();
        int firstVisibleItemPosition = notificationLayoutManager.findFirstVisibleItemPosition();
        if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount) {
            nLoadMoreItems();

        }
    }
};

private void nLoadMoreItems() {
    if (defaultCurrentPage < defaultpageCount)
        layout_load_more_loadingNotification.setVisibility(View.VISIBLE);
    int pageCount = (defaultCurrentPage + 1);
   //fetch second page data
      }

试试吧

答案 2 :(得分:0)

看到所有答案后,我通过这样做解决了我的问题,

 private RecyclerView.OnScrollListener nRecyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == 2) {
            try {
                if (lastVisibleItemPosition + 1 == totalItemCount) {
                    //Write you code here
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.i(TAG, e.getMessage());
            }
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        totalItemCount = mLayoutManager.getItemCount();
        lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();

    }
};

将监听器添加到回收站视图

recyclerView.addOnScrollListener(nRecyclerViewOnScrollListener);