首页 > 解决方案 > 视图可见后滚动

问题描述

在单击按钮后视图变得可见后,我试图滚动到我的滚动视图的底部。问题是在视图实际可见之前应用了 scrollTo 函数。我知道这一点,因为当按钮被按下两次时,它会在第二次点击时滚动到底部。那么,有没有办法在视图变得可见后滚动?

button.setOnClickListener(v -> {
    constraintLayout.setVisibility(View.VISIBLE);
    scrollView.smoothScrollTo(0, constraintLayout.getBottom());
}

标签: android-studiovisibilityandroid-scrollviewvisible

解决方案


button.setOnClickListener(v -> {
    constraintLayout.setVisibility(View.VISIBLE);
    Handler handler = new Handler();
    handler.postDelayed(() -> {
        scrollView.smoothScrollTo(0, constraintLayout.getBottom());
    }, 100);
}

我刚刚发现这是可行的,但我希望不要使用延迟。


推荐阅读