为什么底部TabLayout在向上滚动时不停留在顶部?

问题描述 投票:1回答:1

我正在尝试滚动视图,当我向上滚动时,底部的“标签布局”应滚动到顶部并保持顶部。我有以下布局的结构

<RelativeLayout>
 <CoordinatorLayout>
      <AppBarLayout>
        <Toolbar>
      </AppBarLayout>

 <NestedScrollView>
    <LinearLayout>
        <CardView>
        <TabLayout>
        <ViewPager>
    </LinearLayout>
 </NestedScrollView>

 </CoordinatorLayout>
</RelativeLayout>

图像1:当前我正在得到的输出

The output which currently i am getting

我的布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:orientation="vertical"
    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:background="#e6e6e6"
    tools:context=".PostViewerActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <com.google.android.material.appbar.AppBarLayout
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
                android:background="@color/white"
                app:title="Post View"
                android:id="@+id/PostViewerToolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:fillViewport="true"
        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">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <ImageView
                    android:layout_marginTop="10dp"
                    android:id="@+id/PostViewerSharedImg"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/temp_img"
                    android:scaleType="fitCenter"
                    android:adjustViewBounds="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

                <View
                    android:layout_marginTop="10dp"
                    android:background="@color/bottomNavigationColor"
                    android:layout_width="match_parent"
                    android:layout_height="2dp"/>
                <TextView
                    android:layout_marginTop="10dp"
                    android:id="@+id/DescriptionTv"
                    android:textStyle="bold"
                    android:text="Post description"
                    android:textColor="@color/black_light"
                    android:textSize="17sp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
        </androidx.cardview.widget.CardView>

                <com.google.android.material.tabs.TabLayout
                    android:layout_gravity="bottom"
                    android:background="@color/white"
                    android:id="@+id/TabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <com.google.android.material.tabs.TabItem
                        android:text="Likes"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>
                    <com.google.android.material.tabs.TabItem
                        android:text="Comments"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>

                </com.google.android.material.tabs.TabLayout>
            <androidx.viewpager.widget.ViewPager
                android:layout_gravity="bottom"
                android:id="@+id/Viewpager"
                android:layout_marginTop="5dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>

如何停止我的选项卡布局以使其向上滚动?

图像2:向上滚动后的预期输出

android android-layout android-tablayout android-coordinatorlayout android-nestedscrollview
1个回答
0
投票

尽管在app:layout_collapseMode="pin"上添加TabLayout会有所帮助,但仍然有一些应解决的问题。

[尝试移除RelativeLayout并替换为:CoordinatorLayout,以便CoordinatorLayout将成为根标签,并且将处理诸如滚动之类的效果。

© www.soinside.com 2019 - 2024. All rights reserved.