【问题标题】:Android TabLayout With Viewpager Inside CoordinateLayout With BottomAppBarAndroid TabLayout 与 Viewpager 内部 CoordinateLayout 与 BottomAppBar
【发布时间】:2020-11-08 02:02:43
【问题描述】:

我将 TabLayout 与位于 CoordinatorLayout 内的 ViewPager 一起使用。在这里,我还有一个带有 FAB 的 BottomAppBar。下面是布局图。

MainActivity Layout

这是我用于此布局的 xml 代码。

<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity">

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingBottom="100dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/mainTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            app:tabGravity="fill"
            app:tabInlineLabel="true"
            app:tabMode="fixed" />

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/mainViewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/mainBottomAppBar"
    style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:hideOnScroll="true"
    app:menu="@menu/main_menu"
    app:navigationIcon="@drawable/icon_menu" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/mainFAB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="@color/colorPrimary"
    app:layout_anchor="@id/mainBottomAppBar"
    app:srcCompat="@drawable/icon_add"
    app:tint="@color/white" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

但问题是,在标签(或片段)之间滑动不起作用。如果我像下面这样编写 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:orientation="vertical"
tools:context=".activities.MainActivity">

<com.google.android.material.tabs.TabLayout
    android:id="@+id/mainTabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    app:tabGravity="fill"
    app:tabInlineLabel="true"
    app:tabMode="fixed" />

<androidx.viewpager.widget.ViewPager
    android:id="@+id/mainViewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

我不明白我需要做出哪些改变。任何类型的解决方案或建议将不胜感激。提前致谢。

【问题讨论】:

    标签: android android-viewpager android-tablayout android-coordinatorlayout android-bottomappbar


    【解决方案1】:

    NestedScrollView 中添加android:fillViewport 属性

    <androidx.core.widget.NestedScrollView
        android:fillViewport="true"
        ..>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多