Skip to content

Commit e797f9e

Browse files
committed
Initial commit.
0 parents  commit e797f9e

45 files changed

Lines changed: 1701 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.idea

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2016 Patrick Löwenstein
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# NavigationViewFragmentAdapters
2+
3+
[ ![Download](https://api.bintray.com/packages/patloew/maven/NavigationViewFragmentAdapters/images/download.svg) ](https://bintray.com/patloew/maven/NavigationViewFragmentAdapters/_latestVersion) [![API](https://img.shields.io/badge/API-9%2B-brightgreen.svg?style=flat)](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.

build.gradle

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
repositories {
5+
jcenter()
6+
}
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:2.1.2'
9+
10+
classpath 'me.tatarka:gradle-retrolambda:3.3.0-beta4'
11+
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
12+
13+
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
14+
15+
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
16+
classpath "com.github.dcendents:android-maven-gradle-plugin:1.3"
17+
18+
19+
// NOTE: Do not place your application dependencies here; they belong
20+
// in the individual module build.gradle files
21+
}
22+
23+
configurations.classpath.exclude group: 'com.android.tools.external.lombok'
24+
}
25+
26+
allprojects {
27+
repositories {
28+
jcenter()
29+
}
30+
}
31+
32+
task clean(type: Delete) {
33+
delete rootProject.buildDir
34+
}

gradle.properties

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Project-wide Gradle settings.
2+
3+
# IDE (e.g. Android Studio) users:
4+
# Gradle settings configured through the IDE *will override*
5+
# any settings specified in this file.
6+
7+
# For more details on how to configure your build environment visit
8+
# http://www.gradle.org/docs/current/userguide/build_environment.html
9+
10+
# Specifies the JVM arguments used for the daemon process.
11+
# The setting is particularly useful for tweaking memory settings.
12+
# Default value: -Xmx10248m -XX:MaxPermSize=256m
13+
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14+
org.gradle.jvmargs=-Xmx2048M
15+
16+
# When configured, Gradle will run in incubating parallel mode.
17+
# This option should only be used with decoupled projects. More details, visit
18+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
19+
# org.gradle.parallel=true

gradle/wrapper/gradle-wrapper.jar

52.4 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Mon Dec 28 10:00:20 PST 2015
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip

gradlew

Lines changed: 160 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)