Add Android Fragment Rigger

0
603
Android Fragment Rigger
Android Fragment Rigger

Recently we discuss about Camera Fragment Library which can control all your need on Camera but today we are going to see the Android Fragment Rigger which is the library to manage fragments at the least cost of use.
No need to extend any class!!! No need to extend any class!!! No need to extend any class!!! the most important thing must be said for three times!!!
You just only need cost one line annotation code when you are using FragmentRigger.

Principle of library is define the pointcuts for Fragment/Activity lifecycle methods and bind to the proxy class to execute.

Feature of Android Fragment Rigger

  • Powerful api
  • Enough English notes
  • Strictest exceptions
  • Resolve usual exceptions and bugs in fragments
  • Never lost any fragment transaction commit
  • Extend the android native fragment methods,add some usual methods such as onBackPressed()
  • Print tree for the fragment stack
  • Fragment lazy load
  • Fragment transition animations
  • Fragment shared elements transition animations

See this : Top 35 Android Loading Animation examples with source code

1Support

  • FragmentRigger only supports android.support.v4.app.Fragment and android.support.v4.app.FragmentActivity.This library is not supported as your Fragment/Activity is not extend those classes.
  • FragmentRigger support SDK12+.
  • FragmentRigger support Java language for now. Kylin will be supported in future.

2Installation

This library is powered by AspectJ,you must config the AspectJ library if you wanna to use this library.

Add to build.gradle in root project

buildscript {
    dependencies {
        ...
        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:1.0.10'
    }
}
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add to build.gradle in application module

apply plugin: 'android-aspectjx'
android{
  ...
}

Add to build.gradle in library module

compile 'com.justkiddingbaby:fragment-rigger:1.0.0'

 

3How to use?

FragmentRigger does not need extend any class,all operation is depend on the proxy class Rigger to manage fragments.

Add Fragment/Activity support

Add @Puppet annotation on your Fragment/Activity,but your fragment must is the child class of android.support.v4.app.Fragment and your activity must is the child class of android.support.v4.app.FragmentActivity.

@Puppet
public class ExampleFragment extends Fragment
@Puppet(containerViewId = R.id.atyContent)
public class ExampleActivity extends AppCompatActivity

You can use this library on the class that is added @Puppet annotation

@Puppet

If you wanna to use this library,@puppet is necessary condition.
But this annotation has some params you need to know.

You must see this : How to Add Wave Sidebar Animation Android?

containerViewId

Optional identifier of the container this fragment is to be placed in. If 0, it will not be placed in a container. default value is 0.

This params will be used in method Rigger#startFragment

bondContainerView

bondContainerView is a boolean object.

  • if the value is true: the puppet will be closed as the top fragment in puppet’s stack is closing. the top fragment in stack will not perform transition animation.
  • if the valye is false: the puppet will do nothing as the top fragment in puppet’s stack is closing. the top fragment in stack will perform transition animation and closing.

This params will be used as the host Activity#onBackPressed is called.

4Fragment usage

Android native fragment operate a series of methods to manage fragment, such as add/show/replace/remove, and native Fragment provide FragmentManager and FragmenmtTransaction to let us use fragments. But we often encounter all kinds of problems when we are using FragmentTransactionThis library can make you use fragment easier.

replace method(ReplaceFragment.java

replace method is actually add + show method, you can make sure one container only contain one Fragment when you are using this method.
This method might is the easist method to use fragment, and it is probably one of the easiest ways to get wrong.

Rigger.getRigger(this).replaceFragment(fragment, R.id.fr_content);

Rigger.getRigger(params),the params is the Activity/Fragmentclass marked by @Puppet.
replaceFragment(@NonNull Fragment fragment, @IdRes int containerViewId)has two parameters, the first parameter is the fragment to be replaced, the second parameter is the container view id for the fragment to be placed in.
When you are using this method, the fragment is placed in containerView will be removed, and the fragment to be replaced will be called add and show transaction.

show method(ShowFragment.java

show method has multiple methods, you can add multiple fragments or use fragment by tag use this library.

Type one: Add fragments first and show by fragment object

Fragment fragments[] = new Fragment[4];
Rigger.getRigger(this).addFragment(containerViewId, fragments);
Rigger.getRigger(this).show(fragments[0]);

Type two: Add fragments first and show by fragment tag

Fragment fragments[] = new Fragment[4];
Rigger.getRigger(this).addFragment(containerViewId, fragments);
String tag = Rigger.getRigger(fragment[0]).getFragmentTag();
Rigger.getRigger(this).show(tag);

Type three: Add single fragment and show

Rigger.getRigger(this).addFragment(fragment, containerViewId);

The fragments placed in container view will be hidden but not remove, you can show a fragment by fragment object or fragment tag.

Remove from stack

This library provide support for Fragment remove from stack, the default operation is the onBackPressed method is called, beside, you can use close() method to remove a fragment from stack.

The default operation is remove the pop fragment in stack and show the next pop fragment.

Rigger.getRigger(this).close();

Fragment is a part of view for the host, so we need choose if the host should be closed as the stack is only contain one fragment, now,the second parameter bondContainerView in @Puppetannotation will be used.

  • bondContainerView = true : the host that contain only one fragment in stack will be finishor remove from stack as onBackPressed() method is called, and the last pop fragment in stack does not perform the transition animation.
  • bondContainerView = false: the host that stack is empty will finish or remove from stack and all fragments in stack will perform the transition animation.

5Download this project

 

Please comment us and share with your friends !!

Share your thoughts

Loading Facebook Comments ...
Loading Disqus Comments ...