Commit 5b24f83e by amateur

修改多个模块支持同一个fragment

1 parent 4e6ad246
...@@ -2,16 +2,18 @@ package com.polysoft.nafmii.enums; ...@@ -2,16 +2,18 @@ package com.polysoft.nafmii.enums;
public enum ModulesEnum { public enum ModulesEnum {
HOME("home", "file:///android_asset/webpage/fileChooser.html"), HOME("home", "home", "file:///android_asset/dist/index.html"),
MEMBER("member", "file:///android_asset/webpage/fileChooser.html"), MEMBER("member", "home","file:///android_asset/dict/index.html"),
LEARN("learn", "http://debugtbs.qq.com"), LEARN("learn", "learn", "http://debugtbs.qq.com"),
MY("my", "http://192.168.43.183:8080"); MY("my", "home","file:///android_asset/dist/index.html/#/user/center");
public final String name; public final String name;
public final String fmtKey;
public final String webUrl; public final String webUrl;
ModulesEnum(String tag, String url) { ModulesEnum(String name, String fmtKey, String url) {
this.name = tag; this.name = name;
this.fmtKey = fmtKey;
this.webUrl = url; this.webUrl = url;
} }
......
...@@ -25,17 +25,18 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis ...@@ -25,17 +25,18 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis
} }
public void showInitFragment(ModulesEnum[] modules) { public void showInitFragment(ModulesEnum[] modules) {
for (int i = 1; i < modules.length; i++) { for (int i = 0; i < modules.length; i++) {
this.addModuleFragment(modules[i]); this.addModuleFragment(modules[i]);
} }
Fragment fragment = this.addModuleFragment(modules[0]);
Fragment fragment = this.manager.findFragmentByTag(modules[0].fmtKey);
FragmentTransaction transaction = this.manager.beginTransaction(); FragmentTransaction transaction = this.manager.beginTransaction();
transaction.show(fragment).commitNow(); transaction.show(fragment).commitNow();
this.setCurrentModule(modules[0]); this.setCurrentModule(modules[0]);
} }
public Fragment getCurrentFragment() { public Fragment getCurrentFragment() {
String tag = this.currentModule.name; String tag = this.currentModule.fmtKey;
return this.manager.findFragmentByTag(tag); return this.manager.findFragmentByTag(tag);
} }
...@@ -54,13 +55,15 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis ...@@ -54,13 +55,15 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis
} }
Fragment fragment = this.findFragment(module); Fragment fragment = this.findFragment(module);
FragmentTransaction transaction = this.manager.beginTransaction(); FragmentTransaction transaction = this.manager.beginTransaction();
if (!module.fmtKey.equals(this.currentModule.fmtKey)) {
transaction.hide(this.getCurrentFragment()); transaction.hide(this.getCurrentFragment());
transaction.addToBackStack("").show(fragment).commit(); transaction.addToBackStack("").show(fragment).commit();
}
this.setCurrentModule(module); this.setCurrentModule(module);
} }
public Fragment findFragment(ModulesEnum module) { public Fragment findFragment(ModulesEnum module) {
Fragment fragment = this.manager.findFragmentByTag(module.name); Fragment fragment = this.manager.findFragmentByTag(module.fmtKey);
if (null == fragment) { if (null == fragment) {
return this.addModuleFragment(module); return this.addModuleFragment(module);
} }
...@@ -68,9 +71,12 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis ...@@ -68,9 +71,12 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis
} }
private Fragment addModuleFragment(ModulesEnum module) { private Fragment addModuleFragment(ModulesEnum module) {
Fragment fragment = this.manager.findFragmentByTag(module.fmtKey);
if (null == fragment) {
FragmentTransaction transaction = this.manager.beginTransaction(); FragmentTransaction transaction = this.manager.beginTransaction();
Fragment fragment = WebViewFragment.newInstances(module.webUrl); fragment = WebViewFragment.newInstances(module.webUrl);
transaction.add(this.containerId, fragment, module.name).hide(fragment).commitNow(); transaction.add(this.containerId, fragment, module.fmtKey).hide(fragment).commitNow();
}
return fragment; return fragment;
} }
...@@ -89,7 +95,7 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis ...@@ -89,7 +95,7 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis
Fragment fragment = fragments.get(i); Fragment fragment = fragments.get(i);
if (fragment.isVisible()) { if (fragment.isVisible()) {
setCurrentModule(ModulesEnum.findModule(fragment.getTag())); setCurrentModule(ModulesEnum.findModule(fragment.getTag()));
return ; return;
} }
} }
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!