Java Code Examples for android.support.v7.widget.RecyclerView#smoothScrollBy()

The following examples show how to use android.support.v7.widget.RecyclerView#smoothScrollBy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From DouYinWu with MIT License 6 votes vote down vote up
/**
 * 平滑的滑动到指定位置
 */
private void smoothMoveToPosition(RecyclerView mRecyclerView, final int position) {
    // 第一个可见位置
    int firstItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(0));
    // 最后一个可见位置
    int lastItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(mRecyclerView.getChildCount() - 1));
    if (position < firstItem) {
        // 第一种可能:跳转位置在第一个可见位置之前
        mRecyclerView.smoothScrollToPosition(position);
    } else if (position <= lastItem) {
        //滑动指定高度
        mRecyclerView.smoothScrollBy(0, GetScreenWinth.getHeight(this) -
                (Resources.getSystem().getDimensionPixelSize(Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"))));
    } else {
        // 第三种可能:跳转位置在最后可见项之后
        mRecyclerView.smoothScrollToPosition(position);
    }
}
 
Example 2
Source File: NavigationFragment.java    From MaoWanAndoidClient with Apache License 2.0 6 votes vote down vote up
private void moveToPosition(LinearLayoutManager layoutManager, RecyclerView recyclerView, int position) {
    // 第一个可见的view的位置
    int firstItem = layoutManager.findFirstVisibleItemPosition();
    // 最后一个可见的view的位置
    int lastItem = layoutManager.findLastVisibleItemPosition();
    if (position <= firstItem) {
        // 如果跳转位置firstItem 之前(滑出屏幕的情况),就smoothScrollToPosition可以直接跳转,
        recyclerView.smoothScrollToPosition(position);
    } else if (position <= lastItem) {
        // 跳转位置在firstItem 之后,lastItem 之间(显示在当前屏幕),smoothScrollBy来滑动到指定位置
        int top = recyclerView.getChildAt(position - firstItem).getTop();
        recyclerView.smoothScrollBy(0, top);
    } else {
        // 如果要跳转的位置在lastItem 之后,则先调用smoothScrollToPosition将要跳转的位置滚动到可见位置
        // 再通过onScrollStateChanged控制再次调用当前moveToPosition方法,执行上一个判断中的方法
        recyclerView.smoothScrollToPosition(position);
        scrollToPosition = position;
        canScroll = true;
    }
}
 
Example 3
Source File: CenterScrollListener.java    From CarouselLayoutManager with Apache License 2.0 6 votes vote down vote up
@Override
public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (!(layoutManager instanceof CarouselLayoutManager)) {
        mAutoSet = true;
        return;
    }

    final CarouselLayoutManager lm = (CarouselLayoutManager) layoutManager;
    if (!mAutoSet) {
        if (RecyclerView.SCROLL_STATE_IDLE == newState) {
            final int scrollNeeded = lm.getOffsetCenterView();
            if (CarouselLayoutManager.HORIZONTAL == lm.getOrientation()) {
                recyclerView.smoothScrollBy(scrollNeeded, 0);
            } else {
                recyclerView.smoothScrollBy(0, scrollNeeded);
            }
            mAutoSet = true;
        }
    }
    if (RecyclerView.SCROLL_STATE_DRAGGING == newState || RecyclerView.SCROLL_STATE_SETTLING == newState) {
        mAutoSet = false;
    }
}
 
Example 4
Source File: BannerLayoutManager.java    From RecyclerBanner with Apache License 2.0 5 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    final int offsetPosition = getOffsetToPosition(position);
    if (mOrientation == VERTICAL) {
        recyclerView.smoothScrollBy(0, offsetPosition, mSmoothScrollInterpolator);
    } else {
        recyclerView.smoothScrollBy(offsetPosition, 0, mSmoothScrollInterpolator);
    }
}
 
Example 5
Source File: CenterScrollListener.java    From RecyclerBanner with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  
    final OverFlyingLayoutManager.OnPageChangeListener onPageChangeListener = ((OverFlyingLayoutManager) layoutManager).onPageChangeListener;
    if (onPageChangeListener != null) {
        onPageChangeListener.onPageScrollStateChanged(newState);
    }

    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
        if (mAutoSet) {
            if (onPageChangeListener != null) {
                onPageChangeListener.onPageSelected(((OverFlyingLayoutManager) layoutManager).getCurrentPosition());
            }
            mAutoSet = false;
        } else {
            final int delta;
            delta = ((OverFlyingLayoutManager) layoutManager).getOffsetToCenter();
            if (delta != 0) {
                if (((OverFlyingLayoutManager) layoutManager).getOrientation() == OverFlyingLayoutManager.VERTICAL)
                    recyclerView.smoothScrollBy(0, delta);
                else
                    recyclerView.smoothScrollBy(delta, 0);
                mAutoSet = true;
            } else {
                if (onPageChangeListener != null) {
                    onPageChangeListener.onPageSelected(((OverFlyingLayoutManager) layoutManager).getCurrentPosition());
                }
                mAutoSet = false;
            }
        }
    } else if (newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING) {
        mAutoSet = false;
    }
}
 
Example 6
Source File: CenterScrollListener.java    From CarouselBanner with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (!(layoutManager instanceof CoolBannerLayoutManager)) {
        mAutoSet = true;
        return;
    }

    final CoolBannerLayoutManager.OnPageChangeListener onPageChangeListener = ((CoolBannerLayoutManager) layoutManager).onPageChangeListener;
    if (onPageChangeListener != null) {
        onPageChangeListener.onPageScrollStateChanged(newState);
    }

    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
        if (mAutoSet) {
            if (onPageChangeListener != null) {
                onPageChangeListener.onPageSelected(((CoolBannerLayoutManager) layoutManager).getCurrentPosition());
            }
            mAutoSet = false;
        } else {
            final int delta;
            delta = ((CoolBannerLayoutManager) layoutManager).getOffsetToCenter();
            if (delta != 0) {
                if (((CoolBannerLayoutManager) layoutManager).getOrientation() == CoolBannerLayoutManager.VERTICAL)
                    recyclerView.smoothScrollBy(0, delta);
                else
                    recyclerView.smoothScrollBy(delta, 0);
                mAutoSet = true;
            } else {
                if (onPageChangeListener != null) {
                    onPageChangeListener.onPageSelected(((CoolBannerLayoutManager) layoutManager).getCurrentPosition());
                }
                mAutoSet = false;
            }
        }
    } else if (newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING) {
        mAutoSet = false;
    }
}
 
Example 7
Source File: ScrollHelper.java    From ViewPagerLayoutManager with Apache License 2.0 5 votes vote down vote up
static void smoothScrollToPosition(RecyclerView recyclerView, ViewPagerLayoutManager viewPagerLayoutManager, int targetPosition) {
    final int delta = viewPagerLayoutManager.getOffsetToPosition(targetPosition);
    if (viewPagerLayoutManager.getOrientation() == ViewPagerLayoutManager.VERTICAL) {
        recyclerView.smoothScrollBy(0, delta);
    } else {
        recyclerView.smoothScrollBy(delta, 0);
    }
}
 
Example 8
Source File: CarouselLayoutManager.java    From carouselview with MIT License 4 votes vote down vote up
@Override
	public void smoothScrollToPosition(final RecyclerView recyclerView, final RecyclerView.State state,
	                                   int position) {
//		LinearSmoothScroller linearSmoothScroller =
//				new LinearSmoothScroller(recyclerView.getContext()) {
//					@Override
//					public PointF computeScrollVectorForPosition(int targetPosition) {
//						return CarouselLayoutManager.this
//								.computeScrollVectorForPosition(targetPosition);
//					}
//				};
//		linearSmoothScroller.setTargetPosition(position);
//		startSmoothScroll(linearSmoothScroller);
		log("smoothScrollToPosition " + position + " " + recyclerView);
		int minScrollOffset = Integer.MAX_VALUE;
		final int nChilds = getItemCount();

		if (mDecoratedChildWidth == 0 && nChilds > 0) {
			final int finalPosition = position;
			mPendingTasks.add(new Runnable() {
				@Override
				public void run() {
					smoothScrollToPosition(recyclerView, state, finalPosition);
				}
			});
			return;
		}
		if (mDecoratedChildWidth * nChilds == 0) return;

		if (!isInfinite()) {
			position = Math.max(0, Math.min(nChilds - 1, position));
		} else {
			position %= nChilds;
		}

		for (int round = -1; round <= 1; ++round) {
			if (isInfinite() || round == 0) {
				int offset = ((position + round * nChilds) * mDecoratedChildWidth) - (mScrollOffset % (mDecoratedChildWidth * nChilds));
				if (Math.abs(offset) < Math.abs(minScrollOffset))
					minScrollOffset = offset;
			}
		}
		recyclerView.smoothScrollBy(minScrollOffset, 0);
	}