英文:
scrollToPosition() in onCreate() doesn't work
问题
在方法 changeWeek()
中,我正在尝试滚动我 RecyclerView 中的元素。当这个方法在 onOptionsItemSelected(@NonNull MenuItem item)
中的菜单项点击后调用时。但是当这个方法在 onCreate() 中调用时,它不起作用
有没有什么方法可以修复这个问题?
private void changeWeek(int week, boolean fromStart) {
showSchedule(week);
if (week == 1) {
Objects.requireNonNull(getSupportActionBar()).setTitle(getResources().getString(R.string.first_week) + " " + groupName.split(" ")[0].toUpperCase());
} else {
Objects.requireNonNull(getSupportActionBar()).setTitle(getResources().getString(R.string.second_week) + " " + groupName.split(" ")[0].toUpperCase());
}
recyclerView.getLayoutManager().scrollToPosition(remainder);
}
英文:
In method changeWeek()
I'm trying to scroll elements in my recyclerView. When this method calls after click on menu item in onOptionsItemSelected(@NonNull MenuItem item)
. But when this method called in onCreate() it doesn't work
Is there any way to fix it?
private void changeWeek(int week, boolean fromStart) {
showSchedule(week);
if (week == 1) {
Objects.requireNonNull(getSupportActionBar()).setTitle(getResources().getString(R.string.first_week) + " " + groupName.split(" ")[0].toUpperCase());
} else {
Objects.requireNonNull(getSupportActionBar()).setTitle(getResources().getString(R.string.second_week) + " " + groupName.split(" ")[0].toUpperCase());
}
recyclerView.getLayoutManager().scrollToPosition(remainder);
}
答案1
得分: 0
Sure, here's the translation:
你可以在存在片段时在 OnCreatedView
上调用这个方法吗?也许你需要先创建视图。
试试看。
英文:
Could you call this method on OnCreatedView
if there is a fragment? Maybe you need the view to be created first.
Try it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论