Skip to content

Commit

Permalink
7.0.1
Browse files Browse the repository at this point in the history
- 修复部分关于FragmentChangeUtil使用 ViewPager删除Fragment 后focusFragment指向错误的问题;
- 增加设置 BaseFrameworkSettings.supportOverrideActivityTransition默认为 false,开启可使用overrideActivityTransition而不是overridePendingTransition,但依然警告,该 API 在某些情况下不生效,原因不明。
  • Loading branch information
kongzue committed Jul 3, 2024
1 parent 148b24b commit 3521fd5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ public void jumpAnim(int enterAnim, int exitAnim) {
public void jumpAnim(int enterAnim, int exitAnim, boolean isExit) {
int version = Integer.valueOf(Build.VERSION.SDK_INT);
if (version > 5) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && BaseFrameworkSettings.supportOverrideActivityTransition) {
overrideActivityTransition(isExit ? OVERRIDE_TRANSITION_CLOSE : OVERRIDE_TRANSITION_OPEN, enterAnim, exitAnim);
} else {
overridePendingTransition(enterAnim, exitAnim);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* CreateTime: 2018/9/30 03:22
*/
public class BaseFrameworkSettings {

private static OnBugReportListener onBugReportListener;

//是否开启debug模式,此开关影响打印Log日志等行为
Expand Down Expand Up @@ -202,4 +202,7 @@ private static String createDeviceId() {
//使用DataBinding
@Deprecated
public static boolean useDataBinding = false;

//使用overrideActivityTransition而不是overridePendingTransition,警告,该 API 在某些情况下不生效,原因不明
public static boolean supportOverrideActivityTransition=false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,41 @@
* CreateTime: 2019/2/20 18:17
*/
public class FragmentChangeUtil {

private OnFragmentChangeListener onFragmentChangeListener;

private BaseActivity me;
private List<BaseFragment> fragmentList;
private BaseFragment focusFragment;
private int enterAnimResId;
private int exitAnimResId;

private int frameLayoutResId;
private View containerView;

private PagerAdapter fragmentPagerAdapter;

public FragmentChangeUtil(BaseActivity me, int frameLayoutResId) {
build(me, frameLayoutResId);
}

public FragmentChangeUtil build(BaseActivity me, int frameLayoutResId) {
this.me = me;
this.frameLayoutResId = frameLayoutResId;
containerView = me.findViewById(frameLayoutResId);
fragmentList = new ArrayList<>();
return this;
}

public PagerAdapter getFragmentPagerAdapter() {
return fragmentPagerAdapter;
}

public FragmentChangeUtil addFragment(BaseFragment fragment) {
addFragment(fragment, true);
return this;
}

public FragmentChangeUtil addFragment(BaseFragment fragment, boolean isPreload) {
if (me == null || frameLayoutResId == 0 || fragmentList == null) {
log("错误:请先执行 build(...) 方法初始化 FragmentChangeUtil");
Expand All @@ -84,7 +84,7 @@ public FragmentChangeUtil addFragment(BaseFragment fragment, boolean isPreload)
} else {
fragmentPagerAdapter.notifyDataSetChanged();
}
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if (focusFragment != null) {
Expand All @@ -100,12 +100,12 @@ public void onPageSelected(int position) {
}
return this;
}

public FragmentChangeUtil show(BaseFragment fragment) {
show(fragment, true);
return this;
}

public FragmentChangeUtil show(BaseFragment fragment, boolean autoHideOldFragment) {
if (me == null || frameLayoutResId == 0 || fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
Expand All @@ -122,12 +122,12 @@ public FragmentChangeUtil show(BaseFragment fragment, boolean autoHideOldFragmen
enterAnimResId = 0;
exitAnimResId = 0;
}

if (autoHideOldFragment && focusFragment != null) {
focusFragment.onHide();
transaction.hide(focusFragment);
}

if (!fragment.isAddedCompat()) {
transaction.add(frameLayoutResId, fragment);
} else {
Expand All @@ -151,12 +151,12 @@ public FragmentChangeUtil show(BaseFragment fragment, boolean autoHideOldFragmen
}
return this;
}

public FragmentChangeUtil show(int index) {
show(index, true);
return this;
}

public FragmentChangeUtil show(int index, boolean autoHideOldFragment) {
if (me == null || frameLayoutResId == 0 || fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
Expand All @@ -169,12 +169,12 @@ public FragmentChangeUtil show(int index, boolean autoHideOldFragment) {
enterAnimResId = 0;
exitAnimResId = 0;
}

if (autoHideOldFragment && focusFragment != null) {
focusFragment.onHide();
transaction.hide(focusFragment);
}

if (!fragmentList.get(index).isAddedCompat()) {
transaction.add(frameLayoutResId, fragmentList.get(index));
} else {
Expand All @@ -183,7 +183,7 @@ public FragmentChangeUtil show(int index, boolean autoHideOldFragment) {
}
transaction.show(fragmentList.get(index));
}

transaction.commit();
fragmentList.get(index).callShow();
focusFragment = fragmentList.get(index);
Expand All @@ -199,7 +199,7 @@ public FragmentChangeUtil show(int index, boolean autoHideOldFragment) {
}
return this;
}

public FragmentChangeUtil hide(int index) {
if (me == null || frameLayoutResId == 0 || fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
Expand All @@ -212,13 +212,13 @@ public FragmentChangeUtil hide(int index) {
enterAnimResId = 0;
exitAnimResId = 0;
}

if (!fragmentList.get(index).isAddedCompat()) {
transaction.add(frameLayoutResId, fragmentList.get(index));
}
fragmentList.get(index).onHide();
transaction.hide(fragmentList.get(index));

transaction.commit();
}
if (containerView instanceof ViewPager) {
Expand All @@ -227,7 +227,7 @@ public FragmentChangeUtil hide(int index) {
}
return this;
}

public FragmentChangeUtil hideNow() {
if (me == null || frameLayoutResId == 0 || fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
Expand All @@ -240,10 +240,10 @@ public FragmentChangeUtil hideNow() {
enterAnimResId = 0;
exitAnimResId = 0;
}

focusFragment.onHide();
transaction.hide(focusFragment);

transaction.commit();
}
if (containerView instanceof ViewPager) {
Expand All @@ -252,7 +252,7 @@ public FragmentChangeUtil hideNow() {
}
return this;
}

public FragmentChangeUtil hide(BaseFragment fragment) {
if (me == null || frameLayoutResId == 0 || fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
Expand All @@ -265,13 +265,13 @@ public FragmentChangeUtil hide(BaseFragment fragment) {
enterAnimResId = 0;
exitAnimResId = 0;
}

if (!fragment.isAddedCompat()) {
transaction.add(frameLayoutResId, fragment);
}
fragment.onHide();
transaction.hide(fragment);

transaction.commit();
}
if (containerView instanceof ViewPager) {
Expand All @@ -285,7 +285,7 @@ public FragmentChangeUtil remove(int fragmentIndex) {
remove(getFragment(fragmentIndex));
return this;
}

public FragmentChangeUtil remove(BaseFragment fragment) {
if (me == null || frameLayoutResId == 0 || fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
Expand All @@ -298,47 +298,50 @@ public FragmentChangeUtil remove(BaseFragment fragment) {
enterAnimResId = 0;
exitAnimResId = 0;
}

if (fragment.isAddedCompat()) {
transaction.remove(fragment);
}
fragment.setAdded(false);
fragmentList.remove(fragment);

transaction.commit();
}
if (containerView instanceof ViewPager) {
fragmentList.remove(fragment);
fragmentPagerAdapter.notifyDataSetChanged();
if (focusFragment == fragment) {
focusFragment = fragmentList.get(((ViewPager) containerView).getCurrentItem());
}
}
return this;
}

public int getCount() {
if (fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
return -1;
}
return fragmentList.size();
}

public <B extends BaseFragment> B getFocusFragment() {
return (B) focusFragment;
}

public FragmentChangeUtil setFocusFragment(BaseFragment fragment) {
focusFragment = fragment;
return this;
}

public int getFocusFragmentIndex() {
if (fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
return -1;
}
return fragmentList.indexOf(focusFragment);
}

//简易Log
public void log(final Object obj) {
try {
Expand All @@ -353,7 +356,7 @@ public void log(final Object obj) {
e.printStackTrace();
}
}

public <B extends BaseFragment> B getFragment(int index) {
if (me == null || frameLayoutResId == 0 || fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
Expand All @@ -363,52 +366,52 @@ public <B extends BaseFragment> B getFragment(int index) {
log("错误:要获取的 index=" + index + " 超出了已添加的列表范围:" + fragmentList.size());
return null;
}
return (B)fragmentList.get(index);
return (B) fragmentList.get(index);
}

public <B extends BaseFragment> B getFragment(String instanceKey) {
if (me == null || frameLayoutResId == 0 || fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
return null;
}
for (BaseFragment fragment : fragmentList) {
if (fragment.getInstanceKey().equals(instanceKey)) {
return (B)fragment;
return (B) fragment;
}
}
return null;
}

public <B extends BaseFragment> B getFragment(Class c) {
if (me == null || frameLayoutResId == 0 || fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
return null;
}
for (BaseFragment b : fragmentList) {
if (b.getClass().equals(c)) {
return (B)b;
return (B) b;
}
}
return null;
}

public OnFragmentChangeListener getOnFragmentChangeListener() {
return onFragmentChangeListener;
}

public FragmentChangeUtil setOnFragmentChangeListener(OnFragmentChangeListener onFragmentChangeListener) {
this.onFragmentChangeListener = onFragmentChangeListener;
return this;
}

public int size() {
if (fragmentList == null) {
log("错误:请先执行build(...)方法初始化FragmentChangeUtil");
return 0;
}
return fragmentList.size();
}

public FragmentChangeUtil anim(int enterAnimResId, int exitAnimResId) {
this.enterAnimResId = enterAnimResId;
this.exitAnimResId = exitAnimResId;
Expand Down

0 comments on commit 3521fd5

Please sign in to comment.