Avoid ViewPager going under Toolbar

£可爱£侵袭症+ 提交于 2019-12-04 16:56:52

After a lot of struggling, I found that putting a NestedScrollView inside my FrameLayout and assigning a layout_behaviour to my FrameLayout made the Scroll View inherit it and now it all works like a charm!

This is my DrawerLayout:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<include
    layout="@layout/appbar_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:fitsSystemWindows="true"
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/tealfifty"
        app:itemBackground="@drawable/drawer_item"
        app:itemTextColor="@color/drawer_item"
        app:itemIconTint="@color/drawer_item"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/navigation_view_items" />
</android.support.v4.widget.DrawerLayout>

appbar_content.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include layout="@layout/toolbar" />

    <android.support.design.widget.TabLayout
        android:visibility="visible"
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/teal"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/purple800"
        app:tabIndicatorHeight="4dp"
        app:itemIconTint="@color/tabicons"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/tealfifty"
        app:tabTextAppearance="@style/TabCapsFalse"
        app:tabTextColor="@color/iron" />
</android.support.design.widget.AppBarLayout>



<FrameLayout android:id="@+id/content_main_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:showIn="@layout/appbar_content"
    xmlns:tools="http://schemas.android.com/tools">
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@drawable/addico"
    app:layout_anchor="@id/content_main_frame"
    app:layout_anchorGravity="bottom|end" />

Put your tab layout and view pager inside a linear layout with orientation vertical. So your linear layout will be the child of Coordinator layout. THis will make sure your view pager starts below tablayout.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!