Android 嵌套滚动视图无法滚动。

10

我有一个嵌套的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">

现在当我滚动视图时,视图没有滚动。但是由于布局放置在Cordinator布局中,它会向上移动,直到ToolBar被隐藏。但它不会向上滚动。

这是我的主活动XML文件,ViewPager位于选项卡布局中。

<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>

这是我的屏幕截图,

初始视图 在此输入图片描述

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

在此输入图片描述


在NestedScrollView中,android:layout_height="match_parent"不应该改为android:layout_height="wrap_content"吗?同样,在您的嵌套线性布局中也是如此吧? - longi
你能添加一个你想要实现的可视化吗? - jobbert
请查看屏幕截图。 - Zach
6个回答

1

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


0

从这里引用:协调布局的故障排除

当在ViewPager中有一个项目列表的片段和父活动之间进行协调时,您希望将app:layout_behavior属性放在ViewPager上,如此处所述,以便Pager内的滚动可以被协调布局管理。请注意,您不应该在片段或列表内部的任何位置放置app:layout_behavior属性。


0
你可以试试这个!我之前的应用也遇到了同样的问题,这个方法对我很有效。

<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>


0

试一下

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

0

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

<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"/>

-1

所以问题在于,你不能在FrameLayout中使用NestedScrollView。

此外,在xml中需要进行一些其他较小的更改才能使其正常工作。 例如,TabLayout必须位于AppBar Layout内并被固定。

这是你的代码 -->

</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"/>

而且 -> 更改您的片段代码以删除帧布局!

    <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>

希望这能帮到你!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接