我正在寻找一种方法来滚动RecyclerView,以显示选中的项目在顶部。

在一个ListView中,我能够通过使用scrollTo(x,y)来做到这一点,并获得需要居中的元素的顶部。

喜欢的东西:

@Override
public void onItemClick(View v, int pos){
    mylistView.scrollTo(0, v.getTop());
}

问题是RecyclerView在使用它的scrollTo方法时返回一个错误说

RecyclerView不支持滚动到绝对位置

我如何滚动一个RecyclerView把选定的项目放在视图的顶部?


当前回答

简介

没有一个答案解释如何在顶部显示最后一项。因此,答案只适用于上面或下面仍然有足够的项来填充剩余的RecyclerView的项。例如,如果有59个元素,第56个元素被选中,它应该在顶部,如下图所示:

那么,让我们看看如何在下一段中实现它。

解决方案

我们可以使用linearLayoutManager来处理这些情况。scrollToPositionWithOffset(pos, 0)和额外的逻辑在适配器的RecyclerView -通过添加一个自定义边距下面的最后一项(如果最后一项是不可见的,那么这意味着有足够的空间填充RecyclerView)。自定义边距可以是根视图高度和项目高度之间的差值。因此,您的adaptor for RecyclerView将如下所示:

...
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    ...

    int bottomHeight = 0;
    int itemHeight = holder.itemView.getMeasuredHeight();
    // if it's the last item then add a bottom margin that is enough to bring it to the top
    if (position == mDataSet.length - 1) {
        bottomHeight = Math.max(0, mRootView.getMeasuredHeight() - itemHeight);
    }
    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)holder.itemView.getLayoutParams();
    params.setMargins(0, 0, params.rightMargin, bottomHeight);
    holder.itemView.setLayoutParams(params);

    ...
} 
...

其他回答

如果你正在使用LinearLayoutManager或交错的GridLayoutManager,它们都有一个scrollToPositionWithOffset方法,该方法既取位置,也取项目从RecyclerView开始的偏移量,这似乎可以完成你所需要的(将偏移量设置为0应该与顶部对齐)。

例如:

//Scroll item 2 to 20 pixels from the top
linearLayoutManager.scrollToPositionWithOffset(2, 20);

简介

没有一个答案解释如何在顶部显示最后一项。因此,答案只适用于上面或下面仍然有足够的项来填充剩余的RecyclerView的项。例如,如果有59个元素,第56个元素被选中,它应该在顶部,如下图所示:

那么,让我们看看如何在下一段中实现它。

解决方案

我们可以使用linearLayoutManager来处理这些情况。scrollToPositionWithOffset(pos, 0)和额外的逻辑在适配器的RecyclerView -通过添加一个自定义边距下面的最后一项(如果最后一项是不可见的,那么这意味着有足够的空间填充RecyclerView)。自定义边距可以是根视图高度和项目高度之间的差值。因此,您的adaptor for RecyclerView将如下所示:

...
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    ...

    int bottomHeight = 0;
    int itemHeight = holder.itemView.getMeasuredHeight();
    // if it's the last item then add a bottom margin that is enough to bring it to the top
    if (position == mDataSet.length - 1) {
        bottomHeight = Math.max(0, mRootView.getMeasuredHeight() - itemHeight);
    }
    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)holder.itemView.getLayoutParams();
    params.setMargins(0, 0, params.rightMargin, bottomHeight);
    holder.itemView.setLayoutParams(params);

    ...
} 
...

我所做的恢复滚动位置后刷新RecyclerView按钮点击:

if (linearLayoutManager != null) {

    index = linearLayoutManager.findFirstVisibleItemPosition();
    View v = linearLayoutManager.getChildAt(0);
    top = (v == null) ? 0 : (v.getTop() - linearLayoutManager.getPaddingTop());
    Log.d("TAG", "visible position " + " " + index);
}

else{
    index = 0;
}

linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.scrollToPositionWithOffset(index, top);

在创建linearLayoutManager对象之前,从顶部获得第一个可见项的偏移量,并在实例化它之后,调用linearLayoutManager对象的scrollToPositionWithOffset。

请注意,如果scrolltopposition不工作,请注意您的RecyclerView是在一个NestedScrollView;参考这篇文章

调速器也一样

public class SmoothScrollLinearLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 110f;
private Context mContext;

public SmoothScrollLinearLayoutManager(Context context,int orientation, boolean reverseLayout) {
    super(context,orientation,reverseLayout);
    mContext = context;
}

@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
                                   int position) {
    RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext()){
        //This controls the direction in which smoothScroll looks for your view
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return new PointF(0, 1);
        }

        //This returns the milliseconds it takes to scroll one pixel.
        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}


private class TopSnappedSmoothScroller extends LinearSmoothScroller {
    public TopSnappedSmoothScroller(Context context) {
        super(context);

    }

    @Override
    public PointF computeScrollVectorForPosition(int targetPosition) {
        return SmoothScrollLinearLayoutManager.this
                .computeScrollVectorForPosition(targetPosition);
    }

    @Override
    protected int getVerticalSnapPreference() {
        return SNAP_TO_START;
    }
}
}