Android:在 NestedScrollView 中滚动以将 View 与可见屏幕顶部对齐

标签 android android-nestedscrollview

我的布局中有这样的可扩展 View 。 enter image description here

假设我点击教育,我需要我的 NestedScrollView 移动,这样教育 CardView 应该与布局/屏幕的可见顶部对齐。

我尝试了 scrollView.smoothScrollTo(view.getTop()); 就好像这个函数甚至没有被调用。

我调用 smoothScrollTo() 的代码

@OnClick(R.id.ip_expand_experience)
void expandExperiences(){
    if(!experiencesExpanded){
        AnimationUtils.expand(experiencesCard,context,CARD_HEIGHT_COLLAPSED);
        experiencesExpanded=true;
        focusOnView(experiencesCard);
    }else{
        AnimationUtils.collapse(experiencesCard,context,CARD_HEIGHT_COLLAPSED);
        experiencesExpanded=false;
    }

}

private void focusOnView(final View view){
    scrollView.post(new Runnable() {
        @Override
        public void run() {
            scrollView.smoothScrollTo(0, view.getTop());
        }
    });
}

最佳答案

试试这个

 your_scrollview.post(new Runnable() {
        @Override
        public void run() {
            your_scrollview.scrollTo(0, yourview.getBottom());
        }
    });

关于Android:在 NestedScrollView 中滚动以将 View 与可见屏幕顶部对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44942620/

相关文章:

java - 每个日期单元格样式都可配置的 Android 自定义日历 View ?

android - 具有多个捕捉点的 CoordinatorLayout

android - 从 NestedScrollView 中删除 OnScrollChangeListener

Android EditText.setError 无法在无法聚焦的情况下工作

android - Android 弹出 View 中不显示键盘

android - NestedScrollView 内的 ViewPager 内的 ScrollView 不滚动

android - 如何始终在 Bottom Sheet 对话框 fragment 中将按钮与屏幕底部对齐

android - 对 Android API 和兼容性感到困惑

android - 如何在 Android 应用程序的 Facebook 开发者控制台添加多个类名