从底部滑动时,Recycler视图将获取数据

时间:2018-07-23 12:33:31

标签: android android-recyclerview

我想在用户触摸回收站时获取数据,并在回收站已经位于最底端时向下滚动(就像从底部滑动一样,但是当回收站无法再滚动时)。有什么想法吗?

谢谢

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

       int visibleItemCount = manager.getChildCount();
       int totalItemCount = manager.getItemCount();
       int[] firstVisibleItems = manager.findFirstVisibleItemPositions(null);
       if (firstVisibleItems != null && firstVisibleItems.length > 0) {

        }

       if (visibleItemCount + pastVisibleItems >= totalItemCount) {

     } 

 }

2 个答案:

答案 0 :(得分:0)

尝试一下:

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

   if(manager.findLastVisibleItemPosition() == manager.getItemCount()-1) 
   {
        // We have reached the end of the recycler view.
   }
}

答案 1 :(得分:-1)

检查此开源库,例如: https://github.com/Malinskiy/SuperRecyclerView

它具有setupMoreLoading方法,当X元素要滚动时,它会附加侦听器和调用方法。

这是在这里完成的方式:

     ...
     mInternalOnScrollListener = new RecyclerView.OnScrollListener() {

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

            processOnMore();

            if (mExternalOnScrollListener != null)
                mExternalOnScrollListener.onScrolled(recyclerView, dx, dy);
            if (mSwipeDismissScrollListener != null)
                mSwipeDismissScrollListener.onScrolled(recyclerView, dx, dy);
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (mExternalOnScrollListener != null)
                mExternalOnScrollListener.onScrollStateChanged(recyclerView, newState);
            if (mSwipeDismissScrollListener != null)
                mSwipeDismissScrollListener.onScrollStateChanged(recyclerView, newState);
        }
    };
    mRecycler.addOnScrollListener(mInternalOnScrollListener);
    ....

private void processOnMore() {
    RecyclerView.LayoutManager layoutManager = mRecycler.getLayoutManager();
    int lastVisibleItemPosition = getLastVisibleItemPosition(layoutManager);
    int visibleItemCount = layoutManager.getChildCount();
    int totalItemCount = layoutManager.getItemCount();

    if (((totalItemCount - lastVisibleItemPosition) <= ITEM_LEFT_TO_LOAD_MORE ||
         (totalItemCount - lastVisibleItemPosition) == 0 && totalItemCount > visibleItemCount)
        && !isLoadingMore) {

        isLoadingMore = true;
        if (mOnMoreListener != null) {
            mMoreProgress.setVisibility(View.VISIBLE);
            mOnMoreListener.onMoreAsked(mRecycler.getAdapter().getItemCount(), ITEM_LEFT_TO_LOAD_MORE, lastVisibleItemPosition);
        }
    }
}