標籤:
昨天想到了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)呢?其實也是可以的,只需要把ToolBar下面的地區抽離出來,作為fragment的container就行了,然後在MainActivity啟動的時候啟動一個MainFragment,裡麵包含:
tabLayout
viewPager
。
唯專屬一個問題:如果你的應用想要實現ToolBar的動態摺疊,就像這樣:
我把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.
尷尬的app:layout_scrollFlags="scroll|enterAlways" 配合Drawer