【问题标题】:AppBarLayout with tabs and ViewPager带有标签和 ViewPager 的 AppBarLayout
【发布时间】:2019-02-03 08:23:45
【问题描述】:

我对@9​​87654323@ 和AppBarLayout 有疑问,因为我希望我的标签位于底部,而现在它们位于顶部,当我向上滚动时它们会与图像一起隐藏。还有第二个问题 - AppBarLayout with image 在我滚动文本时不会隐藏,它只会在我尝试 scroll 顶部的图像时隐藏/显示

<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:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows = "true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main_appbar"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">


        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main_collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp">


            <ImageView
                android:id="@+id/main_backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                android:src="@raw/test"
                app:layout_collapseMode="parallax"/>



            <android.support.v7.widget.Toolbar
                android:id="@+id/main_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/sliding_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                />

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



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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </android.support.v4.view.ViewPager>


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

编辑:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_pager_scrollview">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:lineSpacingMultiplier="1.2"
        android:text="@string/lorem_ip"/>

</ScrollView>

【问题讨论】:

  • 文本包裹在什么视图中来处理滚动?
  • ScrollView,我将进行编辑以包含该内容
  • 将其更改为 NestedScrollView 以及我更新的解决方案

标签: java android android-tablayout android-appbarlayout


【解决方案1】:

滚动视图不支持 Appbar 行为,将其更改为 NestedScrollView 并使用以下布局:

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 app:layout_behavior="@string/appbar_scrolling_view_behavior">

  <android.support.design.widget.TabLayout
  android:id="@+id/sliding_tabs"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

   <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

 </LinearLayout

【讨论】:

  • 我认为没有必要将它包装在 NestedScrollView 中
  • 也就是说,文本视图没有被包裹在滚动视图中,并且那部分代码没有被粘贴。
  • 当我这样做时,它会抛出“已停止”错误并显示此消息 android.view.InflateException: Binary XML file line #71: ScrollView can host only one direct child
  • 是的,很抱歉。仅当具有 ScrollingViewBehavior 的视图的子视图层次结构(在您的情况下为 viewpager)包含可滚动视图时,AppBarLayout 才能处理滚动。视图层次结构是否包含滚动成员?
  • 是的,我的意思是,将ScrollView 更改为NestedScrollView。您在LinearLayout 中捕获所有内容的解决方案也与 jle 的解决方案一样有效。非常感谢您的帮助
【解决方案2】:

尝试像这样为您的TabLayout 添加重力:

    <android.support.design.widget.TabLayout
                    android:id="@+id/sliding_tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    />

编辑: 为了在 CollapsingToolbarLayout 下面有 TabLayout,只需将其放入您的 ViewPager 中,如下所示:

<android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

</android.support.v4.view.ViewPager>

【讨论】:

  • 这实际上有点工作,但这不是我想要的,我希望这些标签像 here 那样分开,现在它们看起来像 that
  • 在这种情况下,tablayout 应该在嵌套滚动视图内的 appbar 之外。检查我更新的答案。
  • 我认为这就是您要找的。所以 TabLayout 应该和 AppBar 分开?
  • 如果它应该在外面,所以在工具栏下方,您可以将工具栏放入您的 ViewPager。我希望这就是你的意思,否则就告诉我
  • 是的,你是对的,这行得通,谢谢。现在我需要解决滚动问题。
【解决方案3】:

请将您的代码更改为:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:paddingTop="@dimen/app_bar_padding"
    android:background="@drawable/dash_bar"
    android:layout_height="192dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="10dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/expandedImage"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="parallax">

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


        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            app:tabMaxWidth="0dp"
            app:tabGravity="fill"
            app:tabIndicatorColor="#ffffff"
            app:tabTextColor="#b3ffffff"
            android:background="#00000000"
            app:tabSelectedTextColor="#ffffff"
            app:tabIndicatorHeight="5dp"
            app:tabMode="fixed" />
    </android.support.design.widget.CollapsingToolbarLayout>

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

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

现在要在滚动文本时隐藏图像,将此行添加到正在滚动的视图中(我假设您只是在那里使用了 textView 和滚动条):

app:layout_behavior="@string/appbar_scrolling_view_behavior"

【讨论】:

    猜你喜欢
    • 2019-08-08
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多