我正在使用导航菜单 View ,并以编程方式滚动它,然后获取其项目的 Y 位置。 但是,获取项目位置的代码在列表(使用布局管理器)开始滚动之后以及尚未完成滚动和更改位置时立即运行。 在“scrollToPositionWithOffset”等待滚动操作结束后,如何执行其余代码?
这是我的代码:
RecyclerView recyclerView = (RecyclerView) navigationView.getChildAt(0);
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
layoutManager.scrollToPositionWithOffset(4, 0);
//it should wait the line above run completely to can run too
mImage.animate().y(navView.getChildAt(4).getY());
最佳答案
您应该使用滚动监听器来监听滚动状态更改为空闲的情况。请参见下文。
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
mImage.animate().y(navView.getChildAt(4).getY());
recyclerView.removeOnScrollListener(this);
}
}
}
layoutManager.scrollToPositionWithOffset(4, 0);
关于java - 仅在滚动功能完成后获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42771648/