Is it possible to use a navigation drawer in android but instead of updating fragments, i would like to switch between activities as my means of navigation within the app.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
The Fragment manager can be replaced as mentioned in the post:
https://guides.codepath.com/android/fragment-navigation-drawer#alternative-to-fragments
You can inflate a layout instead of using a fragment manager.
You need a
BaseDrawerActivity
which implement the Navigation Drawer then extend theBaseDrawerActivity
in each activity you need Navigation Drawer.First create
BaseDrawerActivity.java
:then create
activity_base_drawer.xml
inres/layout
folder:where
@layout/app_bar_home
is:Next you enter your Activities that will have Navigation Drawer such as
CameraActivity.java
:Where
R.layout.activity_camera
is your layout forCameraActivity.java
.Then create other Activity like
GalleryActivity.java
and so on that will have Navigation Drawer:As a little improvement to the solution pointed by @David-Crozier, in order to avoid the overlap of both animations (closing the NavigationDrawer and starting a new activity), you can include a little delay in your method as was done in the iosched app v2014:
Here the link for reference: https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/BaseActivity.java
Yes it is possible - it's what I did for my app. I already had a number of activities set up, and rather than convert them all to fragments, I wanted to tailor the navigation drawer to work across all of them. Unfortunately, it's not a quick workaround, so if you have the option of using fragments, I would go with that. But regardless here's how I did it:
Let's say I have 2 activities, both of which I want to have the Navigation Drawer. In the layout.xml for each, I specified a
DrawerLayout
with the appropriateListView
to hold my navigation options. Essentially, the Navigation drawer is made every time I switch between activities, giving the appearance that it is persisting. To make life a lot easier, I took the common methods required to set up the navigation drawer and put them in their own class:NavigationDrawerSetup.java
. That way my activities can use the same custom adapter, etc.Within this
NavigationDrawerSetup.java
class, I have the following:configureDrawer()
- this sets up theActionBar
,ActionBarDrawerToggle
, and the required listenersselectOptions()
method, which handles drawer item clicksWhen you set up the navigation drawer within one of your activities, you just create a new
NavigationDrawerSetup
object and pass in the required layout parameters (like theDrawerLayout
,ListView
etc). Then you'd callconfigureDrawer()
:currentActivity
is passed in since the navigation drawer is tied to the activity you are on. You will have to use it when you set up theActionBarDrawerToggle
:You will also need to use
currentActivity
when setting up your customAdapter
:As for how to switch between activities via the navigation drawer, you can just set up new intents within your selectItem() method:
Just make sure that your new
Activity
also has the navigation drawer setup and it should display.There are a ton of things you can do to customize this method to your own needs, but this is the general structure of how I did it. Hope this helps!