[英]Toolbar hides PagerTitleStrip and part of RecyclerView
Screenshot 1 and Screenshot 2 of my app 我的应用的屏幕截图1和屏幕截图2
The problem is basically this: The PagerTitleStrip and the RecyclerView go below my Toolbar even though I have set layout_behaviour to my ViewPager. 问题基本上是这样的:即使我已将layout_behaviour设置为我的ViewPager,PagerTitleStrip和RecyclerView也会低于我的工具栏。
More detail: 更多详情:
I have a main activity that uses the layout below and then from it I launch fragments. 我有一个主要的活动,使用下面的布局,然后从它发起片段。 Before trying the CoordinatorLayout + ViewPager and all of the other fancy stuff I just had a FrameLayout in which I displayed my fragments. 在尝试CoordinatorLayout + ViewPager和所有其他花哨的东西之前,我只有一个FrameLayout,我在其中显示了我的片段。 However, I decided that I want to use 但是,我决定要使用
app:layout_scrollFlags="scroll|enterAlways"
app:layout_behavior="@string/appbar_scrolling_view_behavior" 应用程式:layout_behavior = “@串/ appbar_scrolling_view_behavior”
so that my Toolbar disappears when I scroll the recyclerview. 这样当我滚动recyclerview时,我的工具栏就会消失。 I found online that FrameLayout had no behaviour in it to allow me to fix the position of the recyclerView going under the Toolbar and that's why I changed to ViewPager + CoordinatorLayout to manage my fragments. 我在网上发现FrameLayout没有任何行为允许我修复在工具栏下的recyclerView的位置,这就是为什么我改为ViewPager + CoordinatorLayout来管理我的片段。
However, that introduced a few problems. 但是,这引入了一些问题。 What I want to achieve is - A toolbar with a PagerTitleStrip attached to it, when I scroll the toolbar disappears leaving the PagerTitleStrip visible ideally, maybe it hides as well I don't care that much about that. 我想要实现的是 - 一个附有PagerTitleStrip的工具栏,当我滚动工具栏消失时,理想的是可以看到PagerTitleStrip,也许它隐藏起来我也不在乎那么多。 But I want my Navigation Drawer to keep working and it doesn't. 但是我希望我的导航抽屉继续工作而不是。 It's like it doesn't exist. 它就像它不存在。
Now, this first layout actually has all of the above features working, but the problem is the strip is below the toolbar and it's not visible unless the Toolbar hides when I scroll. 现在,第一个布局实际上具有上述所有功能,但问题是条带位于工具栏下方,除非滚动时工具栏隐藏,否则它不可见。 Drawer works. 抽屉工作。 Recycler view items are partially hidden below the Toolbar unfortunately - they don't start scrolling from the right point of the screen even though I have that set - appbar_scrolling_view_behavior 遗憾的是,Recycler视图项部分地隐藏在工具栏下方 - 即使我有这个设置,它们也不会从屏幕的右侧点开始滚动 - appbar_scrolling_view_behavior
Layout m_drawerlayout.xml for my main activity to inflate: 布局m_drawerlayout.xml,用于我的主要活动:
<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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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.v4.view.PagerTitleStrip
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:layout_gravity="top"
android:textSize="15sp"
android:textColor="@color/tealfifty"
android:background="@color/teal">
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
<ListView
android:id="@+id/drawer_list"
android:background="@color/tealDark"
android:cacheColorHint="@android:color/transparent"
android:choiceMode="singleChoice"
android:divider="@drawable/list_divider"
android:dividerHeight="1dp"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/app_bar"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
And this is the code for my app_bar.xml 这是我的app_bar.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/top_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/teal"
app:theme="@style/ToolbarTheme"
app:popupTheme="@style/Green.Overlay.LightPopup"
app:layout_scrollFlags="scroll|enterAlways" >
<Spinner android:id="@+id/spinner"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:paddingRight="20dp"
/>
</android.support.v7.widget.Toolbar>
I tried moving elements around, in and out of other components but no success. 我尝试在其他组件内外移动元素,但没有成功。 For example, it seems logical that to make the PagerTitleStrip attached to the Toolbar and share one behaviour I'd have to nest the PagerTitleStrip inside the AppBar layout but PagerTitleStrip has to be a direct child of ViewPager to work...Any suggestions guys about all this? 例如,将PagerTitleStrip附加到工具栏并分享一个我必须将PagerTitleStrip嵌套在AppBar布局中的行为似乎是合乎逻辑的,但PagerTitleStrip必须是ViewPager的直接子项才能工作...任何建议的人这些所有? I'm new to all these design functionalities and I researched a lot without success, there are similar topics here but not at all what I need. 我是所有这些设计功能的新手,我研究了很多没有成功,这里有类似的主题,但根本不需要我。 Seems there's almost nothing on the internet about things I'm trying to do above (there are a few) compared to other basic topics. 似乎互联网上几乎没有关于我正在尝试做的事情(有一些)与其他基本主题相比。
If anyone is wondering why I'm using ViewPager when I have no tabs, it's because as I said, FrameLayout has no behaviour allowing me to use a disappearing Toolbar correctly and that's what I found is good to use. 如果有人想知道为什么我在没有标签时使用ViewPager,那是因为正如我所说,FrameLayout没有允许我正确使用消失工具栏的行为,这就是我发现的好用。 I also saw NestedScrollView could be used but I haven't used it ever so... I now want to possibly use the ViewPagerTitle which I guess limits me to ViewPager for the Fragments. 我也看到了NestedScrollView可以使用,但是我还没有使用过它......我现在想要使用ViewPagerTitle,我猜这会限制我使用ViewPager作为片段。
The problem is in the: 问题在于:
<android.support.design.widget.CoordinatorLayout
<android.support.v4.widget.DrawerLayout
Which the DrawerLayout
should be the root layout. 哪个DrawerLayout
应该是根布局。
Please see: http://developer.android.com/training/implementing-navigation/nav-drawer.html#DrawerLayout 请参阅: http : //developer.android.com/training/implementing-navigation/nav-drawer.html#DrawerLayout
To add a
navigation drawer
, declare your userinterface
with aDrawerLayout
object as the root view of your layout . 要添加navigation drawer
,请使用DrawerLayout
对象声明用户interface
作为布局的根视图 。 Inside theDrawerLayout
, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of thenavigation drawer
. 在DrawerLayout
,添加一个包含屏幕主要内容的视图(隐藏抽屉时的主要布局)和另一个包含navigation drawer
内容的视图。
Also, it is not recommended to use ListView
instead of NavigationView
. 此外,不建议使用ListView
而不是NavigationView
。
And here is the doc suggested way to do(except that ListView
:) ): 这里是doc建议的方法(除了ListView
:)):
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
And also: 并且:
However, that introduced a few problems. 但是,这引入了一些问题。 What I want to achieve is - A
Toolbar
with aPagerTitleStrip
attached to it, when I scroll the toolbar disappears leaving thePagerTitleStrip
visible ideally, maybe it hides as well I don't care that much about that. 我想实现的是-一个Toolbar
与PagerTitleStrip
连接到它,当我滚动工具栏消失离开PagerTitleStrip
可见的理想,也许它隐藏,以及我不很在乎这一点。 But I want myNavigation Drawer
to keep working and it doesn't. 但是我希望我的Navigation Drawer
继续工作而不是。
Check my answer about this one: 检查我的答案:
https://stackoverflow.com/a/35241363/4409113 https://stackoverflow.com/a/35241363/4409113
You just put your Toolbar inside CollapsingToolbarLayout
like this: 您只需将工具栏放在CollapsingToolbarLayout
如下所示:
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingtoolbarly"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|snap">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<ImageView
android:layout_width="match_parent"
android:layout_height="190dp"
android:minHeight="190dp"
android:scaleType="fitXY"
android:src="@drawable/header"
app:layout_collapseMode="parallax" />
</android.support.design.widget.CollapsingToolbarLayout>
It should collapsed and the Toolbar will be hided after that.And if you want to keep that PagerTitleStrip
(Recommended to use TabLayout
Nowadays), just put it below the CollapsingToolbarLayout
and above the </android.support.design.widget.AppBarLayout>
like this: 它应该折叠,然后PagerTitleStrip
工具栏。如果你想保留PagerTitleStrip
(建议现在使用TabLayout
),只需将它放在CollapsingToolbarLayout
下面和</android.support.design.widget.AppBarLayout>
之上这个:
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
That's pretty much it, Goodluck then. 那就是它,Goodluck然后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.