我编写了一个处理程序来维护回收视图的自动滚动。它工作正常。但是当回收站视图中只有一个项目时我面临的问题。我的应用程序崩溃了,当我检查 logcat 时,我收到类似 java.lang.IllegalArgumentException: Invalid target position 的错误。
这是我的自定义 LinearLayoutManager 类
public class CustomLinearLayoutManager extends LinearLayoutManager {
public CustomLinearLayoutManager (Context context) {
super(context);
}
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
final LinearSmoothScroller linearSmoothScroller =
new LinearSmoothScroller(recyclerView.getContext()) {
private static final float MILLISECONDS_PER_INCH = 100f;
@Override
public PointF computeScrollVectorForPosition(int targetPosition) …Run Code Online (Sandbox Code Playgroud)