@Override public int findTargetSnapPosition(RecyclerView.LayoutManager lm, int velocityX, int velocityY) { final int itemCount = lm.getItemCount(); if (itemCount == 0) { return RecyclerView.NO_POSITION; } final int childCount = lm.getChildCount(); if (childCount == 0) { return RecyclerView.NO_POSITION; } final boolean leftDirection = velocityX > 0; if (leftDirection) { final View leftChild = lm.getChildAt(0); final int leftChildPos = lm.getPosition(leftChild); final int leftChildRight = lm.getDecoratedRight(leftChild); return Math.min(itemCount - 1, leftChildPos + (leftChildRight > 0 ? 1 : 2)); } else { final View rightChild = lm.getChildAt(childCount - 1); final int rightChildPos = lm.getPosition(rightChild); return Math.max(0, rightChildPos - 1); } }
@Override public void onDestroyView() { super.onDestroyView(); // 存储 PageIndex mConfig.saveTopicListPageIndex(pageIndex); // 存储 RecyclerView 滚动位置 View view = mRecyclerView.getLayoutManager().getChildAt(0); int lastPosition = mRecyclerView.getLayoutManager().getPosition(view); mConfig.saveTopicListState(lastPosition, 0); } }
@Override public void onDestroyView() { super.onDestroyView(); // 存储 PageIndex mConfig.saveNewsListPageIndex(pageIndex); // 存储 RecyclerView 滚动位置 View view = mRecyclerView.getLayoutManager().getChildAt(0); int lastPosition = mRecyclerView.getLayoutManager().getPosition(view); mConfig.saveNewsListPosition(lastPosition); } }
View snap = mSnapHelper.findSnapView(recyclerView.getLayoutManager()); if (snap != null) { int selectedPosition = recyclerView.getLayoutManager().getPosition(snap); if (selectedPosition != mCurSelectedPosition) { if (mCurSelectedView != null) {
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); View snap = mSnapHelper.findSnapView(recyclerView.getLayoutManager()); if (snap != null) { int selectedPosition = recyclerView.getLayoutManager().getPosition(snap); if (selectedPosition != mCurSelectedPosition) { if (mCurSelectedView != null) { mCurSelectedView.setSelected(false); } mCurSelectedView = snap; mCurSelectedView.setSelected(true); mCurSelectedPosition = selectedPosition; if (!mCallbackInFling && mState != SCROLL_STATE_IDLE) { if (BuildConfig.DEBUG) { Log.v(TAG, "ignore selection change callback when fling "); } mCallbackOnIdle = true; return; } if (mOnItemSelectedListener != null) { mOnItemSelectedListener.onItemSelected(recyclerView, snap, mCurSelectedPosition); } } } if (BuildConfig.DEBUG) { Log.v(TAG, "onScrolled: dx:" + dx + ",dy:" + dy); } }
@Override public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) { View centerView = findSnapView(layoutManager); if (centerView == null) return RecyclerView.NO_POSITION; int position = layoutManager.getPosition(centerView); int targetPosition = -1; if (layoutManager.canScrollHorizontally()) { if (velocityX < 0) { targetPosition = position - 1; } else { targetPosition = position + 1; } } if (layoutManager.canScrollVertically()) { if (velocityY < 0) { targetPosition = position - 1; } else { targetPosition = position + 1; } } final int firstItem = 0; final int lastItem = layoutManager.getItemCount() - 1; targetPosition = Math.min(lastItem, Math.max(targetPosition, firstItem)); if(targetPosition >= 0) recyclerSnapItemListener.onItemSnap(targetPosition); return targetPosition; }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); //得到当前显示的最后一个item的view View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount()-1); //得到lastChildView的bottom坐标值 int lastChildBottom = lastChildView.getBottom(); //得到Recyclerview的底部坐标减去底部padding值,也就是显示内容最底部的坐标 int recyclerBottom = recyclerView.getBottom()-recyclerView.getPaddingBottom(); //通过这个lastChildView得到这个view当前的position值 int lastVisiblePosition = recyclerView.getLayoutManager().getPosition(lastChildView); //判断lastChildView的bottom值跟recyclerBottom //判断lastPosition是不是最后一个position //如果两个条件都满足则说明是真正的滑动到了底部 /* Why <= ? */ int lastPosition = recyclerView.getLayoutManager().getItemCount() - 1; if(lastChildBottom == recyclerBottom && lastPosition == recyclerView.getLayoutManager().getItemCount()-1 ){ bottom = true; mSwipyRefreshLayout.setEnabled(true); }else{ bottom = false; } if (lastVisiblePosition >= lastPosition - 3) viewModel.tryToLoadMore(); } });
private int distanceToCenter(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView, OrientationHelper helper) { if (layoutManager.canScrollHorizontally()) { int totalWidth = mRecyclerView.getWidth(); int columnWidth = totalWidth / mColumn; int position = layoutManager.getPosition(targetView); int pageIndex = pageIndex(position); int currentPageStart = pageIndex * countOfpage(); int distance = ((position - currentPageStart) / mRow) * columnWidth; final int childStart = helper.getDecoratedStart(targetView); return childStart - distance; } else {//数值方向 int totalHeight = mRecyclerView.getHeight(); int rowHeight = totalHeight / mRow; int position = layoutManager.getPosition(targetView); int pageIndex = pageIndex(position); int currentPageStart = pageIndex * countOfpage(); int distance = ((position - currentPageStart) / mColumn) * rowHeight; final int childStart = helper.getDecoratedStart(targetView); return childStart - distance; } }
private int adjustDyForUp(int dy) { final RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManager == null || !layoutManager.canScrollVertically()) { return 0; } final View lastChild = layoutManager.getChildAt(layoutManager.getChildCount() - 1); final int position = layoutManager.getPosition(lastChild); if (position == layoutManager.getItemCount() - 1) { final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) lastChild.getLayoutParams(); final int maxDy = layoutManager.getHeight() - layoutManager.getPaddingBottom() - (layoutManager.getDecoratedBottom(lastChild) + params.bottomMargin); if (dy < maxDy) { return maxDy; } } return dy; }
private int adjustDxForLeft(int dx) { final RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManager == null || !layoutManager.canScrollHorizontally()) { return 0; } final View lastChild = layoutManager.getChildAt(layoutManager.getChildCount() - 1); final int position = layoutManager.getPosition(lastChild); if (position == layoutManager.getItemCount() - 1) { final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) lastChild.getLayoutParams(); final int maxDx = layoutManager.getWidth() - layoutManager.getPaddingRight() - (layoutManager.getDecoratedRight(lastChild) + params.rightMargin); if (dx < maxDx) { return maxDx; } } return dx; }
@Override public int[] calculateDistanceToFinalSnap( @NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) { if (layoutManager instanceof LadderLayoutManager) { int[] out = new int[2]; if (layoutManager.canScrollHorizontally()) { out[0] = ((LadderLayoutManager) layoutManager).calculateDistanceToPosition( layoutManager.getPosition(targetView)); out[1] = 0; } else { out[0] = 0; out[1] = ((LadderLayoutManager) layoutManager).calculateDistanceToPosition( layoutManager.getPosition(targetView)); } return out; } return null; }
private int adjustDyForDown(int dy) { final RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManager == null || !layoutManager.canScrollVertically()) { return 0; } final View firstChild = layoutManager.getChildAt(0); final int position = layoutManager.getPosition(firstChild); if (position == 0) { final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) firstChild.getLayoutParams(); final int maxDy = -(layoutManager.getDecoratedTop(firstChild) - params.topMargin) + layoutManager.getPaddingTop(); if (dy > maxDy) { return maxDy; } } return dy; }
private int adjustDxForRight(int dx) { final RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManager == null || !layoutManager.canScrollHorizontally()) { return 0; } final View firstChild = layoutManager.getChildAt(0); final int position = layoutManager.getPosition(firstChild); if (position == 0) { final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) firstChild.getLayoutParams(); final int maxDx = -(layoutManager.getDecoratedLeft(firstChild) - params.leftMargin) + layoutManager.getPaddingLeft(); if (dx > maxDx) { return maxDx; } } return dx; }
private boolean isFirstItemStayInLeft(View firstVisibilityView) { return getLayoutManager().getPosition(firstVisibilityView) == 0; }
public int getPosition(View view) { return layoutManager.getPosition(view); }
@Override public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) { int targetPos = super.findTargetSnapPosition(layoutManager, velocityX, velocityY); final View currentView = findSnapView(layoutManager); if (targetPos != RecyclerView.NO_POSITION && currentView != null) { int currentPos = layoutManager.getPosition(currentView); int first = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); int last = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); currentPos = targetPos < currentPos ? last : (targetPos > currentPos ? first : currentPos); targetPos = targetPos < currentPos ? currentPos - 1 : (targetPos > currentPos ? currentPos + 1 : currentPos); } return targetPos; } }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount() - 1); int lastChildBottom = lastChildView.getBottom(); int recyclerBottom = recyclerView.getBottom() - recyclerView.getPaddingBottom(); int lastPosition = recyclerView.getLayoutManager().getPosition(lastChildView); if (lastChildBottom == recyclerBottom && lastPosition == recyclerView.getLayoutManager().getItemCount() - 1) { if (canLoadMore) { loadPictures(id, false); } } } });
@Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { if (TYPE.equals(Constants.StoryType.STORY_THEME)) return; View lastchildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount() - 1); int lastChildBottomY = lastchildView.getBottom(); int recyclerBottomY = recyclerView.getBottom() - recyclerView.getPaddingBottom(); int lastPosition = recyclerView.getLayoutManager().getPosition(lastchildView); if (lastChildBottomY == recyclerBottomY && newState == RecyclerView.SCROLL_STATE_IDLE && lastPosition == recyclerView.getLayoutManager().getItemCount() - 1) { mPresenter.getBeforeDaily(mdate); } } });
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount() - 1); int lastChildBottom = lastChildView.getBottom(); int recyclerBottom = recyclerView.getBottom() - recyclerView.getPaddingBottom(); int lastPosition = recyclerView.getLayoutManager().getPosition(lastChildView); if (lastChildBottom == recyclerBottom && lastPosition == recyclerView.getLayoutManager().getItemCount() - 1) { loadVideo(false); } } });
return RecyclerView.NO_POSITION; final int firstBeforeStartPosition = layoutManager.getPosition(firstBeforeStartChild); if (firstBeforeStartPosition == RecyclerView.NO_POSITION) { return RecyclerView.NO_POSITION;