scrollToPosition() 在 onCreate() 中不起作用

huangapple go评论81阅读模式
英文:

scrollToPosition() in onCreate() doesn't work

问题

在方法 changeWeek() 中,我正在尝试滚动我 RecyclerView 中的元素。当这个方法在 onOptionsItemSelected(@NonNull MenuItem item) 中的菜单项点击后调用时。但是当这个方法在 onCreate() 中调用时,它不起作用 scrollToPosition() 在 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 scrollToPosition() 在 onCreate() 中不起作用
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.

huangapple
  • 本文由 发表于 2020年10月9日 19:35:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64279263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定