ModulesFragmentMgr.java
4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.polysoft.nafmii.fragment;
import android.text.TextUtils;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.polysoft.nafmii.bean.MenuBean;
import com.polysoft.nafmii.enums.ModulesEnum;
import com.polysoft.nafmii.webview.IWebview;
import java.util.List;
public class ModulesFragmentMgr implements FragmentManager.OnBackStackChangedListener {
private FragmentManager manager;
private int containerId;
private ModulesEnum currentModule;
private OnChangeModuleListener changeListener;
public ModulesFragmentMgr(FragmentManager manager, int containerId) {
this.manager = manager;
this.containerId = containerId;
}
public void addChangeModuleLsitener(OnChangeModuleListener listener) {
this.changeListener = listener;
}
public void showInitFragment(ModulesEnum[] modules) {
for (int i = 0; i < modules.length; i++) {
this.addModuleFragment(modules[i]);
}
WebViewFragment fragment = this.findFragment(modules[0]);
FragmentTransaction transaction = this.manager.beginTransaction();
transaction.show(fragment).commitNow();
this.setCurrentModule(modules[0]);
}
public WebViewFragment getCurrentFragment() {
return this.findFragment(this.currentModule);
}
public boolean onBackStack() {
WebViewFragment fragment = getCurrentFragment();
IWebview webview = fragment.getWebview();
if (null != webview && webview.canGoBack()) {
return false;
}
if (this.manager.getBackStackEntryCount() > 0) {
this.manager.addOnBackStackChangedListener(this);
this.manager.popBackStack();
return false;
}
return true;
}
public void showModule(ModulesEnum module) {
if (module == this.currentModule) {
return ;
}
WebViewFragment fragment = this.findFragment(module);
if (!module.fmtKey.equals(this.currentModule.fmtKey)) {
FragmentTransaction transaction = this.manager.beginTransaction();
transaction.hide(this.getCurrentFragment());
transaction.addToBackStack("").show(fragment).commit();
} else {
IWebview webview = fragment.getWebview();
if (null != webview) {
webview.loadUrl(module.webUrl);
}
}
this.setCurrentModule(module);
}
public void showModule(MenuBean bean) {
ModulesEnum module = ModulesEnum.findModule(bean.getModuleName());
WebViewFragment fragment = this.findFragment(module);
if (!TextUtils.isEmpty(bean.getUrl())) {
IWebview webview = fragment.getWebview();
webview.loadUrl(bean.getUrl());
}
if (!module.fmtKey.equals(this.currentModule.fmtKey)) {
FragmentTransaction transaction = this.manager.beginTransaction();
transaction.hide(this.getCurrentFragment());
transaction.addToBackStack("").show(fragment).commit();
}
this.setCurrentModule(module);
}
public WebViewFragment findFragment(ModulesEnum module) {
WebViewFragment fragment = (WebViewFragment) this.manager.findFragmentByTag(module.fmtKey);
if (null == fragment) {
return this.addModuleFragment(module);
}
return fragment;
}
private WebViewFragment addModuleFragment(ModulesEnum module) {
WebViewFragment fragment = (WebViewFragment) this.manager.findFragmentByTag(module.fmtKey);
if (null == fragment) {
FragmentTransaction transaction = this.manager.beginTransaction();
fragment = WebViewFragment.newInstances(module.webUrl);
transaction.add(this.containerId, fragment, module.fmtKey).hide(fragment).commitNow();
}
return fragment;
}
private void setCurrentModule(ModulesEnum module) {
this.currentModule = module;
if (null != this.changeListener) {
Fragment fragment = this.getCurrentFragment();
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()) {
setCurrentModule(ModulesEnum.findModule(fragment.getTag()));
return;
}
}
}
public interface OnChangeModuleListener {
void changeModule(ModulesEnum module, Fragment fragment);
}
}