[英]Hide toolbar in activity by scrolling a recyclerview inside the Fragment?
My Activity XML is 我的Activity XML是
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:title="@string/app_name"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/drawerNavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="@color/navview_text_color"
app:menu="@menu/home_drawer_items_menu"/>
</android.support.v4.widget.DrawerLayout>
My Fragment XML is : My Fragment 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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:clickable="true"
android:src="@drawable/ic_plus"
android:tint="@android:color/white"
app:elevation="5dp"
app:layout_anchor="@id/viewPager"
app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>
My fragment
uses tabs
and nested fragments
inside the ViewPager
and my activity
uses navigation drawer
so i can't move the tabs
to activity
as the tabs are only needed for this specific fragment
. 我的
fragment
在ViewPager
使用tabs
和nested fragments
,我的activity
使用navigation drawer
所以我无法将tabs
移动到activity
因为只有这个特定fragment
需要制表符。 And other fragments just need a toolbar
. 而其他片段只需要一个
toolbar
。 At the same time all the fragments need to access the navigation drawer
. 同时,所有片段都需要访问
navigation drawer
。
Now what i want to do is somehow associate app:layout_scrollFlags="scroll|enterAlways"
with my activity's toolbar. 现在我要做的是以某种方式关联
app:layout_scrollFlags="scroll|enterAlways"
和我的活动工具栏。 So that whenever i scroll my RecyclerView
inside the fragment
the toolbar
in the activity gets hidden. 因此,每当我在
fragment
内滚动我的RecyclerView
时,活动中的toolbar
就会被隐藏。
It would be great if you guys can point me in the right direction or help me figure out how to do this ? 如果你们能指出我正确的方向或帮助我弄清楚如何做到这一点会很棒吗?
If I understand your question correctly what you can do is 如果我正确理解你的问题,你可以做的是
1) Include the Toolbar and NavigationDrawer
in MainActivity
so it will be accessible in all fragments too. 1)在
MainActivity
包含工具栏和NavigationDrawer
,以便它也可以在所有片段中访问。
2) Include tabs in in only ParentFragment
which contains ViewPager
for other fragments 2)仅在
ParentFragment
中包含选项卡,其中包含用于其他片段的ViewPager
with this approach your Toolbar and NavigationDrawer
will be visible in all fragments while tabs only in ParentFragment
使用此方法,您的工具栏和
NavigationDrawer
将在所有片段中可见,而选项卡仅在ParentFragment
Hope it helps 希望能帮助到你
I have solved this issue, though there is still room for some minor improvements like the FAB
is moving up and down whenever i scroll RecyclerView
inside the Fragment
我已经解决了这个问题,虽然还是有余地的一些小的改进,如
FAB
是上下 移动 ,每当我滚动RecyclerView
内部Fragment
In order to make toolbar
respond to app:layout_scrollFlags="scroll|enterAlways"
this is what i did 为了使
toolbar
响应app:layout_scrollFlags="scroll|enterAlways"
这就是我所做的
I changed my Activity XML to: 我将Activity XML更改为:
<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">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:title="@string/app_name"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/drawerNavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="@color/navview_text_color"
app:menu="@menu/home_drawer_items_menu"/>
</android.support.v4.widget.DrawerLayout>
Also i changed my Fragment XML to: 我还将我的Fragment XML更改为:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="60dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:src="@drawable/ic_plus"
android:tint="@android:color/white"
app:elevation="5dp"/>
</RelativeLayout>
</LinearLayout>
Also ran into this problem. 也碰到了这个问题。 But a solution was found.
但是找到了解决方案。 The solution with xml rework can not be, so you need to do it programmatically.
使用xml返工的解决方案不可能,因此您需要以编程方式执行此操作。 All we need is Inflater .
我们需要的只是Inflater 。 Yes, yes, that's so simple.
是的,是的,那很简单。 On the fly change xml to work all scrolling.
在飞行中更改xml以工作所有滚动。 Inflate/AddView/RemoveView - that's all you need!
Inflate / AddView / RemoveView - 这就是你所需要的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.