简体   繁体   English

如何让 ScrollView 的顶部在后按时可见

[英]How to have top of ScrollView be visible on back press

I'm creating an Android app using MVVM and view binding.我正在使用 MVVM 和视图绑定创建一个 Android 应用程序。 My home fragment has a ScrollView with three horizontal RecyclerViews inside.我的 home 片段有一个 ScrollView,里面有三个水平的 RecyclerViews。

When the user navigates to another view by clicking on an item & then returns to the home fragment on back press, I want the ScrollView to be at the top instead of returning to the part of the screen that was clicked.当用户通过单击一个项目导航到另一个视图然后在后按时返回主片段时,我希望 ScrollView 位于顶部而不是返回到被单击的屏幕部​​分。

I've tried this in my home fragment, but it didn't so anything:我已经在我的家庭片段中尝试过这个,但它没有任何效果:

binding.container.requestFocus(View.FOCUS_UP);
binding.container.scrollTo(0,0);

Any other suggestions would be appreciated.任何其他建议将不胜感激。

This solved my problem:这解决了我的问题:

binding.container.post(new Runnable() {
    @Override
    public void run() {
        binding.container.fullScroll(View.FOCUS_UP);
    }
});

Thank you @narendra-nath谢谢@narendra-nath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM