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

昨天想到了NavigationDrawer中Item點擊的問題。html

點擊Drawer中的一個Item須要到一個新的頁面,你是應該打開一個新的Activity呢仍是直接用fragment呢?android

若是打開新的activity,那麼新的Activity覆蓋,Drawer按鈕就會消失;固然你也能夠在新的Activity上加一樣的NavigationDrawer,可是那樣太浪費資源了。app

但若是你用fragment,那麼你只須要ToolBar下面的區域,整個做爲一個可替換的container,而後不一樣的fragment進行replace就好了。不少App都是這樣作的,好比Google+,好比知乎等等,這樣他們在切換左側的Drawer Item的時候,只須要replace container(不要忘了把fragment入棧)。ide

 

但是,若是你的App有一個ViewPager(FragmentPagerAdapter)呢?佈局

一般狀況,AppBarLayout會包含widget.Toolbar和TabLayout。code

其實也是能夠的,只須要把ToolBar下面的區域抽離出來(這樣咱們就必須把TabLayout從AppBarLayout裏面抽出來),做爲fragment的container就好了,而後在MainActivity啓動的時候啓動一個MainFragment,裏面包含:xml

  • tabLayout
  • viewPager

惟獨有一個問題:若是你的應用想要實現ToolBar的滾動時摺疊,就像這樣:htm

這樣若是僅僅用一個Activity+多個Fragment,是不行的 。下面說一下爲何。blog

用一個Activity+多個Fragment意味着要有給fragments一個公共的container。ci

個人作法是,把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(起初我還覺得是酷安的開發在偷懶-_-)。因此我仍是決定採用相似酷安的方案了。

啊 浪費了好多時間。

 

5月1日於無線樓

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息