RecyclerView does not scroll as expected

99封情书 提交于 2019-12-02 17:31:56

I found a surprising simple workaround:

@Override public void onClick(View v) {     int pos = mLayoutManager.findFirstVisibleItemPosition();     int outer = (MyAdapter.VISIBLE_ITEMS + 1) / 2;     int delta = pos + outer - ForecastAdapter.ITEM_IN_CENTER;     //Log.d("Scroll", "delta=" + delta);     View firstChild = mForecast.getChildAt(0);     if(firstChild != null) {         mForecast.smoothScrollBy(firstChild.getWidth() * -delta, 0);     } } 

Here I calculate the width to jump myself, that does exactly what I want.

forcelain

In case of LinearLayoutManager with vertical orientation you can create own SmoothScroller and override calculateDyToMakeVisible() method where you can set desired view position. For example, to make the target view always to be appeared at the top side of RecyclerView after smoothScroll() write this:

class CustomLinearSmoothScroller extends LinearSmoothScroller {      public CustomLinearSmoothScroller(Context context) {         super(context);     }      @Override     public int calculateDyToMakeVisible(View view, int snapPreference) {         final RecyclerView.LayoutManager layoutManager = getLayoutManager();         if (!layoutManager.canScrollVertically()) {             return 0;         }         final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)view.getLayoutParams();         final int top = layoutManager.getDecoratedTop(view) - params.topMargin;         final int bottom = layoutManager.getDecoratedBottom(view) + params.bottomMargin;         final int viewHeight = bottom - top;         final int start = layoutManager.getPaddingTop();         final int end = start + viewHeight;         return calculateDtToFit(top, bottom, start, end, snapPreference);     } 

"top" and "bottom" - bounds of the target view

"start" and "end" - points between which the view should be placed during smoothScroll

To support smooth scrolling, you must override smoothScrollToPosition(RecyclerView, State, int) and create a RecyclerView.SmoothScroller.

RecyclerView.LayoutManager is responsible for creating the actual scroll action. If you want to provide a custom smooth scroll logic, override smoothScrollToPosition(RecyclerView, State, int) in your LayoutManager.

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#smoothScrollToPosition(int)

In your case, use smoothScrollBy could be a workaround (doesn't need this override).

This worked for me:

itemsView.smoothScrollBy(-recyclerView.computeHorizontalScrollOffset(), 0) 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!