Java 如何使用多种类型的customViewHolder在recyclerView中实现recyclerView.smoothScrollToPosition(位置)?
我的recycleView有3种类型的视图支架 位置0是viewHolder1 位置1-41为视图保持架2 位置42是视图保持器3 但如果myDataSource.size()的值超过42,它将创建viewHolder3 所以我需要让recyclerView.smoothScrollToPosition(位置)工作 请帮忙 这是我的LinearLayoutManagerWithSmoothScrollerJava 如何使用多种类型的customViewHolder在recyclerView中实现recyclerView.smoothScrollToPosition(位置)?,java,android,android-recyclerview,Java,Android,Android Recyclerview,我的recycleView有3种类型的视图支架 位置0是viewHolder1 位置1-41为视图保持架2 位置42是视图保持器3 但如果myDataSource.size()的值超过42,它将创建viewHolder3 所以我需要让recyclerView.smoothScrollToPosition(位置)工作 请帮忙 这是我的LinearLayoutManagerWithSmoothScroller public class LinearLayoutManagerWithSmoothScr
public class LinearLayoutManagerWithSmoothScroller extends LinearLayoutManager {
public LinearLayoutManagerWithSmoothScroller(Context context) {
super(context);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
View firstVisibleChild = recyclerView.getChildAt(0);
final int childHeight = firstVisibleChild.getHeight();
int distanceInPixels = ((findFirstVisibleItemPosition() - position) * childHeight);
if (distanceInPixels == 0) {
distanceInPixels = (int) Math.abs(firstVisibleChild.getY());
}
SmoothScroller smoothScroller = new SmoothScroller(recyclerView.getContext(), Math.abs(distanceInPixels), 1000);
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private class SmoothScroller extends LinearSmoothScroller {
private static final int TARGET_SEEK_SCROLL_DISTANCE_PX = 10000;
private final float distanceInPixels;
private final float duration;
public SmoothScroller(Context context, int distanceInPixels, int duration) {
super(context);
this.distanceInPixels = distanceInPixels;
float millisecondsPerPx = calculateSpeedPerPixel(context.getResources().getDisplayMetrics());
this.duration = distanceInPixels < TARGET_SEEK_SCROLL_DISTANCE_PX ?
(int) (Math.abs(distanceInPixels) * millisecondsPerPx) : duration;
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return LinearLayoutManagerWithSmoothScroller.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected int calculateTimeForScrolling(int dx) {
float proportion = (float) dx / distanceInPixels;
return (int) (duration * proportion);
}
}
使用带有首选项的平滑滚动条
SNAP\u TO\u START
:
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(context) {
@Override protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
现在设置要滚动到的位置:
smoothScroller.setTargetPosition(position);
并将该平滑滚动条
传递给布局管理器
:
layoutManager.startSmoothScroll(smoothScroller);
更新
从onResponse
中删除这两行,并将它们添加到onCreate
recycleview.setAdapter(mAdapter);
recycleview.setLayoutManager(new LinearLayoutManagerWithSmoothScroller(QuizGameRankingActivity.this));
反复设置适配器不是一种好的做法为什么要使用自定义平滑滚动条,,而您已经可以使用默认的滚动条了。并且避免在回调后初始化适配器。使用空白arraylist在创建时对其进行初始化,然后将数据更新到适配器中,这将是一个良好的做法,否则适配器将在每个和每个api回调中进行初始化 此外,您还需要进行后期运行,以便在后期帧中平滑滚动,如
recyclerview.post(new Runnable() {
@Override
public void run() {
recycleview.smoothScrollToPosition(20);
}
});
您尚未提供所面临的任何错误。如果您想解决问题,请在问题中添加有关错误的信息。否则,语法已经很好了。我已经更新了我的帖子。你能给我看一下例子吗?你读过我下面的答案了吗?我已经试过了,但不起作用。我想是因为我有很多类型的customViewHolder
recycleview.setAdapter(mAdapter);
recycleview.setLayoutManager(new LinearLayoutManagerWithSmoothScroller(QuizGameRankingActivity.this));
recyclerview.post(new Runnable() {
@Override
public void run() {
recycleview.smoothScrollToPosition(20);
}
});