|
| 1 | +# NavigationViewFragmentAdapters |
| 2 | + |
| 3 | + [  ](https://bintray.com/patloew/maven/NavigationViewFragmentAdapters/_latestVersion) [](https://android-arsenal.com/api?level=9) |
| 4 | + |
| 5 | +A small library containing two adapters which allow for easy fragment management with a NavigationView. |
| 6 | + |
| 7 | +The library handles replacing the fragments and saving/restoring the fragment state, including showing the right fragment when opening an app again after it was killed. |
| 8 | + |
| 9 | +# Usage |
| 10 | + |
| 11 | +First, decide which adapter suits your use case better. |
| 12 | + |
| 13 | +The `NavigationViewFragmentAdapter` detaches fragments after another menu item is selected. This means that each Fragment for a menu item is kept in memory, but there is no overhead for recreating when it is attached again. |
| 14 | + |
| 15 | +The `NavigationViewStateFragmentAdapter` saves the state and removes the fragment after another menu item is selected. This means that only the fragment for the current menu item is kept in memory, but there is overhead for recreating other fragments when they are selected again. |
| 16 | + |
| 17 | +After you decided, create your adapter implementation: |
| 18 | + |
| 19 | + private class MyNavigationViewAdapter extends NavigationViewFragmentAdapter { |
| 20 | + |
| 21 | + public MyDrawerAdapter(FragmentManager fragmentManager, @IdRes int containerId, @IdRes int defaultMenuItemId, Bundle savedInstanceState) { |
| 22 | + super(fragmentManager, containerId, defaultMenuItemId, savedInstanceState); |
| 23 | + } |
| 24 | + |
| 25 | + @NonNull |
| 26 | + @Override |
| 27 | + public Fragment getFragment(@IdRes int menuItemId) { |
| 28 | + switch (menuItemId) { |
| 29 | + case R.id.navitem_1: |
| 30 | + return SampleFragment.newInstance("Fragment 1"); |
| 31 | + case R.id.navitem_2: |
| 32 | + return SampleFragment.newInstance("Fragment 2"); |
| 33 | + case R.id.navitem_3: |
| 34 | + return SampleFragment.newInstance("Fragment 3"); |
| 35 | + default: |
| 36 | + return SettingsFragment.newInstance(); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | +Now, create an instance and attach it to your NavigationView in your Activity `onCreate()`: |
| 42 | + |
| 43 | + myDrawerAdapter = new MyDrawerAdapter(getSupportFragmentManager(), R.id.container, R.id.navitem_1, savedInstanceState); |
| 44 | + myDrawerAdapter.attachTo(navigationView); |
| 45 | + |
| 46 | +Also, don't forget to call `myDrawerAdapter.onSaveInstanceState()` in your Activity `onSaveInstanceState()`. |
| 47 | + |
| 48 | +Now you have your navigation drawer up and running, including state saving of the fragments. |
| 49 | + |
| 50 | +# Advanced Usage |
| 51 | + |
| 52 | +## Back Stack |
| 53 | + |
| 54 | +There are cases when some menu items (e.g. settings) should not simply replace the current fragment, but also be added to the back stack. To do this, override `shouldAddToBackStack()` in your adapter: |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean shouldAddToBackStack(@IdRes int menuItemId) { |
| 58 | + return menuItemId == R.id.navitem_settings; |
| 59 | + } |
| 60 | + |
| 61 | +## Animations |
| 62 | + |
| 63 | +You can provide custom animations for your fragment transactions via `setCustomAnimations()`. Separate animations can be set for when you add the transaction to the back stack via `setBackStackCustomAnimations()`. |
| 64 | + |
| 65 | +## OnNavigationItemSelectedListener |
| 66 | + |
| 67 | +An additional `OnNavigationItemSelectedListener` can be provided via `setNavigationItemSelectedListener()`. This can be used to add behavior after the fragment transaction was commited. If you just want to close the drawer, the lib includes a `CloseDrawerNavigationItemSelectedListener`. |
| 68 | + |
| 69 | +# Sample |
| 70 | + |
| 71 | +A basic sample app with example Activities for both adapters is available in the `sample` project. |
| 72 | + |
| 73 | +# Setup |
| 74 | + |
| 75 | +Add the following to your `build.gradle`: |
| 76 | + |
| 77 | + repositories { |
| 78 | + maven { url 'https://dl.bintray.com/patloew/maven' } |
| 79 | + } |
| 80 | + |
| 81 | + dependencies { |
| 82 | + compile 'com.patloew.navigationviewfragmentadapters:adapters:0.1.1' |
| 83 | + } |
| 84 | + |
| 85 | +# License |
| 86 | + |
| 87 | + Copyright 2016 Patrick Löwenstein |
| 88 | + |
| 89 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 90 | + you may not use this file except in compliance with the License. |
| 91 | + You may obtain a copy of the License at |
| 92 | + |
| 93 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 94 | + |
| 95 | + Unless required by applicable law or agreed to in writing, software |
| 96 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 97 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 98 | + See the License for the specific language governing permissions and |
| 99 | + limitations under the License. |
0 commit comments