以编程方式滚动内容时,Appbar滚动行为不起作用

时间:2018-07-19 14:25:30

标签: android android-studio smooth-scrolling android-appbarlayout

我已经在我的应用程序中实现了appbar滚动行为,并且就像whatsapp应用程序一样甜美地工作。

但是,仅当我通过触摸(即,用手指滚动)滚动时,折叠行为才起作用。当我以编程方式滚动内容时,内容确实滚动了,但是appbar仍处于完全展开状态,并且根本不会折叠。没有崩溃,只是内容向上滚动。

这是我的主要布局代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    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
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="@string/app_name">

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

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

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_1" />

            <android.support.design.widget.TabItem                    
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_2" />

        </android.support.design.widget.TabLayout>
    </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.CoordinatorLayout>

这是我的标签片段,在其中进行滚动:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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/nested_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".Tutorial">

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

        <include
            android:id="@+id/layout1"
            layout="@layout/panel1"/>
        <include
            android:id="@+id/layout2"
            layout="@layout/panel2"/>
        <include
            android:id="@+id/layout3"
            layout="@layout/panel3"/>

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

*此片段在主版面的viewPager中

这是我用来滚动NestedScrollView的方法

nestedScrollView.smoothScrollTo(0, panel3.getTop());

NestedScrollView的内容很长,足以让我滚动。当我通过触摸而不是通过编程方式滚动时,可以实现“ appbar折叠”滚动行为。我尝试用RecyclerView替换NestedScrollView,但无济于事。

请帮助!预先感谢!

0 个答案:

没有答案