我想在滚动时固定 TabLayout
我已经尝试过...代码。但无法将 TabLayout 固定在工具栏下方
在这个我有工具栏,下面有滚动视图,在滚动视图内我有很多布局......
我想固定位于 ViewPager 上方(底部)的 TabLayout。当我向下滚动时,tabLayout 被固定并显示视图寻呼机(看起来像 tabLayout 被固定在工具栏下方)。
这是示例图像:
https://i.stack.imgur.com/Md13C.jpg https://i.stack.imgur.com/Md13C.jpg
示例视频:
https://drive.google.com/open?id=146UaY89cgxQ3XJyZb1Uos-JQjurDL2hW https://drive.google.com/open?id=146UaY89cgxQ3XJyZb1Uos-JQjurDL2hW
活动主文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="#EAEAEA"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#03A9F4"
android:elevation="3dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:padding="4dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="180dp"
android:elevation="2dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:id="@+id/live_wallpaper_card"
android:layout_width="match_parent"
android:layout_height="120dp"
android:elevation="2dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
<!--Todo: horizontal card scroll-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="Category"
android:textColor="#000"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/more_textTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="More"
android:textStyle="bold" />
</RelativeLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="15dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="15dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="15dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="15dp"
app:cardUseCompatPadding="true" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<!--Todo: Featured Album horizontal scroll view-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Featured Albums"
android:textColor="#000"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="300dp"
android:layout_height="220dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="300dp"
android:layout_height="220dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="300dp"
android:layout_height="220dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="300dp"
android:layout_height="220dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
app:tabIndicatorColor="@color/colorPrimary"
app:tabIndicatorHeight="3dp"
app:tabSelectedTextColor="@color/black"
app:tabTextColor="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="650dp"
android:background="@color/colorPrimaryDark" />
</LinearLayout>
<!--
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
</LinearLayout>
-->
</LinearLayout>
</ScrollView>
</LinearLayout>
这是答案
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:background="@android:color/white"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FAFAFA"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="#03A9F4"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:orientation="vertical"
app:layout_collapseMode="parallax">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="180dp"
android:elevation="2dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:id="@+id/live_wallpaper_card"
android:layout_width="match_parent"
android:layout_height="120dp"
android:elevation="2dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
<!--Todo: horizontal card scroll-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="Category"
android:textColor="#000"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/more_textTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="More"
android:textColor="#BF000000"
android:textStyle="bold" />
</RelativeLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="15dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="15dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="15dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="15dp"
app:cardUseCompatPadding="true" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<!--Todo: Featured Album horizontal scroll view-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Featured Albums"
android:textColor="#000"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="300dp"
android:layout_height="220dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="300dp"
android:layout_height="220dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="300dp"
android:layout_height="220dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
<androidx.cardview.widget.CardView
android:layout_width="300dp"
android:layout_height="220dp"
app:cardCornerRadius="20dp"
app:cardUseCompatPadding="true" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</LinearLayout>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbarMain"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
app:tabIndicatorFullWidth="false"
app:tabIndicatorColor="#FFEB3B"
app:tabSelectedTextColor="#FFEB3B"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabTextColor="@color/colorPrimaryDark" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)