RecyclerView + HidingScrollListener +加载更多

时间:2015-10-15 13:10:44

标签: android android-recyclerview endlessscroll

我使用recyclerview隐藏滚动侦听器。当我向下滚动recyclerview并释放滚动时,如果加载更多并向插入项目范围的适配器添加新内容,我之前添加的行或新添加的不响应click。但是当我再次滚动一点点我所有的点击尝试都有效。

这是我的HidingScrollListener

public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {

   private static final float HIDE_THRESHOLD = 10;
   private static final float SHOW_THRESHOLD = 70;

   private int                 mToolbarOffset   = 0;
   private boolean             mControlsVisible = true;
   private int                 mToolbarHeight;
   private int                 mTotalScrolledDistance;
   private int                 previousTotal    = 0;
   private boolean             loading          = true;
   private int                 visibleThreshold = 4;
   int                         firstVisibleItem, visibleItemCount, totalItemCount;
   private LinearLayoutManager layoutManager;

   public HidingScrollListener(Context context, LinearLayoutManager layoutManager) {
      mToolbarHeight = Tools.getFooterHeight(context);
      this.layoutManager = layoutManager;
   }

   @Override
   public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
      super.onScrollStateChanged(recyclerView, newState);

      if (newState == RecyclerView.SCROLL_STATE_IDLE) {
         if (mTotalScrolledDistance < mToolbarHeight) {
            setVisible();
         }
         else {
            if (mControlsVisible) {
               if (mToolbarOffset > HIDE_THRESHOLD) {
                  setInvisible();
               }
               else {
                  setVisible();
               }
            }
            else {
               if ((mToolbarHeight - mToolbarOffset) > SHOW_THRESHOLD) {
                  setVisible();
               }
               else {
                  setInvisible();
               }
            }
         }
      }

   }

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

      clipToolbarOffset();
      onMoved(mToolbarOffset);

      if ((mToolbarOffset < mToolbarHeight && dy > 0) || (mToolbarOffset > 0 && dy < 0)) {
         mToolbarOffset += dy;
      }
      if (mTotalScrolledDistance < 0) {
         mTotalScrolledDistance = 0;
      }
      else {
         mTotalScrolledDistance += dy;
      }
      // for load more
      visibleItemCount = recyclerView.getChildCount();
      totalItemCount = layoutManager.getItemCount();
      firstVisibleItem = layoutManager.findFirstVisibleItemPosition();

      if (loading) {
         if (totalItemCount > previousTotal) {
            loading = false;
            previousTotal = totalItemCount;
         }
      }
      if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
         // End has been reached
         // Do something

         loading = true;
         onLoadMore();
      }
   }

   private void clipToolbarOffset() {
      if (mToolbarOffset > mToolbarHeight) {
         mToolbarOffset = mToolbarHeight;
      }
      else if (mToolbarOffset < 0) {
         mToolbarOffset = 0;
      }
   }

   private void setVisible() {
      if (mToolbarOffset > 0) {
         onShow();
         mToolbarOffset = 0;
      }
      mControlsVisible = true;
   }

   private void setInvisible() {
      if (mToolbarOffset < mToolbarHeight) {
         onHide();
         mToolbarOffset = mToolbarHeight;
      }
      mControlsVisible = false;
   }

   public abstract void onMoved(int distance);

   public abstract void onShow();

   public abstract void onHide();

   public abstract void onLoadMore();
}

onLoadMore()

mAdapter.notifyItemRangeInserted(size, data.size());

我无法弄清楚为什么会发生这种情况。这很奇怪。感谢。

1 个答案:

答案 0 :(得分:0)

当我尝试使用notifyItemRangeChanged()的{​​{1}}方法时,我会通知错误的范围。

这导致阻止Ui线程,所以在中断ui线程之前我无法获取任何操作。

当我为通知适配器设置有效的项目范围时,这已得到修复。

我希望我的回答可以帮助别人。

正确的电话如下:

RecyclerViewAdapter