【问题标题】:Android Nested Scrollview not scrollingAndroid嵌套滚动视图不滚动
【发布时间】:2016-05-20 20:49:25
【问题描述】:

我有一个嵌套的 ScrollView,其中包含线性布局内的内容。

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:CoverFlowPager="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:clipToPadding="false"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

    </android.support.v4.widget.NestedScrollView>

</FrameLayout>

我在 ViewPager 中有这个布局,而 ViewPager 在 CordinatorLayout 中。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

现在,当我滚动视图时,视图不会滚动。但是由于布局放置在协调器布局内,因此它向上移动直到工具栏被隐藏。但它没有向上滚动。

这是我的主要活动 xml,视图寻呼机位于选项卡式布局内。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    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="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

           <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:attrs="http://schemas.android.com/apk/res-auto"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay">

            <FrameLayout
                android:id="@+id/titleContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"    
                android:gravity="center">

                <com.CustomFontTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Toolbar Title"
                    android:textColor="#ffffff"
                    attrs:customFont="handyman_bold"
                    android:textSize="8pt"
                    android:layout_marginLeft="-20dp"
                    android:id="@+id/toolbar_title"/>
            </FrameLayout>

            <ImageButton
                android:id="@+id/btn_ToolBarRightBtn"
                android:layout_width="32dp"
                android:layout_height="28dp"
                android:tag="0"
                android:background="@null"
                android:scaleType="fitXY"
                android:src="@drawable/icon_shopping"
                android:layout_alignParentRight="true"
                android:layout_gravity="right"
                android:layout_marginRight="10dp"
                />   

        </android.support.v7.widget.Toolbar>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_gravity="bottom"
        android:background="@color/offwhite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

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

这是我的截图,

初始视图

当视图向上滚动时,它只滚动以隐藏顶部导航栏。它不会滚动显示标签栏下方的项目,

【问题讨论】:

  • 在 NestedScrool 视图中的 android:layout_height="match_parent" 不应该是 android:layout_height="wrap_content" ...在嵌套线性布局中也是如此?
  • 你能添加一个你想要实现的视觉效果吗?
  • 请看截图

标签: android android-nestedscrollview


【解决方案1】:

app:layout_behavior="..." 应设置在 CoordinatorLayout 的直接子级上。如果您的 ViewPager 是 CoordinatorLayout 的直接子级,请将其放在 ViewPager 声明中。

【讨论】:

    【解决方案2】:

    从这里引用:Troubleshooting Coordinator Layouts

    当在 ViewPager 和父 Activity 中协调带有项目列表的 Fragment 时,您希望将 ViewPager 上的 app:layout_behavior 属性设置为 outlined here 以便分页器内的滚动条会冒泡并且可以由 CoordinatorLayout 管理。请注意,您不应将该 app:layout_behavior 属性放在片段或列表中的任何位置。

    【讨论】:

      【解决方案3】:

      你可以试试这个!我的应用程序遇到了同样的问题。这对我有用。

      <android.support.design.widget.AppBarLayout
          android:id="@+id/appbar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/AppTheme.AppBarOverlay">
      
      
          <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:attrs="http://schemas.android.com/apk/res-auto"
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:layout_scrollFlags="scroll|enterAlways"
              app:popupTheme="@style/AppTheme.PopupOverlay">
      
              <FrameLayout
                  android:id="@+id/titleContainer"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
      
                  android:gravity="center">
      
                  <com.CustomFontTextView
                      android:id="@+id/toolbar_title"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="-20dp"
                      android:gravity="center"
                      android:text="Toolbar Title"
                      android:textColor="#ffffff"
                      android:textSize="8pt"
                      attrs:customFont="handyman_bold" />
              </FrameLayout>
      
              <ImageButton
                  android:id="@+id/btn_ToolBarRightBtn"
                  android:layout_width="32dp"
                  android:layout_height="28dp"
                  android:layout_alignParentRight="true"
                  android:layout_gravity="right"
                  android:layout_marginRight="10dp"
                  android:background="@null"
                  android:scaleType="fitXY"
                  android:src="@drawable/icon_shopping"
                  android:tag="0" />
      
      
          </android.support.v7.widget.Toolbar>
      
      </android.support.design.widget.AppBarLayout>
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
          <android.support.design.widget.TabLayout
              android:id="@+id/tabs"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="bottom"
              android:background="@color/offwhite" />
      
      
          <android.support.v4.view.ViewPager
              android:id="@+id/container"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
      
      
      </LinearLayout>
      

      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      
      <android.support.v4.widget.NestedScrollView
          android:id="@+id/scroll"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginTop="10dp"
          android:clipToPadding="false"
          android:fillViewport="true">
      
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"></LinearLayout>
      </android.support.v4.widget.NestedScrollView>
      

      【讨论】:

        【解决方案4】:

        试试这个

        scroll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
        

        【讨论】:

          【解决方案5】:

          ScrollingViewBehavior 只是为您的 ViewPager 添加顶部和底部偏移量。通常偏移量取决于 AppBarLayout 的高度,它不取决于您的 TabLayout 高度,因为它不在 AppBarLayout 中。因此,为了快速解决问题,您可以像这样将 paddingBottom 添加到您的 ViewPager 中:

          <android.support.v4.view.ViewPager
                  android:id="@+id/container"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:paddingBottom="50dp"
                  app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
          
              <android.support.design.widget.TabLayout
                  android:id="@+id/tabs"
                  android:layout_gravity="bottom"
                  android:background="@android:color/black"
                  android:layout_width="match_parent"
                  android:layout_height="50dp"/>
          

          【讨论】:

            【解决方案6】:

            所以问题来了,你不能在 FrameLayout 中有 NestedScrollView。

            此外,还需要对 xml 进行一些其他较小的更改才能使其正常工作。 就像 TabLayout 必须在 AppBar 布局内并被固定。

            这是你的代码 -->

            </android.support.v7.widget.Toolbar>
            
            <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_gravity="bottom"
            android:background="@color/offwhite"
            android:layout_width="match_parent"
            app:layout_collapseMode="pin"
            app:layout_anchor="@+id/appbar"
            app:layout_anchorGravity="bottom"
            android:layout_height="wrap_content"/>
            
            </android.support.design.widget.AppBarLayout>
            
            
            <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
            

            并且 --> 更改您的 Fragment 代码以删除框架布局!

                <android.support.v4.widget.NestedScrollView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:CoverFlowPager="http://schemas.android.com/apk/res-auto"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                    android:id="@+id/scroll"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="10dp"
                    android:clipToPadding="false"
                    android:fillViewport="true">
            
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
                    </LinearLayout>
            
                </android.support.v4.widget.NestedScrollView>
            

            希望这会有所帮助!

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-09-15
              • 1970-01-01
              • 2016-10-08
              • 2016-01-12
              • 1970-01-01
              相关资源
              最近更新 更多