【问题标题】:Not be able to fully scroll inside a NestedScrollView无法在 NestedScrollView 内完全滚动
【发布时间】:2019-05-05 00:54:15
【问题描述】:

不知何故,我无法在 NestedScrollView 内完全滚动。在我的 NestedScrollView 里面是一个 TextView,里面有很多文本和一个按钮。当我向下滚动时,我只能看到按钮的顶部(只有几个像素)。滚动不够远。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:stateListAnimator="@drawable/appbar_always_elevated"
        android:layout_height="128dp">

    <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="@string/app_name"
                app:layout_collapseMode="pin">

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>


<android.support.v4.widget.NestedScrollView
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TextView
                android:id="@+id/textView"
                style="@style/Text.Body1.Medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin"
                android:layout_marginTop="@dimen/margin"
                android:layout_marginEnd="@dimen/margin"
                android:text="@string/large_text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/appbar"/>

        <Button
                android:id="@+id/continueButton"
                style="@style/Widget.Button.Green"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin"
                android:layout_marginStart="@dimen/margin"
                android:layout_marginEnd="@dimen/margin"
                android:layout_marginBottom="@dimen/margin"
                android:text="@string/continue_text"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"/>
    </LinearLayout>
   </android.support.v4.widget.NestedScrollView>

  </android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 我已经删除了AppBar 中的android:stateListAnimator="@drawable/appbar_always_elevated" 以及TextViewButton 中的约束属性。没有问题成立。它工作正常。我认为您可能会错过一些导致问题的信息。
  • @Peter,你能在 github 上发布一个简单的项目来重现这个问题吗?
  • 这个问题是否已为您解答或您仍在寻找解决方案?
  • 使用相对布局并检查您的布局使用全高

标签: android android-layout layout


【解决方案1】:

NestedScrollView 中的LinearLayout 的高度更改为wrap_content

NestedScrollView 及其单个滚动子项背后的想法是 outside(滚动视图)应该有一些固定/预定的高度,而 inside(滚动子)应具有大于父级的动态高度。毕竟,如果内容不大于父级,那么滚动是什么?

【讨论】:

  • 虽然您的推理听起来是正确的,但它并不能解决我的问题。我在布局检查器中看到的是 NestedScrollView 太大了。它与 CoordinatorLayout 具有相同的高度,这当然是不正确的,因为 AppBarLayout(带有工具栏)也在其中。
  • 所以我猜我缺少的底部高度与折叠工具栏的高度相同。
【解决方案2】:

您需要在 CoordinatorLayoutAppBarLayoutCollapsingToolbarLayout 中添加 android:fitsSystemWindows="true"

阅读更多关于fitsSystemWindows

Why would I want to fitsSystemWindows?

试试这个

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="128dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:title="@string/app_name">

            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/large_text" />

            <Button
                android:id="@+id/continueButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="continue_text" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

【讨论】:

    【解决方案3】:

    这是我的工作 .xml 代码。

    请申请。

    strings.xml

    <string name="appbar_scrolling_view_behavior" translatable="false">android.support.design.widget.AppBarLayout$ScrollingViewBehavior</string>
    

    ma​​in_activity.xml 文件

    <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/adView">
    
            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsingToolbarLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:expandedTitleMarginEnd="64dp"
                    app:expandedTitleMarginStart="48dp"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">
                    <!--app:contentScrim="?attr/colorPrimary"-->
    
    
                    <RelativeLayout
                        android:id="@+id/lay"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/inhouse_ad_card_height"
                        android:background="@drawable/app_gradient_square"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:visibility="visible"
                        app:layout_collapseMode="parallax"
                        app:layout_scrollFlags="scroll|enterAlways">
    
                    <!--write tag here-->
    
                    </RelativeLayout>
    
                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="@drawable/bg_transition"
                        android:visibility="visible"
                        app:contentInsetEnd="@dimen/dim_10"
                        app:contentInsetStart="@dimen/dim_10"
                        app:layout_collapseMode="pin"
                        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
    
                        <RelativeLayout
                            android:id="@+id/layBtns"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center">
    
                            <ImageView
                                android:id="@+id/btnSetting"
                                android:layout_width="@dimen/btn_height"
                                android:layout_height="@dimen/btn_height"
                                android:layout_alignParentLeft="true"
                                android:layout_alignParentStart="true"
                                android:layout_centerVertical="true"
                                android:gravity="center"
                                ads:srcCompat="@drawable/ic_editor_settings" />
    
                            <TextView
                                android:id="@+id/txtAppTitle"
                                style="@style/TitleFont"
                                android:layout_width="wrap_content"
                                android:layout_height="@dimen/img_btn_height"
                                android:layout_centerInParent="true"
                                android:gravity="center"
                                android:text="@string/display_name"
                                android:textColor="@color/white"
                                android:textSize="@dimen/font_size_extra_large"
                                android:visibility="gone" />
    
                        </RelativeLayout>
    
                    </android.support.v7.widget.Toolbar>
    
                </android.support.design.widget.CollapsingToolbarLayout>
    
            </android.support.design.widget.AppBarLayout>
    
            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="none"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusableInTouchMode="true"
                    android:orientation="vertical">
    
                        <!--write tag here-->
    
                </LinearLayout>
    
            </android.support.v4.widget.NestedScrollView>
    
        </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      【解决方案4】:

      只需将您的 NestedScrollView 更改为:

      <android.support.v4.widget.NestedScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              android:fillViewport="true">
      

      【讨论】:

        【解决方案5】:

        试试这个

           private void updateListViewHeight(ListView myListView) {
        
            ListAdapter myListAdapter = myListView.getAdapter();
        
            if (myListAdapter == null) {
                return;
            }
            //get ListView height
            int totalHeight = 0;
            int adapterCount = myListAdapter.getCount();
            for (int size = 0; size < adapterCount; size++) {
                View listItem = myListAdapter.getView(size, null, myListView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            //Change Height of ListView
            ViewGroup.LayoutParams params = myListView.getLayoutParams();
            params.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
            myListView.setLayoutParams(params);
        
        }
        

        在上述方法中传递您的列表视图。

        【讨论】:

          【解决方案6】:

          供大家参考,因为我也遇到了同样的问题。这是一个已知问题,已在 com.google.android.material:material:1.3.0-alpha04 中修复

          https://github.com/material-components/material-components-android/commit/a21a30026a33fc20cf7ad699d32d1298b84096c6

          【讨论】:

            猜你喜欢
            • 2018-02-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多