簡體   English   中英

禁用在CollapsingToolbar中的TabLayout片段中滾動

[英]To disable scrolling in TabLayout fragment in CollapsingToolbar

我有一個帶有CollapsingToolbar的屏幕,下面有一個帶有一些片段的TabLayout。

其中一個片段具有使用RecyclerView檢索的數據。

問題在於屏幕縮小了使RecyclerView一起滾動的片段並延遲了CollapsingToolbar,從而產生了不良效果。

有什么方法可以阻止片段的Scroll,直到CollapsingToolbar完成該過程並完全折疊嗎?

鏈接Gif示例

代碼活動:

<?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/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true">


        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/cinzaMEdio"
            app:expandedTitleGravity="bottom"
            android:background="@android:color/white"
            app:expandedTitleMarginBottom="170dp"
            app:expandedTitleMarginStart="10dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            >


            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="225dp">

                <ImageView
                    android:id="@+id/main.backdrop"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/nav_header_height"
                    android:fitsSystemWindows="true"
                    android:src="@drawable/hair"
                    app:layout_collapseMode="parallax"
                    android:adjustViewBounds="true"
                    android:cropToPadding="false"
                    android:scaleType="fitXY"
                    />

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="225dp"
                    android:background="@drawable/gradient_bg">

                </FrameLayout>


                <TextView
                    android:id="@+id/tvPerEmpNome"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginStart="10dp"
                    android:layout_marginBottom="28dp"
                    android:text="Firma X"
                    android:textColor="@android:color/white"
                    android:textSize="24sp"
                    app:layout_collapseMode="parallax"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/tvNomeCidade"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginBottom="10dp"
                    android:layout_marginStart="10dp"
                    android:text="São Paulo - SP"
                    android:textColor="@android:color/white"
                    android:textSize="14sp"
                    app:layout_collapseMode="parallax" />
            </FrameLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_marginBottom="120dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:orientation="horizontal"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal">

                    <LinearLayout
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical"
                        android:clickable="true"
                        android:background="@drawable/ripple_menus_perfil_empresa">

                        <ImageButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:adjustViewBounds="true"
                            android:background="@drawable/ic_person_add"
                            android:baselineAlignBottom="false"
                            android:clickable="true"
                            android:cropToPadding="false"
                            android:scaleType="centerCrop"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:paddingTop="5dp"
                            android:text="Seguir"
                            android:textSize="12sp" />
                    </LinearLayout>


                    <LinearLayout
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical"
                        android:clickable="true"
                        android:background="@drawable/ripple_menus_perfil_empresa">

                        <ImageButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:adjustViewBounds="true"
                            android:background="@drawable/ic_star_half"
                            android:baselineAlignBottom="false"
                            android:clickable="true"
                            android:cropToPadding="false"
                            android:scaleType="centerCrop" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:paddingTop="5dp"
                            android:text="Avaliações"
                            android:textSize="12sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical"
                        android:clickable="true"
                        android:background="@drawable/ripple_menus_perfil_empresa">

                        <ImageButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:adjustViewBounds="true"
                            android:background="@drawable/ic_photo"
                            android:baselineAlignBottom="false"
                            android:clickable="true"
                            android:cropToPadding="false"
                            android:scaleType="centerCrop" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:paddingTop="5dp"
                            android:text="Fotos"
                            android:textSize="12sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical"
                        android:clickable="true"
                        android:background="@drawable/ripple_menus_perfil_empresa">

                        <ImageButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:adjustViewBounds="true"
                            android:background="@drawable/ic_business"
                            android:baselineAlignBottom="false"
                            android:clickable="true"
                            android:cropToPadding="false"
                            android:scaleType="centerCrop" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:paddingTop="5dp"
                            android:text="Sobre"
                            android:textSize="12sp" />
                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_marginBottom="65dp"
                android:orientation="horizontal"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_marginRight="5dp">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Agendar"
                        android:textSize="16sp"
                        android:textAlignment="center"
                        android:background="@drawable/ripple_button_border_black"
                        android:textColor="@android:color/black"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="5dp"
                        android:clickable="true"
                        android:paddingBottom="5dp"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:layout_marginLeft="5dp">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Mensagem"
                        android:textAlignment="center"
                        android:textSize="16sp"
                        android:background="@drawable/ripple_button_black"
                        android:textColor="@android:color/white"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="5dp"
                        android:clickable="true"
                        android:paddingBottom="5dp"/>
                </LinearLayout>

            </LinearLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbarPerfilEmpresa"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="top"
                android:layout_marginBottom="48dp"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways" />

            <View
                android:layout_width="match_parent"
                android:layout_height="3dp"
                android:layout_gravity="bottom|end"
                android:layout_marginBottom="48dp"
                android:background="@color/cinza300"
                app:layout_collapseMode="parallax" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs_perf_emp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="@color/cinzaMEdio"
                app:layout_collapseMode="pin"
                app:tabTextAppearance="@style/MineCustomTabText">

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

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

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollViewContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffff0d02"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <android.support.v4.view.ViewPager
                android:id="@+id/pager_tabs_perf_emp"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:minHeight="600dp"
                android:layout_weight="1"
                android:background="@color/cinza300"
                />
        </LinearLayout>

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


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

代碼片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/cardFeedUser_list" />

</RelativeLayout>

您必須對cardFeedUser_list recyclerView使用setNestedScrollingEnabled(false)

如果您使用的是API> 21

recyclerView.setNestedScrollingEnabled(false)

如果您使用的API <21

ViewCompat.setNestedScrollingEnabled(recyclerView, true);

對於支持庫,您可以輕松控制應用欄的可見性。

setExpanded(true,true);

要么

setExpanded(false,true);

另外,您可以使用DragCallback方法

private void setDragCallback() {
setDragCallback(new DragCallback() {
    @Override
    public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
        return mEnabled;
    }
});

}

解。 刪除了NestedScrollView

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

    <android.support.v4.view.ViewPager
        android:id="@+id/pager_tabs_perf_emp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="600dp"
        android:layout_weight="1"
        android:background="@color/cinza300"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM