@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); LinearLayoutManager mLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); lastVisibleItem = mLayoutManager.findLastVisibleItemPosition(); pulldown = (dy > 0); }
/** * This method takes layout manager and finds the right center element of it * * @param layoutManager * @return View */ private View findCenterView(LinearLayoutManager layoutManager) { int minDistance = 0; View view, returnView = null; boolean notFound = true; for (int i = layoutManager.findFirstVisibleItemPosition(); i <= layoutManager.findLastVisibleItemPosition() && notFound; i++) { view = layoutManager.findViewByPosition(i); int center = (view.getLeft() + view.getRight()) / 2; int leastDifference = Math.abs(mCenterPivot - center); if (leastDifference <= minDistance || i == layoutManager.findFirstVisibleItemPosition()) { minDistance = leastDifference; returnView = view; } else { notFound = false; } } return returnView; }
private void FadingOutOtherItems(PopupMenu popupMenu) { // Dim out all the other list items if they exist LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager(); int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition(); int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition(); for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) { if (i == mHolder.getLayoutPosition()) continue; RecyclerViewHolder viewHolder = (RecyclerViewHolder) mRecyclerView.findViewHolderForLayoutPosition(i); View itemView = viewHolder.getItemView(); itemView.clearAnimation(); itemView.startAnimation(mFadeOut); } // Make sure to bring them back to normal after the menu is gone popupMenu.setOnDismissListener( new PopupMenu.OnDismissListener() { @Override public void onDismiss(PopupMenu popupMenu) { LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager(); int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition(); int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition(); for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; i++) { if (i == mHolder.getLayoutPosition()) continue; RecyclerViewHolder viewHolder = (RecyclerViewHolder) mRecyclerView.findViewHolderForLayoutPosition(i); View itemView = viewHolder.getItemView(); viewHolder.getLayoutPosition(); itemView.clearAnimation(); itemView.startAnimation(mFadeIn); } } }); }
private View findCenterView(LinearLayoutManager lm) { int minDistance = 0; View view = null; View returnView = null; boolean notFound = true; for (int i = lm.findFirstVisibleItemPosition(); i <= lm.findLastVisibleItemPosition() && notFound; i++) { view = lm.findViewByPosition(i); int center = lm.getOrientation() == LinearLayoutManager.HORIZONTAL ? (view.getLeft() + view.getRight()) / 2 : (view.getTop() + view.getBottom()) / 2; int leastDifference = Math.abs(mCenterPivot - center); if (leastDifference <= minDistance || i == lm.findFirstVisibleItemPosition()) { minDistance = leastDifference; returnView = view; setCenterView(returnView); } else { notFound = false; } } return returnView; }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { if (dy == 0 || recyclerView.getChildCount() <= 0) return; LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); int firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition(); int lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); int visibleItemCount = lastVisibleItem - firstVisibleItem; int totalItemCount = recyclerView.getAdapter().getItemCount(); NewsListRecyclerAdapter adapter = (NewsListRecyclerAdapter) recyclerView.getAdapter(); // Set the item at top to read ViewHolder vh = (ViewHolder) recyclerView.findViewHolderForLayoutPosition(firstVisibleItem); if (vh != null && !vh.shouldStayUnread()) { adapter.ChangeReadStateOfItem(vh, true); } // Check if Listview is scrolled to bottom if (lastVisibleItem == (totalItemCount - 1) && recyclerView.getChildAt(visibleItemCount).getBottom() <= recyclerView.getHeight()) { for (int i = firstVisibleItem + 1; i <= lastVisibleItem; i++) { vh = (ViewHolder) recyclerView.findViewHolderForLayoutPosition(i); if (vh != null && !vh.shouldStayUnread()) { adapter.ChangeReadStateOfItem(vh, true); } } } }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { if (dy > 0 && viewListLayoutManager.findLastVisibleItemPosition() == viewListLayoutManager.getItemCount() - 1) { presenter.onScrolledToBottom(); } }
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); int lastVisibleItem = mLayoutManager.findLastVisibleItemPosition(); int totalItemCount = mLayoutManager.getItemCount(); if (lastVisibleItem >= totalItemCount - 4 && dy > 0) { if (mState == STATE_NONE && mAdapter != null && mAdapter.getDataSize() > 0) { loadMore(); } } };
/** Close all opened items */ void closeOpenedItems() { if (opened != null) { int start = mLayoutManager.findFirstVisibleItemPosition(); int end = mLayoutManager.findLastVisibleItemPosition(); for (int i = start; i <= end; i++) { if (opened.get(i)) { closeAnimate(swipeRecyclerView.getChildAt(i - start).findViewById(swipeFrontView), i); } } } }
private void closeOtherOpenedItems() { if (opened != null && downPosition != SwipeRecyclerView.NO_POSITION) { int start = mLayoutManager.findFirstVisibleItemPosition(); int end = mLayoutManager.findLastVisibleItemPosition(); for (int i = start; i <= end; i++) { if (opened.get(i) && i != downPosition) { closeAnimate(swipeRecyclerView.getChildAt(i - start).findViewById(swipeFrontView), i); } } } }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition(); int itemCount = recyclerView.getAdapter().getItemCount(); if (itemCount - lastVisibleItemPosition <= visibleThreshold) { if (onMoreListener != null) { onMoreListener.onMore(); } } }
/** Unselected choice state in item */ protected void unselectedChoiceStates() { int start = mLayoutManager.findFirstVisibleItemPosition(); int end = mLayoutManager.findLastVisibleItemPosition(); for (int i = 0; i < checked.size(); i++) { if (checked.get(i) && i >= start && i <= end) { reloadChoiceStateInView( swipeRecyclerView.getChildAt(i - start).findViewById(swipeFrontView), i); } checked.set(i, false); } swipeRecyclerView.onChoiceEnded(); returnOldActions(); }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); currentPosition = layoutManager.findLastVisibleItemPosition(); if (mPostsEntityList == null || mPostsEntityList.isEmpty() || mRefreshing || mLoading) { return; } if (currentPosition + 4 >= mPostsEntityList.size()) { mSwipeRefresh.setRefreshing(true); mLoading = true; mRequestType = ConstData.REQUEST_LOAD; dayBefore--; requestContent(); } }
/** * Dismiss an item. * * @param position is the position of the item to delete. * @return 0 if the item is not visible. Otherwise return the height of the cell to dismiss. */ protected int dismiss(int position) { opened.remove(position); checked.remove(position); int start = mLayoutManager.findFirstVisibleItemPosition(); int end = mLayoutManager.findLastVisibleItemPosition(); View view = swipeRecyclerView.getChildAt(position - start); ++dismissAnimationRefCount; if (position >= start && position <= end) { performDismiss(view, position, false); return view.getHeight(); } else { pendingDismisses.add(new PendingDismissData(position, null)); return 0; } }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); int lastVisibleItem = 0; int firstVisibleItem = 0; RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); int totalItemCount = layoutManager.getItemCount(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) layoutManager); lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); firstVisibleItem = linearLayoutManager.findFirstCompletelyVisibleItemPosition(); } else if (layoutManager instanceof GridLayoutManager) { GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager); // 通过LayoutManager找到当前显示的最后的item的position lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition(); firstVisibleItem = gridLayoutManager.findFirstCompletelyVisibleItemPosition(); } else if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = ((StaggeredGridLayoutManager) layoutManager); // 因为StaggeredGridLayoutManager的特殊性可能导致最后显示的item存在多个,所以这里取到的是一个数组 // 得到这个数组后再取到数组中position值最大的那个就是最后显示的position值了 int[] lastPositions = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()]; staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); lastVisibleItem = findMax(lastPositions); firstVisibleItem = staggeredGridLayoutManager.findFirstVisibleItemPositions(lastPositions)[0]; } if (firstVisibleItem == 0) { if (!mPullLoadMoreRecyclerView.isLoadMore()) { mPullLoadMoreRecyclerView.setPullRefreshEnable(true); } } else { mPullLoadMoreRecyclerView.setPullRefreshEnable(false); } /** 无论水平还是垂直 */ if (!mPullLoadMoreRecyclerView.isRefresh() && mPullLoadMoreRecyclerView.isHasMore() && (lastVisibleItem >= totalItemCount - 1) && !mPullLoadMoreRecyclerView.isLoadMore() && (dx > 0 || dy > 0)) { mPullLoadMoreRecyclerView.setIsLoadMore(true); mPullLoadMoreRecyclerView.loadMore(); } }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); totalItemCount = recyclerView.getAdapter().getItemCount(); lastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition(); if (loading) { if (totalItemCount > previousTotal) { loading = false; previousTotal = totalItemCount; } } if (!loading && (totalItemCount <= (lastVisibleItem + VISIBLE_THRESHOLD))) { loading = true; onLoadMore(); } }
@Override public void onScrollStateChanged( android.support.v7.widget.RecyclerView recyclerView, int newState) { LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); if (newState == RecyclerView.SCROLL_STATE_IDLE && layoutManager.findLastVisibleItemPosition() + 1 == layoutManager.getItemCount()) { // 如果滚动到最后一行 if (autoLoadMore) { if (hasMoreData) { if (!isLoading()) { // 如果网络可以用 if (needCheckNetwork && !NetworkUtils.hasNetwork(context)) { mLoadMoreView.showFail(); } else { loadMore(); } } } } } }
public static boolean canRecyclerViewScroll(RecyclerView view) { if (view == null || view.getAdapter() == null || view.getLayoutManager() == null) return false; final RecyclerView.LayoutManager lm = view.getLayoutManager(); final int count = view.getAdapter().getItemCount(); int lastVisible; if (lm instanceof LinearLayoutManager) { LinearLayoutManager llm = (LinearLayoutManager) lm; lastVisible = llm.findLastVisibleItemPosition(); } else { throw new MaterialDialog.NotImplementedException( "Material Dialogs currently only supports LinearLayoutManager. Please report any new layout managers."); } if (lastVisible == -1) return false; /* We scroll if the last item is not visible */ final boolean lastItemVisible = lastVisible == count - 1; return !lastItemVisible || (view.getChildCount() > 0 && view.getChildAt(view.getChildCount() - 1).getBottom() > view.getHeight() - view.getPaddingBottom()); }
private int calculateLastVisiblePosition(LayoutManager layoutManager) { if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; return linearLayoutManager.findLastVisibleItemPosition(); } if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int[] lastVisibleItemPositions = staggeredGridLayoutManager.findLastVisibleItemPositions(null); int max = -1; for (int i = 0; i < lastVisibleItemPositions.length; i++) { max = Math.max(max, lastVisibleItemPositions[i]); Log.e(TAG, "lastVisibleItemPositions" + i + lastVisibleItemPositions[i]); } return max; } return -1; }
private void scrollChat(int itemCountBeforeUpdate) { int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition(); if (lastVisibleItemPosition == -1 || lastVisibleItemPosition == (itemCountBeforeUpdate - 1)) { scrollDown(); } }
@Override public boolean handleKeyboardShortcutRepeat( @NonNull final KeyboardShortcutsHandler handler, final int keyCode, final int repeatCount, @NonNull final KeyEvent event, int metaState) { final String action = handler.getKeyAction(CONTEXT_TAG_NAVIGATION, keyCode, event, metaState); if (action == null) return false; final int direction; switch (action) { case ACTION_NAVIGATION_PREVIOUS: { direction = -1; break; } case ACTION_NAVIGATION_NEXT: { direction = 1; break; } case ACTION_NAVIGATION_PAGE_DOWN: { RecyclerViewUtils.pageScroll(view, manager, 1); return true; } case ACTION_NAVIGATION_PAGE_UP: { RecyclerViewUtils.pageScroll(view, manager, -1); return true; } default: { return false; } } final View focusedChild = RecyclerViewUtils.findRecyclerViewChild(view, manager.getFocusedChild()); final int position; final int firstVisibleItemPosition = manager.findFirstVisibleItemPosition(); final int lastVisibleItemPosition = manager.findLastVisibleItemPosition(); final int itemCount = adapter.getItemCount(); final boolean backupOutsideRange = positionBackup > lastVisibleItemPosition || positionBackup < firstVisibleItemPosition; if (focusedChild != null) { position = view.getChildLayoutPosition(focusedChild); } else if (firstVisibleItemPosition == 0) { position = -1; } else if (lastVisibleItemPosition == itemCount - 1) { position = itemCount; } else if (direction > 0 && backupOutsideRange) { position = firstVisibleItemPosition; } else if (direction < 0 && backupOutsideRange) { position = lastVisibleItemPosition; } else { position = positionBackup; } positionBackup = position; RecyclerViewUtils.focusNavigate(view, manager, position, direction); return true; }