问题
Based on
this answer I tried to make a solution for my Use Case. Instead of using a Collapsing toolbar I'm using a regular one but with a TabLayout. However, for the TabLayout I need to use a ViewPager and I have no idea where to place it in order to avoid trouble with the FrameLayout. ViewPager was in my Host Fragment and when its layout replaced the FrameLayout, the content went below the Toolbar and TabLayout for some reason...
I tried putting it below that line
<include layout="@layout/content_main" />
in the app_bar_main.xml but it messed up my UI.
The real question is, if I need a FrameLayout for my fragments and I need a ViewPager for my TabLayout, how do I implement a solution?
You can see that I moved the view a bit so that the Toolbar hides. This way it's visible the ViewPager takes up space even below the Toolbar (when it's visible)
Current attempt - appbar_content (Toolbar is just a Toolbar in separate layout):
<?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>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main (removed NestedScrollView as it screwed up everything, put my whole fragment view like in a small frame(maybe 50x50 size) in the middle of the screen)
<?xml version="1.0" encoding="utf-8"?>
<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" />
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>
回答1:
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" />
回答2:
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.
来源:https://stackoverflow.com/questions/35590799/avoid-viewpager-going-under-toolbar