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

The following examples show how to use android.support.v7.widget.RecyclerView#smoothScrollToPosition() . 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: WindowControl.java    From Fairy with Apache License 2.0 6 votes vote down vote up
public WindowControl(Context context) {
    this.context = context;
    wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    rootView = LayoutInflater.from(context).inflate(R.layout.window_display, null);
    mRecycleView = (RecyclerView) rootView;
    mRecycleView.setLayoutManager(new LinearLayoutManager(context));
    mAdapter = new LogcatAdapter(context);
    mRecycleView.setAdapter(mAdapter);

    observer = content -> {
        if (content != null) {
            if (isFirst) {
                content.setContent("init window");
            }

            if (!TextUtils.isEmpty(content.getContent())) {
                mAdapter.addData(content);
                mRecycleView.smoothScrollToPosition(mAdapter.getItemCount() - 1);
            }

            isFirst = false;
        }
    };
}
 
Example 4
Source File: BannerLayoutManager.java    From RecyclerBanner with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onAddFocusables(RecyclerView recyclerView, ArrayList<View> views, int direction, int focusableMode) {
    final int currentPosition = getCurrentPosition();
    final View currentView = findViewByPosition(currentPosition);
    if (currentView == null) return true;
    if (recyclerView.hasFocus()) {
        final int movement = getMovement(direction);
        if (movement != DIRECTION_NO_WHERE) {
            final int targetPosition = movement == DIRECTION_BACKWARD ?
                    currentPosition - 1 : currentPosition + 1;
            recyclerView.smoothScrollToPosition(targetPosition);
        }
    } else {
        currentView.addFocusables(views, direction, focusableMode);
    }
    return true;
}
 
Example 5
Source File: CityWeatherActivity.java    From LittleFreshWeather with Apache License 2.0 6 votes vote down vote up
@Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (RecyclerView.SCROLL_STATE_IDLE == newState) {
                View view = recyclerView.getChildAt(0);
//                Rect rect = new Rect();
//                Point point = new Point();
//                recyclerView.getChildVisibleRect(view, rect, point);
                if ((int)view.getTag() == CityWeatherAdapter.VIEW_MAIN) {
                    if (mScrollDown) {
                        recyclerView.smoothScrollToPosition(1);
                    } else {
                        recyclerView.smoothScrollToPosition(0);
                    }
                }
            }
        }
 
Example 6
Source File: FastScrollToTop.java    From RecyclerViewTools with Apache License 2.0 6 votes vote down vote up
public FastScrollToTop(RecyclerView recyclerView, int threshold, int lastScroll, long scrollToPositionDelay) {
   this.lastScroll = lastScroll;

   if (recyclerView.getChildCount() == 0) {
      weakRecycler = null;
      return;
   }

   View v = recyclerView.getChildAt(0);
   int position = recyclerView.getChildAdapterPosition(v);
   if (position < threshold) {
      recyclerView.smoothScrollToPosition(0);
      weakRecycler = null;
      return;
   }

   // here is where the fun happens
   weakRecycler = new WeakReference<>(recyclerView);
   recyclerView.smoothScrollToPosition(0);
   recyclerView.postDelayed(runAfterSpeedUp, scrollToPositionDelay);
}
 
Example 7
Source File: OnLineMusicFragment.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
public void onRefresh() {
    RecyclerView mRecyclerView = this.recyclerView.getRecyclerView();
    int firstVisibleItemPosition = ((LinearLayoutManager)
            mRecyclerView.getLayoutManager()).findFirstVisibleItemPosition();
    if (firstVisibleItemPosition == 0) {
        onLazyLoad();
        return;
    }
    mRecyclerView.scrollToPosition(5);
    mRecyclerView.smoothScrollToPosition(0);
}
 
Example 8
Source File: LocalMusicFragment.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
public void onRefresh() {
    RecyclerView mRecyclerView = recyclerView.getRecyclerView();
    int firstVisibleItemPosition = ((LinearLayoutManager)
            mRecyclerView.getLayoutManager()).findFirstVisibleItemPosition();
    if (firstVisibleItemPosition == 0) {
        onLazyLoad();
        return;
    }
    mRecyclerView.scrollToPosition(5);
    mRecyclerView.smoothScrollToPosition(0);
}
 
Example 9
Source File: ScrollUtils.java    From SlyceMessaging with MIT License 5 votes vote down vote up
public static void scrollToPosition(int position, RecyclerView mRecyclerView, RecyclerView.Adapter mRecyclerAdapter) {
    int offsetOfScroll = getOffsetOfGoal(position, mRecyclerView, mRecyclerAdapter);
    if (offsetOfScroll < SMOOTH_SCROLL_IF_THIS_MUCH) {
        mRecyclerView.smoothScrollToPosition(position);
    } else {
        mRecyclerView.scrollToPosition(position);
    }
}
 
Example 10
Source File: LoadMoreRecyclerListener.java    From RecyclerViewManager with MIT License 5 votes vote down vote up
/**
 * 添加LoadMore布局
 */
private void addFooterLoadinLayout(RecyclerView recyclerView) {
    isLoading = true;
    if (null == mFooterLoadingLayout) {
        mFooterLoadingLayout = new RotateLoadingLayout(mContext, RecyclerMode.BOTTOM);
    }
    mAdapter.addFooterView(mFooterLoadingLayout);
    mOldItemCount = mAdapter.getItemCount();
    recyclerView.smoothScrollToPosition(mOldItemCount - 1);
    mFooterLoadingLayout.onRefresh();
    mFooterLoadingLayout.setVisibility(View.VISIBLE);
}
 
Example 11
Source File: RecyclerViewBA.java    From chaoli-forum-for-android-2 with GNU General Public License v3.0 4 votes vote down vote up
@BindingAdapter("app:position")
public static void setPosition(RecyclerView recyclerView, int position) {
    recyclerView.smoothScrollToPosition(position);
}
 
Example 12
Source File: DefaultChildSelectionListener.java    From CarouselLayoutManager with Apache License 2.0 4 votes vote down vote up
@Override
protected void onBackItemClicked(@NonNull final RecyclerView recyclerView, @NonNull final CarouselLayoutManager carouselLayoutManager, @NonNull final View v) {
    recyclerView.smoothScrollToPosition(carouselLayoutManager.getPosition(v));
}
 
Example 13
Source File: FastScrollToTop.java    From RecyclerViewTools with Apache License 2.0 4 votes vote down vote up
@Override public void run() {
   RecyclerView recyclerView = weakRecycler.get();
   if (recyclerView == null) return;
   recyclerView.scrollToPosition(lastScroll);
   recyclerView.smoothScrollToPosition(0);
}
 
Example 14
Source File: RecyclerViewTools.java    From RecyclerViewTools with Apache License 2.0 2 votes vote down vote up
/**
 * Currently this simply calls recyclerView.smoothScrollToPosition(position);
 * But the API is already available and in the near future we'll try to code the `offset`
 *
 * @param recyclerView
 * @param position
 * @param offset
 */
public static void smoothScrollToPositionWithOffset(RecyclerView recyclerView, int position, int offset) {
   recyclerView.smoothScrollToPosition(position);
}