FragmentPresenter.java
3.44 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
package com.lijinji.part.fragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import com.lijinji.part.R;
import com.lijinji.part.product.PartFillingFragment;
import com.lijinji.part.product.PartManagerFragment;
/**
* Created by youjie on 2018/4/28.
*/
public class FragmentPresenter {
private FragmentManager fragmentManager;
private FragmentTransaction mFragmentTransaction;
private String[] strarr;
public String[] getStrarr() {
return strarr;
}
public void setStrarr(String[] strarr) {
this.strarr = strarr;
}
public FragmentManager getFragmentManager() {
return fragmentManager;
}
public void setFragmentManager(FragmentManager fragmentManager) {
this.fragmentManager = fragmentManager;
}
public void selectPage(int start, int end) {
if (start == end) {
return;
}
if (start < strarr.length && end < strarr.length) {
String cur = getTag(start, strarr);
String old = getTag(end, strarr);
hideAllFragment();
showFragment(cur, old);
}
}
public String getTag(int index, String[] array) {
String tag = "";
if (index > -1 && index < array.length) {
tag = array[index];
}
return tag;
}
private FragmentTransaction initTransaction() {
if (mFragmentTransaction == null) {
mFragmentTransaction = fragmentManager.beginTransaction();
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
}
return mFragmentTransaction;
}
private void commitTransactions() {
if (mFragmentTransaction != null && !mFragmentTransaction.isEmpty()) {
mFragmentTransaction.commitAllowingStateLoss();
mFragmentTransaction = null;
}
}
private void showFragment(String oldTag, String newTag) {
initTransaction();
Fragment oldFragment = getFragment(oldTag);
Fragment newFragment = getFragment(newTag);
if (newFragment == null || !newFragment.isAdded()) {
newFragment = getFragment(newTag);
mFragmentTransaction.add(R.id.content, newFragment, newTag);
mFragmentTransaction.addToBackStack(newTag);
}
if (oldFragment != null) {
mFragmentTransaction.hide(oldFragment);
}
if (newFragment != null) {
mFragmentTransaction.show(newFragment);
}
commitTransactions();
}
private void hideAllFragment() {
initTransaction();
for (String str : strarr) {
Fragment fragment = fragmentManager.findFragmentByTag(str);
if (fragment != null) {
mFragmentTransaction.hide(fragment);
}
}
commitTransactions();
}
public Fragment getFragment(String tag) {
Fragment fragment = fragmentManager.findFragmentByTag(tag);
if (fragment == null) {
fragment = newFragment(tag);
}
return fragment;
}
private BaseFragment newFragment(String tag) {
BaseFragment baseFragment = null;
if (tag.equals(strarr[0])) {
baseFragment = new PartManagerFragment();
} else if (tag.equals(strarr[1])) {
baseFragment = new PartFillingFragment();
}
return baseFragment;
}
}