Raf*_*ael 6 android android-recyclerview
在我们的聊天应用程序中,我们使用 RecyclerView 可以有不同高度的消息。我想用 smoothScroll 动画消息添加。我的问题是:当我使用recyclerView.smoothScrollToPosition(position)小高度的消息时,它滚动太快。
我也试过这个解决方案改变 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;
}
}
Run Code Online (Sandbox Code Playgroud)
并将其用于回收视图:
LinearLayoutManager layoutManager = new SpeedyLinearLayoutManager(context, 100);
mRecyclerView.setLayoutManager(layoutManager);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1722 次 |
| 最近记录: |