Commit 14d7e47f by amateur

1

1 parent c7a6c26e
...@@ -7,6 +7,7 @@ import androidx.fragment.app.FragmentTransaction; ...@@ -7,6 +7,7 @@ import androidx.fragment.app.FragmentTransaction;
import com.polysoft.nafmii.bean.MenuBean; import com.polysoft.nafmii.bean.MenuBean;
import com.polysoft.nafmii.enums.ModulesEnum; import com.polysoft.nafmii.enums.ModulesEnum;
import com.polysoft.nafmii.fragment.HomeWebviewFragment;
import com.polysoft.nafmii.fragment.WebViewFragment; import com.polysoft.nafmii.fragment.WebViewFragment;
import com.polysoft.nafmii.webview.IWebview; import com.polysoft.nafmii.webview.IWebview;
...@@ -14,18 +15,19 @@ import java.util.List; ...@@ -14,18 +15,19 @@ import java.util.List;
public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedListener { public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedListener {
private FragmentManager manager; private final FragmentManager manager;
private int containerId; private final int containerId;
private final ModuleUrlHandle moduleHandle;
private ModulesEnum currentModule; private ModulesEnum currentModule;
private OnChangeModuleListener changeListener; private OnChangeModuleListener changeListener;
private ModuleUrlHandle moduleHandle;
public ModulesFragmentMgr(FragmentActivity activity, int containerId) { public ModulesFragmentMgr(FragmentActivity activity, int containerId) {
this.manager = activity.getSupportFragmentManager(); this.manager = activity.getSupportFragmentManager();
this.containerId = containerId; this.containerId = containerId;
this.moduleHandle = new ModuleUrlHandle(activity); this.moduleHandle = new ModuleUrlHandle(activity);
manager.addOnBackStackChangedListener(this); this.manager.addOnBackStackChangedListener(this);
} }
public void addChangeModuleLsitener(OnChangeModuleListener listener) { public void addChangeModuleLsitener(OnChangeModuleListener listener) {
...@@ -33,22 +35,23 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis ...@@ -33,22 +35,23 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis
} }
public void showInitFragment(ModulesEnum[] modules) { public void showInitFragment(ModulesEnum[] modules) {
for (int i = 0; i < modules.length; i++) { for (ModulesEnum module : modules) {
this.addModuleFragment(modules[i]); this.findNewModuleFragment(module);
} }
this.showModule(modules[0]);
WebViewFragment fragment = this.findFragment(modules[0]);
FragmentTransaction transaction = this.manager.beginTransaction();
transaction.show(fragment).commitNow();
this.setCurrentModule(modules[0]);
} }
public WebViewFragment getCurrentFragment() { public WebViewFragment getCurrentFragment() {
if (null == this.currentModule) return null;
return this.findFragment(this.currentModule); return this.findFragment(this.currentModule);
} }
public boolean onBackStack() { public boolean onBackStack() {
WebViewFragment fragment = getCurrentFragment(); WebViewFragment fragment = this.getCurrentFragment();
if (null == fragment) {
return false;
}
IWebview webview = fragment.getWebview(); IWebview webview = fragment.getWebview();
if (null != webview && webview.canGoBack()) { if (null != webview && webview.canGoBack()) {
return false; return false;
...@@ -60,10 +63,27 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis ...@@ -60,10 +63,27 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis
return true; return true;
} }
@Override
public void onBackStackChanged() {
List<Fragment> fragments = this.manager.getFragments();
for (int i = 0; i < fragments.size(); i++) {
Fragment fragment = fragments.get(i);
if (fragment.isVisible()) {
this.setCurrentModule(ModulesEnum.findModule(fragment.getTag()));
return;
}
}
}
public void showModule(ModulesEnum module) { public void showModule(ModulesEnum module) {
if (module == this.currentModule) { if (module == this.currentModule) {
return; return;
} }
if (null == this.currentModule) {
this.showModuleFragment(this.findFragment(module));
this.setCurrentModule(module);
return;
}
this.showModule(this.moduleHandle.getModuleHandle(module)); this.showModule(this.moduleHandle.getModuleHandle(module));
} }
...@@ -71,54 +91,52 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis ...@@ -71,54 +91,52 @@ public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedLis
this.showModule(moduleHandle.getModuleHandle(bean)); this.showModule(moduleHandle.getModuleHandle(bean));
} }
public WebViewFragment findFragment(ModulesEnum module) {
return (WebViewFragment) this.manager.findFragmentByTag(module.fmtKey);
}
private void showModule(ModuleUrlHandle.IModuleHandle handle) { private void showModule(ModuleUrlHandle.IModuleHandle handle) {
ModulesEnum module = handle.getModule(); ModulesEnum module = handle.getModule();
WebViewFragment fragment = this.findFragment(module); WebViewFragment fragment = this.findNewModuleFragment(module);
if (!module.fmtKey.equals(this.currentModule.fmtKey)) { if (!module.fmtKey.equals(this.currentModule.fmtKey)) {
FragmentTransaction transaction = this.manager.beginTransaction(); this.showModuleFragment(fragment);
transaction.hide(this.getCurrentFragment());
transaction.addToBackStack("").show(fragment).commit();
} else { } else {
handle.loadUrl(fragment.getWebview()); handle.loadUrl(fragment.getWebview());
} }
this.setCurrentModule(module); this.setCurrentModule(module);
} }
public WebViewFragment findFragment(ModulesEnum module) { private WebViewFragment findNewModuleFragment(ModulesEnum module) {
return this.addModuleFragment(module); WebViewFragment fragment = this.findFragment(module);
}
private WebViewFragment addModuleFragment(ModulesEnum module) {
WebViewFragment fragment = (WebViewFragment) this.manager.findFragmentByTag(module.fmtKey);
if (null == fragment) { if (null == fragment) {
FragmentTransaction transaction = this.manager.beginTransaction(); FragmentTransaction transaction = this.manager.beginTransaction();
fragment = WebViewFragment.newInstances(this.moduleHandle.getModuleUrl(module)); fragment = HomeWebviewFragment.newInstance(this.moduleHandle.getModuleUrl(module));
transaction.add(this.containerId, fragment, module.fmtKey).hide(fragment).commitNow(); transaction.add(this.containerId, fragment, module.fmtKey).hide(fragment).commitNow();
} }
return fragment; return fragment;
} }
private void showModuleFragment(Fragment fragment) {
if (null == fragment) return;
FragmentTransaction transaction = this.manager.beginTransaction();
WebViewFragment currentFragment = this.getCurrentFragment();
if (null != currentFragment) {
transaction.hide(currentFragment).addToBackStack("");
}
transaction.show(fragment).commit();
}
private void setCurrentModule(ModulesEnum module) { private void setCurrentModule(ModulesEnum module) {
this.currentModule = module; this.currentModule = module;
if (null != this.changeListener) { if (null != this.changeListener) {
Fragment fragment = this.getCurrentFragment(); WebViewFragment fragment = this.getCurrentFragment();
this.changeListener.changeModule(module, fragment); this.changeListener.changeModule(module, fragment);
} }
} }
@Override
public void onBackStackChanged() {
List<Fragment> fragments = this.manager.getFragments();
for (int i = 0; i < fragments.size(); i++) {
Fragment fragment = fragments.get(i);
if (fragment.isVisible()) {
this.setCurrentModule(ModulesEnum.findModule(fragment.getTag()));
return;
}
}
}
public interface OnChangeModuleListener { public interface OnChangeModuleListener {
void changeModule(ModulesEnum module, Fragment fragment); void changeModule(ModulesEnum module, Fragment fragment);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!