Java Android smoothScrollToPosition不';我不能正常工作
我正在使用此功能滚动到水平回收视图中的选定位置。问题是,如果我滚动回收器,然后单击一个项目,它不会进入视图的中心。相反,如果我不滚动回收器,它会工作。 我在单击某个项目时使用此方法。该位置是适配器位置Java Android smoothScrollToPosition不';我不能正常工作,java,android,kotlin,Java,Android,Kotlin,我正在使用此功能滚动到水平回收视图中的选定位置。问题是,如果我滚动回收器,然后单击一个项目,它不会进入视图的中心。相反,如果我不滚动回收器,它会工作。 我在单击某个项目时使用此方法。该位置是适配器位置 override fun scrollToSelected(position: Int, isLeftScroll: Boolean) { if(!isLeftScroll || position == 0) contactRV.layoutManager?.smoothScrollToPosit
override fun scrollToSelected(position: Int, isLeftScroll: Boolean) {
if(!isLeftScroll || position == 0) contactRV.layoutManager?.smoothScrollToPosition(contactRV, RecyclerView.State(), position +1)
else contactRV.layoutManager?.smoothScrollToPosition(contactRV, RecyclerView.State(), position-1)
我该怎么解决这个问题 您将通过实现
RecyclerView.SmoothScroller
的方法onTargetFound(视图、状态、操作)
来实现
特别是在带有linearslayoutmanager
线性平滑滚动条的linearslayoutmanager中:
public class CenterLayoutManager extends LinearLayoutManager {
public CenterLayoutManager(Context context) {
super(context);
}
public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CenterLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private static class CenterSmoothScroller extends LinearSmoothScroller {
CenterSmoothScroller(Context context) {
super(context);
}
@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
}
}
您将通过实现RecyclerView.SmoothScroller
的方法onTargetFound(视图、状态、操作)
来实现
特别是在带有linearslayoutmanager
线性平滑滚动条的linearslayoutmanager中:
public class CenterLayoutManager extends LinearLayoutManager {
public CenterLayoutManager(Context context) {
super(context);
}
public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CenterLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private static class CenterSmoothScroller extends LinearSmoothScroller {
CenterSmoothScroller(Context context) {
super(context);
}
@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
}
}
}
调用smoothScrollToPosition方法时,如何获取recyclerview的状态?只需调用recyclerview.smoothScrollToPosition(position);不要忘记设置布局管理器recyclerView.setLayoutManager(layoutManager);调用smoothScrollToPosition方法时,如何获取recyclerview的状态?只需调用recyclerview.smoothScrollToPosition(position);不要忘记设置布局管理器recyclerView.setLayoutManager(layoutManager);