【问题标题】:ScrollView to show layout if expandable layout goes outside the screen如果可扩展布局超出屏幕,则 ScrollView 显示布局
【发布时间】:2020-12-22 15:35:36
【问题描述】:

我的 Android 应用中有几个可扩展的布局。当我单击展开布局时,布局会在屏幕外消失,我必须手动向下滚动以使其可见。如何让 ScrollView 自动向下滚动以使点击的布局可见?

我尝试使用scrollView.scrollTo(0, textID.getBottom()); 滚动到布局元素的底部,但没有成功。

Java:

expandableLayout1 = root.findViewById(R.id.expandable_layout1);
        button = (LinearLayout)root.findViewById(R.id.box_header);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                if (expandableLayout1.isExpanded()) {
                    expandableLayout1.collapse();
                } else {
                    expandableLayout1.expand();
                    scrollView.scrollTo(0, textID.getBottom());
                }
            }
        });

xml:

        <LinearLayout
            android:id="@+id/box"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/box_header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="title"
                    android:textColor="#ffffff"
                    android:textSize="16sp"/>

            </LinearLayout>

            <net.cachapa.expandablelayout.ExpandableLayout
                android:id="@+id/expandable_layout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:el_duration="1000"
                app:el_expanded="false"
                app:el_parallax="0.5">

                
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="some text"
                    android:textColor="#ffffff"
                    android:textSize="12sp"/>
                
            </net.cachapa.expandablelayout.ExpandableLayout>

        </LinearLayout>

【问题讨论】:

    标签: java android android-studio android-layout expandable


    【解决方案1】:

    您面临的问题是 ExpandableLayout 在 ScrollView 滚动到底部之前没有时间完成打开。所以最终发生的是 ScrollView 滚动到尚未完全打开的 ExpandableLayout 的底部。您需要做的是在 Layout 打开和 ScrollView 启动它的滚动功能之间添加延迟。这是您需要的代码,我会尝试调整毫秒,您可能会将它们降低到 1000 以下,但不会降低太多,但您仍然需要对其进行一些故障排除以使其更快和更顺畅一点。也可以尝试使用 smoothScrollTo 而不是 scrollTo。

    expandableLayout1 = root.findViewById(R.id.expandable_layout1);
        button = (LinearLayout)root.findViewById(R.id.box_header);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                if (expandableLayout1.isExpanded()) {
                    expandableLayout1.collapse();
                } else {
                    expandableLayout1.expand();
    
                    new Handler().postDelayed(new Runnable() {
    
                        @Override
                        public void run() {
    
                             scrollView.post(new Runnable() {
                                 @Override
                                 public void run() {
    
                                     scrollView.scrollTo(0, textID.getBottom());
    
                                 }
                             });
    
                        }
                     }, 1000 ); // time in milliseconds
                    
                }
            }
        });
    

    这是我以原始形式添加的代码,可能会帮助您更容易理解它。

    new Handler().postDelayed(new Runnable() {
     @Override
     public void run() {
    
     // do something
    
     }
    }, 1000 );  // time in milliseconds
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多