我正在尝试在滚动时滚动到第一个完全可见的项目,我正在执行以下操作:
recyclerView.addOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager layoutManager = ((LinearLayoutManager) recyclerView
.getLayoutManager());
int position = layoutManager.findFirstCompletelyVisibleItemPosition();
if(position>0){
recyclerView.smoothScrollToPosition(position);
Log.e(TAG, "onScrolled: just called smooth scroll >> "+position );
}
}
});
我获得了第一个完全可见项目的正确位置,但滚动到它[尝试将其滚动到屏幕可见部分的初始位置]不起作用。我尝试通过以下方式实现它:
1
recyclerView.smoothScrollToPosition(position);
2
layoutManager.smoothScrollToPosition(recyclerView, null, position);
他们没有改变!!我用谷歌搜索解决方案,但我找不到合适的解决方案。请建议我如何实现这一目标?
答案 0 :(得分:0)
使用 SnapHelper
SnapHelper snapHelper = new GravitySnapHelper(Gravity.START);
snapHelper.attachToRecyclerView(startRecyclerView);
有关更多详细信息,请阅读本文: https://rubensousa.github.io/2016/08/recyclerviewsnap