diff --git a/lib_base/src/main/java/com/android/base/adapter/pager/ViewPageFragmentAdapter.java b/lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerFragmentAdapter.java similarity index 59% rename from lib_base/src/main/java/com/android/base/adapter/pager/ViewPageFragmentAdapter.java rename to lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerFragmentAdapter.java index f6d0f06..7dfb8ff 100644 --- a/lib_base/src/main/java/com/android/base/adapter/pager/ViewPageFragmentAdapter.java +++ b/lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerFragmentAdapter.java @@ -10,20 +10,20 @@ import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; @SuppressWarnings("unused") -public class ViewPageFragmentAdapter extends FragmentPagerAdapter { +public class ViewPagerFragmentAdapter extends FragmentPagerAdapter { - private final List mTabs; + private final List mTabs; private Context mContext; - public ViewPageFragmentAdapter(FragmentManager fragmentManager, Context context) { + public ViewPagerFragmentAdapter(FragmentManager fragmentManager, Context context) { super(fragmentManager); mContext = context; mTabs = new ArrayList<>(); } - public void setDataSource(List viewPageInfoList) { + public void setDataSource(List viewPagerInfoList) { mTabs.clear(); - mTabs.addAll(viewPageInfoList); + mTabs.addAll(viewPagerInfoList); } @Override @@ -33,8 +33,8 @@ public class ViewPageFragmentAdapter extends FragmentPagerAdapter { @Override public Fragment getItem(int position) { - ViewPageInfo viewPageInfo = mTabs.get(position); - return Fragment.instantiate(mContext, viewPageInfo.clazz.getName(), viewPageInfo.args); + ViewPagerInfo viewPagerInfo = mTabs.get(position); + return Fragment.instantiate(mContext, viewPagerInfo.clazz.getName(), viewPagerInfo.args); } @Override @@ -42,7 +42,7 @@ public class ViewPageFragmentAdapter extends FragmentPagerAdapter { return mTabs.get(position).title; } - public List getTabs() { + public List getTabs() { return mTabs; } } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/adapter/pager/ViewPageInfo.java b/lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerInfo.java similarity index 74% rename from lib_base/src/main/java/com/android/base/adapter/pager/ViewPageInfo.java rename to lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerInfo.java index f3f1675..2e8f2dc 100644 --- a/lib_base/src/main/java/com/android/base/adapter/pager/ViewPageInfo.java +++ b/lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerInfo.java @@ -5,13 +5,13 @@ import android.os.Bundle; import androidx.fragment.app.Fragment; @SuppressWarnings("all") -public class ViewPageInfo { +public class ViewPagerInfo { public final Class clazz; public final Bundle args; public final String title; - public ViewPageInfo(String title, Class clazz, Bundle args) { + public ViewPagerInfo(String title, Class clazz, Bundle args) { this.title = title; this.clazz = clazz; this.args = args; diff --git a/lib_base/src/main/java/com/android/base/adapter/pager/ViewPageStateFragmentAdapter.java b/lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerStateFragmentAdapter.java similarity index 59% rename from lib_base/src/main/java/com/android/base/adapter/pager/ViewPageStateFragmentAdapter.java rename to lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerStateFragmentAdapter.java index 8f6dd19..f62ca79 100644 --- a/lib_base/src/main/java/com/android/base/adapter/pager/ViewPageStateFragmentAdapter.java +++ b/lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerStateFragmentAdapter.java @@ -11,20 +11,20 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentStatePagerAdapter; -public class ViewPageStateFragmentAdapter extends FragmentStatePagerAdapter { +public class ViewPagerStateFragmentAdapter extends FragmentStatePagerAdapter { - private final List mTabs; + private final List mTabs; private Context mContext; - public ViewPageStateFragmentAdapter(FragmentManager fragmentManager, Context context) { + public ViewPagerStateFragmentAdapter(FragmentManager fragmentManager, Context context) { super(fragmentManager); mContext = context; mTabs = new ArrayList<>(); } - public void setDataSource(List viewPageInfoList) { + public void setDataSource(List viewPagerInfoList) { mTabs.clear(); - mTabs.addAll(viewPageInfoList); + mTabs.addAll(viewPagerInfoList); } @Override @@ -35,8 +35,8 @@ public class ViewPageStateFragmentAdapter extends FragmentStatePagerAdapter { @NotNull @Override public Fragment getItem(int position) { - ViewPageInfo viewPageInfo = mTabs.get(position); - return Fragment.instantiate(mContext, viewPageInfo.clazz.getName(), viewPageInfo.args); + ViewPagerInfo viewPagerInfo = mTabs.get(position); + return Fragment.instantiate(mContext, viewPagerInfo.clazz.getName(), viewPagerInfo.args); } @Override @@ -44,7 +44,7 @@ public class ViewPageStateFragmentAdapter extends FragmentStatePagerAdapter { return mTabs.get(position).title; } - protected List getTabs() { + protected List getTabs() { return mTabs; } diff --git a/lib_base/src/main/java/com/android/base/app/Sword.java b/lib_base/src/main/java/com/android/base/app/Sword.java index 5ebaae5..a144d95 100644 --- a/lib_base/src/main/java/com/android/base/app/Sword.java +++ b/lib_base/src/main/java/com/android/base/app/Sword.java @@ -6,6 +6,7 @@ import android.content.Context; import android.os.Bundle; import com.android.base.app.dagger.Injectable; +import com.android.base.app.fragment.FragmentAnimator; import com.android.base.app.fragment.FragmentConfig; import com.android.base.app.fragment.LoadingViewFactory; import com.android.base.app.ui.PageNumber; @@ -118,6 +119,11 @@ public final class Sword { return this; } + public Sword setDefaultFragmentAnimator(FragmentAnimator animator) { + FragmentConfig.setDefaultFragmentAnimator(animator); + return this; + } + public Sword enableAutoInject() { Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacksAdapter() { diff --git a/lib_base/src/main/java/com/android/base/app/fragment/AnimatorHelper.java b/lib_base/src/main/java/com/android/base/app/fragment/AnimatorHelper.java new file mode 100644 index 0000000..26065ca --- /dev/null +++ b/lib_base/src/main/java/com/android/base/app/fragment/AnimatorHelper.java @@ -0,0 +1,102 @@ +package com.android.base.app.fragment; + +import android.content.Context; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; + +import com.android.base.R; + +import androidx.fragment.app.FragmentTransaction; + +public final class AnimatorHelper { + + private Animation noneAnim, noneAnimFixed; + private Animation enterAnim, exitAnim, popEnterAnim, popExitAnim; + + private Context context; + private FragmentAnimator fragmentAnimator; + + AnimatorHelper(Context context, FragmentAnimator fragmentAnimator) { + this.context = context; + notifyChanged(fragmentAnimator); + } + + void notifyChanged(FragmentAnimator fragmentAnimator) { + this.fragmentAnimator = fragmentAnimator; + if (fragmentAnimator != null) { + initEnterAnim(); + initExitAnim(); + initPopEnterAnim(); + initPopExitAnim(); + } + } + + public Animation getNoneAnim() { + if (noneAnim == null) { + noneAnim = AnimationUtils.loadAnimation(context, R.anim.no_anim); + } + return noneAnim; + } + + public Animation getNoneAnimFixed() { + if (noneAnimFixed == null) { + noneAnimFixed = new Animation() { + }; + } + return noneAnimFixed; + } + + private Animation initEnterAnim() { + if (fragmentAnimator.getEnter() == 0) { + enterAnim = AnimationUtils.loadAnimation(context, R.anim.no_anim); + } else { + enterAnim = AnimationUtils.loadAnimation(context, fragmentAnimator.getEnter()); + } + return enterAnim; + } + + private Animation initExitAnim() { + if (fragmentAnimator.getExit() == 0) { + exitAnim = AnimationUtils.loadAnimation(context, R.anim.no_anim); + } else { + exitAnim = AnimationUtils.loadAnimation(context, fragmentAnimator.getExit()); + } + return exitAnim; + } + + private Animation initPopEnterAnim() { + if (fragmentAnimator.getPopEnter() == 0) { + popEnterAnim = AnimationUtils.loadAnimation(context, R.anim.no_anim); + } else { + popEnterAnim = AnimationUtils.loadAnimation(context, fragmentAnimator.getPopEnter()); + } + return popEnterAnim; + } + + private Animation initPopExitAnim() { + if (fragmentAnimator.getPopExit() == 0) { + popExitAnim = AnimationUtils.loadAnimation(context, R.anim.no_anim); + } else { + popExitAnim = AnimationUtils.loadAnimation(context, fragmentAnimator.getPopExit()); + } + return popExitAnim; + } + + Animation onCreateAnimation(BaseFragment baseFragment, int transit, boolean enter, int nextAnim) { + if (transit == FragmentTransaction.TRANSIT_FRAGMENT_OPEN) { + if (enter) { + return enterAnim; + } else { + return popExitAnim; + } + } else if (transit == FragmentTransaction.TRANSIT_FRAGMENT_CLOSE) { + if (enter) { + return popEnterAnim; + } else { + return exitAnim; + } + } + return null; + } + +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.java b/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.java index da41900..71392c2 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.java +++ b/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.java @@ -7,6 +7,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; +import android.view.animation.Animation; import com.android.base.app.Sword; import com.android.base.app.activity.BackHandlerHelper; @@ -45,6 +46,8 @@ public class BaseFragment extends Fragment implements LoadingView, OnBackPressLi /* just for cache*/ private View mCachedView; + private AnimatorHelper mAnimatorHelper; + private final FragmentDelegates mFragmentDelegates = new FragmentDelegates(this); private String tag() { @@ -295,4 +298,13 @@ public class BaseFragment extends Fragment implements LoadingView, OnBackPressLi getLoadingViewImpl().showMessage(messageId); } + @Nullable + @Override + public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { + if (mAnimatorHelper == null) { + mAnimatorHelper = new AnimatorHelper(getContext(), FragmentConfig.defaultFragmentAnimator()); + } + return mAnimatorHelper.onCreateAnimation(this, transit, enter, nextAnim); + } + } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/DefaultHorizontalAnimator.java b/lib_base/src/main/java/com/android/base/app/fragment/DefaultHorizontalAnimator.java new file mode 100644 index 0000000..241a787 --- /dev/null +++ b/lib_base/src/main/java/com/android/base/app/fragment/DefaultHorizontalAnimator.java @@ -0,0 +1,44 @@ +package com.android.base.app.fragment; + +import android.os.Parcel; +import android.os.Parcelable; + +import com.android.base.R; + + +public class DefaultHorizontalAnimator extends FragmentAnimator implements Parcelable { + + public DefaultHorizontalAnimator() { + enter = R.anim.h_fragment_enter; + exit = R.anim.h_fragment_exit; + popEnter = R.anim.h_fragment_pop_enter; + popExit = R.anim.h_fragment_pop_exit; + } + + protected DefaultHorizontalAnimator(Parcel in) { + super(in); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + } + + @Override + public int describeContents() { + return 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public DefaultHorizontalAnimator createFromParcel(Parcel in) { + return new DefaultHorizontalAnimator(in); + } + + @Override + public DefaultHorizontalAnimator[] newArray(int size) { + return new DefaultHorizontalAnimator[size]; + } + }; + +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/DefaultNoAnimator.java b/lib_base/src/main/java/com/android/base/app/fragment/DefaultNoAnimator.java new file mode 100644 index 0000000..645e04d --- /dev/null +++ b/lib_base/src/main/java/com/android/base/app/fragment/DefaultNoAnimator.java @@ -0,0 +1,42 @@ +package com.android.base.app.fragment; + + +import android.os.Parcel; +import android.os.Parcelable; + +public class DefaultNoAnimator extends FragmentAnimator implements Parcelable { + + public DefaultNoAnimator() { + enter = 0; + exit = 0; + popEnter = 0; + popExit = 0; + } + + protected DefaultNoAnimator(Parcel in) { + super(in); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + } + + @Override + public int describeContents() { + return 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public DefaultNoAnimator createFromParcel(Parcel in) { + return new DefaultNoAnimator(in); + } + + @Override + public DefaultNoAnimator[] newArray(int size) { + return new DefaultNoAnimator[size]; + } + }; + +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/DefaultVerticalAnimator.java b/lib_base/src/main/java/com/android/base/app/fragment/DefaultVerticalAnimator.java new file mode 100644 index 0000000..868cea9 --- /dev/null +++ b/lib_base/src/main/java/com/android/base/app/fragment/DefaultVerticalAnimator.java @@ -0,0 +1,43 @@ +package com.android.base.app.fragment; + +import android.os.Parcel; +import android.os.Parcelable; + +import com.android.base.R; + +public class DefaultVerticalAnimator extends FragmentAnimator implements Parcelable { + + public DefaultVerticalAnimator() { + enter = R.anim.v_fragment_enter; + exit = R.anim.v_fragment_exit; + popEnter = R.anim.v_fragment_pop_enter; + popExit = R.anim.v_fragment_pop_exit; + } + + protected DefaultVerticalAnimator(Parcel in) { + super(in); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + } + + @Override + public int describeContents() { + return 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public DefaultVerticalAnimator createFromParcel(Parcel in) { + return new DefaultVerticalAnimator(in); + } + + @Override + public DefaultVerticalAnimator[] newArray(int size) { + return new DefaultVerticalAnimator[size]; + } + }; + +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/FragmentAnimator.java b/lib_base/src/main/java/com/android/base/app/fragment/FragmentAnimator.java new file mode 100644 index 0000000..de93ae5 --- /dev/null +++ b/lib_base/src/main/java/com/android/base/app/fragment/FragmentAnimator.java @@ -0,0 +1,105 @@ +package com.android.base.app.fragment; + +import android.os.Parcel; +import android.os.Parcelable; + +import androidx.annotation.AnimRes; + +public class FragmentAnimator implements Parcelable { + + @AnimRes protected int enter; + @AnimRes protected int exit; + @AnimRes protected int popEnter; + @AnimRes protected int popExit; + + public FragmentAnimator() { + } + + public FragmentAnimator(int enter, int exit) { + this.enter = enter; + this.exit = exit; + } + + public FragmentAnimator(int enter, int exit, int popEnter, int popExit) { + this.enter = enter; + this.exit = exit; + this.popEnter = popEnter; + this.popExit = popExit; + } + + public FragmentAnimator copy() { + return new FragmentAnimator(getEnter(), getExit(), getPopEnter(), getPopExit()); + } + + protected FragmentAnimator(Parcel in) { + enter = in.readInt(); + exit = in.readInt(); + popEnter = in.readInt(); + popExit = in.readInt(); + } + + public static final Creator CREATOR = new Creator() { + @Override + public FragmentAnimator createFromParcel(Parcel in) { + return new FragmentAnimator(in); + } + + @Override + public FragmentAnimator[] newArray(int size) { + return new FragmentAnimator[size]; + } + }; + + public int getEnter() { + return enter; + } + + public FragmentAnimator setEnter(int enter) { + this.enter = enter; + return this; + } + + public int getExit() { + return exit; + } + + /** + * enter animation + */ + public FragmentAnimator setExit(int exit) { + this.exit = exit; + return this; + } + + public int getPopEnter() { + return popEnter; + } + + public FragmentAnimator setPopEnter(int popEnter) { + this.popEnter = popEnter; + return this; + } + + public int getPopExit() { + return popExit; + } + + public FragmentAnimator setPopExit(int popExit) { + this.popExit = popExit; + return this; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(enter); + dest.writeInt(exit); + dest.writeInt(popEnter); + dest.writeInt(popExit); + } + +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/FragmentConfig.java b/lib_base/src/main/java/com/android/base/app/fragment/FragmentConfig.java index 5ccac3c..6aa7f96 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/FragmentConfig.java +++ b/lib_base/src/main/java/com/android/base/app/fragment/FragmentConfig.java @@ -9,6 +9,7 @@ public class FragmentConfig { private static final int INVALIDATE_ID = -1; private static int sDefaultContainerId = INVALIDATE_ID; + private static FragmentAnimator sFragmentAnimator = null; public static void setDefaultContainerId(int defaultContainerId) { sDefaultContainerId = defaultContainerId; @@ -21,4 +22,12 @@ public class FragmentConfig { return sDefaultContainerId; } -} + public static void setDefaultFragmentAnimator(FragmentAnimator animator) { + sFragmentAnimator = animator; + } + + static FragmentAnimator defaultFragmentAnimator() { + return sFragmentAnimator; + } + +} \ No newline at end of file diff --git a/lib_base/src/main/res/anim/h_fragment_enter.xml b/lib_base/src/main/res/anim/h_fragment_enter.xml new file mode 100644 index 0000000..5f704c0 --- /dev/null +++ b/lib_base/src/main/res/anim/h_fragment_enter.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/lib_base/src/main/res/anim/h_fragment_exit.xml b/lib_base/src/main/res/anim/h_fragment_exit.xml new file mode 100644 index 0000000..d18e69e --- /dev/null +++ b/lib_base/src/main/res/anim/h_fragment_exit.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/lib_base/src/main/res/anim/h_fragment_pop_enter.xml b/lib_base/src/main/res/anim/h_fragment_pop_enter.xml new file mode 100644 index 0000000..1c1a691 --- /dev/null +++ b/lib_base/src/main/res/anim/h_fragment_pop_enter.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/lib_base/src/main/res/anim/h_fragment_pop_exit.xml b/lib_base/src/main/res/anim/h_fragment_pop_exit.xml new file mode 100644 index 0000000..1dd1579 --- /dev/null +++ b/lib_base/src/main/res/anim/h_fragment_pop_exit.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/lib_base/src/main/res/anim/no_anim.xml b/lib_base/src/main/res/anim/no_anim.xml new file mode 100644 index 0000000..42a38a5 --- /dev/null +++ b/lib_base/src/main/res/anim/no_anim.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/lib_base/src/main/res/anim/pop_exit_no_anim.xml b/lib_base/src/main/res/anim/pop_exit_no_anim.xml new file mode 100644 index 0000000..208938a --- /dev/null +++ b/lib_base/src/main/res/anim/pop_exit_no_anim.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/lib_base/src/main/res/anim/v_fragment_enter.xml b/lib_base/src/main/res/anim/v_fragment_enter.xml new file mode 100644 index 0000000..7d75613 --- /dev/null +++ b/lib_base/src/main/res/anim/v_fragment_enter.xml @@ -0,0 +1,40 @@ + + + + + + + + \ No newline at end of file diff --git a/lib_base/src/main/res/anim/v_fragment_exit.xml b/lib_base/src/main/res/anim/v_fragment_exit.xml new file mode 100644 index 0000000..ad6e77e --- /dev/null +++ b/lib_base/src/main/res/anim/v_fragment_exit.xml @@ -0,0 +1,41 @@ + + + + + + + + \ No newline at end of file diff --git a/lib_base/src/main/res/anim/v_fragment_pop_enter.xml b/lib_base/src/main/res/anim/v_fragment_pop_enter.xml new file mode 100644 index 0000000..d9c64b1 --- /dev/null +++ b/lib_base/src/main/res/anim/v_fragment_pop_enter.xml @@ -0,0 +1,30 @@ + + + + + + \ No newline at end of file diff --git a/lib_base/src/main/res/anim/v_fragment_pop_exit.xml b/lib_base/src/main/res/anim/v_fragment_pop_exit.xml new file mode 100644 index 0000000..c713cdd --- /dev/null +++ b/lib_base/src/main/res/anim/v_fragment_pop_exit.xml @@ -0,0 +1,30 @@ + + + + + + \ No newline at end of file