尴尬的app:layout_scrollFlags="scroll|enterAlways" 配合NavigationDrawer

昨天想到了NavigationDrawer中Item点击的问题。

点击Drawer中的一个Item需要到一个新的页面,你是应该打开一个新的Activity呢还是直接用fragment呢?

如果打开新的activity,那么新的Activity覆盖,Drawer按钮就会消失;当然你也可以在新的Activity上加同样的NavigationDrawer,但是那样太浪费资源了。

但如果你用fragment,那么你只需要ToolBar下面的区域,整个作为一个可替换的container,然后不同的fragment进行replace就行了。很多App都是这样做的,比如Google+,比如知乎等等,这样他们在切换左侧的Drawer Item的时候,只需要replace container(不要忘了把fragment入栈)。

 

可是,如果你的App有一个ViewPager(FragmentPagerAdapter)呢?

通常情况,AppBarLayout会包含widget.Toolbar和TabLayout。

其实也是可以的,只需要把ToolBar下面的区域抽离出来(这样我们就必须把TabLayout从AppBarLayout里面抽出来),作为fragment的container就行了,然后在MainActivity启动的时候启动一个MainFragment,里面包含:

  • tabLayout
  • viewPager

唯独有一个问题:如果你的应用想要实现ToolBar的滚动时折叠,就像这样:

这样如果仅仅用一个Activity+多个Fragment,是不行的 。下面说一下为什么。

用一个Activity+多个Fragment意味着要有给fragments一个公共的container。

我的做法是,把ToolBar下面的区域提取出来了,如下,content_main是一个空白的layout,也就是container,activity启动时我可以往里面插入一个含有tabLayout和viewPager的fragment。不知道我的意思表达清楚没有。。

 

 

我唯一见到的app:layout_scrollFlags="scroll|enterAlways" 使用场景是在ToolBar标签中设置,与CoordinatorLayout配合对ToolBar进行滚动时隐藏。

 app:layout_scrollFlags是AppBarLayout的属性。

AppBarLayout is a vertical LinearLayout which implements many of the features of material designs app bar concept, namely scrolling gestures.

Children should provide their desired scrolling behavior through setScrollFlags(int) and the associated layout xml attribute: app:layout_scrollFlags.

This view depends heavily on being used as a direct child within a CoordinatorLayout. If you use AppBarLayout within a different ViewGroup, most of it's functionality will not work.

 尴尬的地方在于,如果你像我上面说的那样进行布局,你可以只用一个fragment实现NavigationDrawer,但你的ToolBar将不能滚动收缩。为什么呢?大概是因为上图的content_main本身不具有滚动属性了,就像ListView不能在CoordinatorLayout实现ToolBar的滚动隐藏一样。

G+、知乎等应用,首页没有ViewPager。酷安网客户端的首页有ViewPager,所以也在Item中用了独立Activity(起初我还以为是酷安的开发在偷懒-_-)。所以我还是决定采用类似酷安的方案了。

啊 浪费了好多时间。


原文地址:http://www.cnblogs.com/larrylawrence/p/5450341.html

以下代码,请帮我将Textview修改为一个imageview 。谢谢 。<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:didi="http://schemas.android.com/apk/res-auto" > <com.blog.demo10.NestedViewGroup android:id="@+id/dd_view_group" android:layout_width="match_parent" android:layout_height="match_parent" didi:header_id="@+id/view_bg" didi:target_id="@+id/target_layout" didi:inn_id="@+id/inner_rv" didi:header_init_top="0" didi:target_init_bottom="250"> <TextView android:id="@+id/view_bg" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textColor="#f00" android:textSize="25sp" android:textAllCaps="false" android:text="@string/txt_nested_scroll_bg" /> <LinearLayout android:id="@+id/target_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#fff"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/inner_rv" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </com.blog.demo10.NestedViewGroup> <androidx.appcompat.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" didi:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> </RelativeLayout>
最新发布
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值