Skip to content

Commit

Permalink
Add support to nested fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
t0rr3sp3dr0 committed Apr 14, 2017
1 parent cd485e9 commit f5ef542
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'

compile 'com.android.support:cardview-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta5'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'

compile project(':library')
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.3.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Fri Apr 14 17:41:16 BRT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})

compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
testCompile 'junit:junit:4.12'

compile 'com.google.guava:guava:20.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -40,8 +43,17 @@
@SuppressWarnings({"unused", "WeakerAccess"})
public abstract class CandyFragment<T extends ViewDataBinding> extends Fragment {
private final Map<String, Object> map = new HashMap<>();
protected CandyActivity<? extends ViewDataBinding> mListener;
private T binding;
@AnimRes
private int enter = R.anim.slide_in_right;
@AnimRes
private int exit = R.anim.slide_out_left;
@AnimRes
private int popEnter = R.anim.slide_in_left;
@AnimRes
private int popExit = R.anim.slide_out_right;
protected CandyActivity<? extends ViewDataBinding> mListener;
public FragmentManager childFragmentManager;

/**
* Mandatory empty constructor for the fragment manager to instantiate the
Expand Down Expand Up @@ -137,6 +149,8 @@ public void onAttach(Context context) {
mListener = (CandyActivity<? extends ViewDataBinding>) context;
else
throw new RuntimeException(context.toString() + " must extend CandyFragment");

childFragmentManager = getChildFragmentManager();
}

/**
Expand Down Expand Up @@ -223,4 +237,62 @@ public final void replaceFragment(@IdRes int containerViewId, Fragment fragment,
public final void restartFragment(@IdRes int containerViewId, boolean animated) {
((CandyActivity) getActivity()).restartFragment(containerViewId, animated);
}

public final void setChildCustomAnimations(@AnimRes int enter, @AnimRes int exit, @AnimRes int popEnter, @AnimRes int popExit) {
this.enter = enter;
this.exit = exit;
this.popEnter = popEnter;
this.popExit = popExit;
}

public final void addChildFragment(@IdRes int containerViewId, Fragment fragment, boolean animated) {
FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
if (animated)
fragmentTransaction.setCustomAnimations(enter, exit, popEnter, popExit);
fragmentTransaction.add(containerViewId, fragment);
fragmentTransaction.commit();
}

public final void replaceChildFragment(@IdRes int containerViewId, Fragment fragment, boolean toBackStack, boolean animated) {
FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
if (toBackStack)
fragmentTransaction.addToBackStack(Integer.toString(System.identityHashCode(this)));
else
childFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
if (animated)
fragmentTransaction.setCustomAnimations(enter, exit, popEnter, popExit);
fragmentTransaction.replace(containerViewId, fragment);
fragmentTransaction.commit();
}

@SuppressWarnings("TryWithIdenticalCatches")
public final void restartChildFragment(@IdRes int containerViewId, boolean animated) {
try {
Fragment actualFragment = childFragmentManager.findFragmentById(containerViewId);

// Clone actualFragment
Fragment cloneFragment = actualFragment.getClass().newInstance();
Bundle args = new Bundle();
actualFragment.onSaveInstanceState(args);
cloneFragment.setArguments(args);

FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
if (animated)
fragmentTransaction.setCustomAnimations(enter, exit, popEnter, popExit);
fragmentTransaction.replace(containerViewId, cloneFragment);
if (childFragmentManager.getBackStackEntryCount() > 0) {
childFragmentManager.popBackStack();
fragmentTransaction.addToBackStack(Integer.toString(System.identityHashCode(this)));
}
fragmentTransaction.commitAllowingStateLoss();
} catch (java.lang.InstantiationException | NullPointerException e) {
e.printStackTrace();

Log.e("Confectionery", "It was not possible to restart your fragment!", e);
} catch (IllegalAccessException e) {
e.printStackTrace();

Log.e("Confectionery", "It was not possible to restart your fragment!", e);
}
}
}

0 comments on commit f5ef542

Please sign in to comment.