问题描述
我想在滚动时固定TabLayout
I want to pin TabLayout when scroll
我尝试过...代码.但无法将TabLayout固定在工具栏下方
I have tried ...code. but not able to pin TabLayout below toolbar
在这里我有工具栏,下面有滚动视图,在该滚动视图内部我有很多布局...
In this I have toolbar and below that I have scrollview and inside that scrollview I have many layouts...
我想固定在ViewPager上方(底部)的TabLayout.当我向下滚动时,tabLayout被固定,并且显示了视图寻呼机(看起来类似于tabLayout被固定在工具栏下方).
I want to pin TabLayout which is above the ViewPager (at the bottom). When I scroll down the tabLayout is pinned and the view pager shown(looks something Like tabLayout is pinned below the toolbar).
以下是示例图片: https://imgur.com/dGddsey
视频示例: https://drive.google.com/open?id=146UaY89cgxQ3XJyZb1Uos-JQjurDL2hWa
activity_main.xml
<?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>
这篇关于将TabLayout固定到工具栏Scrollview的顶部和下方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!