gpt4 book ai didi

android - RecyclerView smoothScrollToPosition 小距离太快

转载 作者:行者123 更新时间:2023-12-04 11:58:09 58 4
gpt4 key购买 nike

在我们的聊天应用程序中,我们使用 RecyclerView 可以包含不同高度的消息。我想用smoothScroll为消息添加动画。我的问题是:当我使用 recyclerView.smoothScrollToPosition(position)在高度较小的消息上,它滚动得太快。

我也试过这个solution changing smoothScoll speed ,它适用于小消息,但是当消息很大时,它的滚动速度会使消息显得太慢。

我的完美速度是通过 recyclerView.smoothScrollBy(x, y) 实现的,但在这里我在插入消息高度时遇到问题,因为消息可以有任何高度。

最佳答案

覆盖 calculateTimeForScrolling LinearSmoothScroller的方法计算所有距离的时间:

public class SpeedyLinearLayoutManager extends LinearLayoutManager {

private int smoothScrollDuration;

public SpeedyLinearLayoutManager(Context context, int smoothScrollDuration) {
super(context);
this.smoothScrollDuration = smoothScrollDuration;
}

public SpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout, int smoothScrollDuration) {
super(context, orientation, reverseLayout);
this.smoothScrollDuration = smoothScrollDuration;
}

public SpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, int smoothScrollDuration) {
super(context, attrs, defStyleAttr, defStyleRes);
this.smoothScrollDuration = smoothScrollDuration;
}

@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {

final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

@Override
protected int calculateTimeForScrolling(int dx) {
return smoothScrollDuration;
}
};

linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}

public int getSmoothScrollDuration() {
return smoothScrollDuration;
}

public void setSmoothScrollDuration(int smoothScrollDuration) {
this.smoothScrollDuration = smoothScrollDuration;
}
}
并将其用于recyclerview:
    LinearLayoutManager layoutManager = new SpeedyLinearLayoutManager(context, 100);
mRecyclerView.setLayoutManager(layoutManager);

关于android - RecyclerView smoothScrollToPosition 小距离太快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157038/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com