Recyclerview smoothScrollToPosition()無效問題解決方法如下
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext) {
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
LinearSmoothScroller smoothScroller =
new LinearSmoothScroller(recyclerView.getContext()) {
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
// 返回:滑過1px時(shí)經(jīng)歷的時(shí)間(ms)蛉腌。
return 150f / displayMetrics.densityDpi;
}
@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return boxStart - viewStart;
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
};