Skip to content

Commit 4b5b8f3

Browse files
committed
Refactoring code
1 parent 913b1b6 commit 4b5b8f3

File tree

3 files changed

+89
-88
lines changed

3 files changed

+89
-88
lines changed

fragmentation/src/main/java/me/yokeyword/fragmentation/Fragmentation.java

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.support.v4.app.FragmentManager;
88
import android.support.v4.app.FragmentTransaction;
99
import android.support.v4.app.FragmentTransactionBugFixHack;
10+
import android.support.v7.app.AlertDialog;
1011
import android.util.Log;
1112
import android.view.View;
1213
import android.view.ViewGroup;
@@ -16,6 +17,7 @@
1617
import java.util.List;
1718

1819
import me.yokeyword.fragmentation.debug.DebugFragmentRecord;
20+
import me.yokeyword.fragmentation.debug.DebugHierarchyViewContainer;
1921
import me.yokeyword.fragmentation.helper.FragmentResultRecord;
2022
import me.yokeyword.fragmentation.helper.OnEnterAnimEndListener;
2123

@@ -239,8 +241,6 @@ void startWithPop(FragmentManager fragmentManager, SupportFragment from, Support
239241

240242
/**
241243
* 获得栈顶SupportFragment
242-
*
243-
* @return
244244
*/
245245
SupportFragment getTopFragment(FragmentManager fragmentManager) {
246246
List<Fragment> fragmentList = fragmentManager.getFragments();
@@ -259,7 +259,6 @@ SupportFragment getTopFragment(FragmentManager fragmentManager) {
259259
* 获取目标Fragment的前一个Fragment
260260
*
261261
* @param fragment 目标Fragment
262-
* @return
263262
*/
264263
SupportFragment getPreFragment(Fragment fragment) {
265264
List<Fragment> fragmentList = fragment.getFragmentManager().getFragments();
@@ -588,18 +587,89 @@ public void run() {
588587
// }, popAniDuration);
589588
// }
590589

591-
List<DebugFragmentRecord> getFragmentRecords() {
592-
List<DebugFragmentRecord> fragmentRecords = new ArrayList<>();
590+
/**
591+
* 调试相关:以dialog形式 显示 栈视图
592+
*/
593+
void showFragmentStackHierarchyView() {
594+
DebugHierarchyViewContainer container = new DebugHierarchyViewContainer(mActivity);
595+
container.bindFragmentRecords(getFragmentRecords());
596+
container.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
597+
new AlertDialog.Builder(mActivity)
598+
.setTitle("栈视图")
599+
.setView(container)
600+
.setPositiveButton("关闭", null)
601+
.setCancelable(true)
602+
.show();
603+
}
604+
605+
/**
606+
* 调试相关:以log形式 打印 栈视图
607+
*/
608+
void logFragmentRecords(String tag) {
609+
List<DebugFragmentRecord> fragmentRecordList = getFragmentRecords();
610+
if (fragmentRecordList == null) return;
611+
612+
StringBuilder sb = new StringBuilder();
613+
614+
for (int i = fragmentRecordList.size() - 1; i >= 0; i--) {
615+
DebugFragmentRecord fragmentRecord = fragmentRecordList.get(i);
616+
617+
if (i == fragmentRecordList.size() - 1) {
618+
sb.append("═══════════════════════════════════════════════════════════════════════════════════\n");
619+
if (i == 0) {
620+
sb.append("\t栈顶\t\t\t").append(fragmentRecord.fragmentName).append("\n");
621+
sb.append("═══════════════════════════════════════════════════════════════════════════════════");
622+
} else {
623+
sb.append("\t栈顶\t\t\t").append(fragmentRecord.fragmentName).append("\n\n");
624+
}
625+
} else if (i == 0) {
626+
sb.append("\t栈底\t\t\t").append(fragmentRecord.fragmentName).append("\n\n");
627+
processChildLog(fragmentRecord.childFragmentRecord, sb, 1);
628+
sb.append("═══════════════════════════════════════════════════════════════════════════════════");
629+
Log.i(tag, sb.toString());
630+
return;
631+
} else {
632+
sb.append("\t\t\t\t").append(fragmentRecord.fragmentName).append("\n\n");
633+
}
634+
635+
processChildLog(fragmentRecord.childFragmentRecord, sb, 1);
636+
}
637+
}
638+
639+
private List<DebugFragmentRecord> getFragmentRecords() {
640+
List<DebugFragmentRecord> fragmentRecordList = new ArrayList<>();
593641

594642
List<Fragment> fragmentList = mActivity.getSupportFragmentManager().getFragments();
643+
595644
if (fragmentList == null || fragmentList.size() < 1) return null;
596645

597646
for (Fragment fragment : fragmentList) {
598647
if (fragment == null) continue;
599-
fragmentRecords.add(new DebugFragmentRecord(fragment.getClass().getSimpleName(), getChildFragmentRecords(fragment)));
648+
fragmentRecordList.add(new DebugFragmentRecord(fragment.getClass().getSimpleName(), getChildFragmentRecords(fragment)));
600649
}
650+
return fragmentRecordList;
651+
}
601652

602-
return fragmentRecords;
653+
private void processChildLog(List<DebugFragmentRecord> fragmentRecordList, StringBuilder sb, int childHierarchy) {
654+
if (fragmentRecordList == null || fragmentRecordList.size() == 0) return;
655+
656+
for (int j = 0; j < fragmentRecordList.size(); j++) {
657+
DebugFragmentRecord childFragmentRecord = fragmentRecordList.get(j);
658+
for (int k = 0; k < childHierarchy; k++) {
659+
sb.append("\t\t\t");
660+
}
661+
if (j == 0) {
662+
sb.append("\t子栈顶\t\t").append(childFragmentRecord.fragmentName).append("\n\n");
663+
} else if (j == fragmentRecordList.size() - 1) {
664+
sb.append("\t子栈底\t\t").append(childFragmentRecord.fragmentName).append("\n\n");
665+
processChildLog(childFragmentRecord.childFragmentRecord, sb, ++childHierarchy);
666+
return;
667+
} else {
668+
sb.append("\t\t\t\t").append(childFragmentRecord.fragmentName).append("\n\n");
669+
}
670+
671+
processChildLog(childFragmentRecord.childFragmentRecord, sb, childHierarchy);
672+
}
603673
}
604674

605675
private List<DebugFragmentRecord> getChildFragmentRecords(Fragment parentFragment) {

fragmentation/src/main/java/me/yokeyword/fragmentation/SupportActivity.java

Lines changed: 9 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@
44
import android.os.Handler;
55
import android.support.annotation.Nullable;
66
import android.support.v4.app.Fragment;
7-
import android.support.v4.app.FragmentTransaction;
8-
import android.support.v7.app.AlertDialog;
97
import android.support.v7.app.AppCompatActivity;
10-
import android.util.Log;
118
import android.view.KeyEvent;
129
import android.view.MotionEvent;
13-
import android.view.ViewGroup;
14-
15-
import java.util.List;
1610

1711
import me.yokeyword.fragmentation.anim.DefaultVerticalAnimator;
1812
import me.yokeyword.fragmentation.anim.FragmentAnimator;
19-
import me.yokeyword.fragmentation.debug.DebugFragmentRecord;
20-
import me.yokeyword.fragmentation.debug.DebugHierarchyViewContainer;
2113

2214
/**
2315
* Created by YoKeyword on 16/1/22.
@@ -27,7 +19,7 @@ public class SupportActivity extends AppCompatActivity implements ISupport {
2719

2820
private FragmentAnimator mFragmentAnimator;
2921

30-
boolean mPopMulitpleNoAnim = false;
22+
boolean mPopMultipleNoAnim = false;
3123

3224
// 防抖动 是否可以点击
3325
private boolean mFragmentClickable = true;
@@ -78,7 +70,7 @@ public void setFragmentAnimator(FragmentAnimator fragmentAnimator) {
7870

7971
/**
8072
* 构建Fragment转场动画
81-
* <p/>
73+
* <p>
8274
* 如果是在Activity内实现,则构建的是Activity内所有Fragment的转场动画,
8375
* 如果是在Fragment内实现,则构建的是该Fragment的转场动画,此时优先级 > Activity的onCreateFragmentAnimator()
8476
*
@@ -190,18 +182,14 @@ public void startWithPop(SupportFragment toFragment) {
190182

191183
/**
192184
* 得到位于栈顶Fragment
193-
*
194-
* @return
195185
*/
196186
@Override
197187
public SupportFragment getTopFragment() {
198188
return mFragmentation.getTopFragment(getSupportFragmentManager());
199189
}
200190

201191
/**
202-
* 获取栈内的framgent对象
203-
*
204-
* @param fragmentClass
192+
* 获取栈内的fragment对象
205193
*/
206194
@Override
207195
public <T extends SupportFragment> T findFragment(Class<T> fragmentClass) {
@@ -236,11 +224,11 @@ public void popTo(Class<?> fragmentClass, boolean includeSelf, Runnable afterPop
236224
}
237225

238226
void preparePopMultiple() {
239-
mPopMulitpleNoAnim = true;
227+
mPopMultipleNoAnim = true;
240228
}
241229

242230
void popFinish() {
243-
mPopMulitpleNoAnim = false;
231+
mPopMultipleNoAnim = false;
244232
}
245233

246234
@Override
@@ -274,73 +262,16 @@ public void setFragmentClickable() {
274262
}
275263

276264
/**
277-
* 显示栈视图,调试时使用
265+
* 显示栈视图dialog,调试时使用
278266
*/
279267
public void showFragmentStackHierarchyView() {
280-
DebugHierarchyViewContainer container = new DebugHierarchyViewContainer(this);
281-
container.bindFragmentRecords(mFragmentation.getFragmentRecords());
282-
container.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
283-
new AlertDialog.Builder(this)
284-
.setTitle("栈视图")
285-
.setView(container)
286-
.setPositiveButton("关闭", null)
287-
.setCancelable(true)
288-
.show();
268+
mFragmentation.showFragmentStackHierarchyView();
289269
}
290270

291271
/**
292-
* 显示栈视图 日志 ,调试时使用
272+
* 显示栈视图日志,调试时使用
293273
*/
294274
public void logFragmentStackHierarchy(String TAG) {
295-
List<DebugFragmentRecord> fragmentRecordList = mFragmentation.getFragmentRecords();
296-
if (fragmentRecordList == null) return;
297-
298-
StringBuilder sb = new StringBuilder();
299-
300-
for (int i = fragmentRecordList.size() - 1; i >= 0; i--) {
301-
DebugFragmentRecord fragmentRecord = fragmentRecordList.get(i);
302-
303-
if (i == fragmentRecordList.size() - 1) {
304-
sb.append("═══════════════════════════════════════════════════════════════════════════════════\n");
305-
if (i == 0) {
306-
sb.append("\t栈顶\t\t\t" + fragmentRecord.fragmentName + "\n");
307-
sb.append("═══════════════════════════════════════════════════════════════════════════════════");
308-
} else {
309-
sb.append("\t栈顶\t\t\t" + fragmentRecord.fragmentName + "\n\n");
310-
}
311-
} else if (i == 0) {
312-
sb.append("\t栈底\t\t\t" + fragmentRecord.fragmentName + "\n\n");
313-
processChildLog(fragmentRecord.childFragmentRecord, sb, 1);
314-
sb.append("═══════════════════════════════════════════════════════════════════════════════════");
315-
Log.i(TAG, sb.toString());
316-
return;
317-
} else {
318-
sb.append("\t\t\t\t" + fragmentRecord.fragmentName + "\n\n");
319-
}
320-
321-
processChildLog(fragmentRecord.childFragmentRecord, sb, 1);
322-
}
323-
}
324-
325-
private void processChildLog(List<DebugFragmentRecord> fragmentRecordList, StringBuilder sb, int childHierarchy) {
326-
if (fragmentRecordList == null || fragmentRecordList.size() == 0) return;
327-
328-
for (int j = 0; j < fragmentRecordList.size(); j++) {
329-
DebugFragmentRecord childFragmentRecord = fragmentRecordList.get(j);
330-
for (int k = 0; k < childHierarchy; k++) {
331-
sb.append("\t\t\t");
332-
}
333-
if (j == 0) {
334-
sb.append("\t子栈顶\t\t" + childFragmentRecord.fragmentName + "\n\n");
335-
} else if (j == fragmentRecordList.size() - 1) {
336-
sb.append("\t子栈底\t\t" + childFragmentRecord.fragmentName + "\n\n");
337-
processChildLog(childFragmentRecord.childFragmentRecord, sb, ++childHierarchy);
338-
return;
339-
} else {
340-
sb.append("\t\t\t\t" + childFragmentRecord.fragmentName + "\n\n");
341-
}
342-
343-
processChildLog(childFragmentRecord.childFragmentRecord, sb, childHierarchy);
344-
}
275+
mFragmentation.logFragmentRecords(TAG);
345276
}
346277
}

fragmentation/src/main/java/me/yokeyword/fragmentation/SupportFragment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private void initAnim() {
145145

146146
@Override
147147
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
148-
if (_mActivity.mPopMulitpleNoAnim || mLocking) {
148+
if (_mActivity.mPopMultipleNoAnim || mLocking) {
149149
return mNoAnim;
150150
}
151151
if (transit == FragmentTransaction.TRANSIT_FRAGMENT_OPEN) {
@@ -422,15 +422,15 @@ public SupportFragment getPreFragment() {
422422
}
423423

424424
/**
425-
* @return 栈内fragmentClass的framgent对象
425+
* @return 栈内fragmentClass的fragment对象
426426
*/
427427
@Override
428428
public <T extends SupportFragment> T findFragment(Class<T> fragmentClass) {
429429
return mFragmentation.findStackFragment(fragmentClass, getFragmentManager(), false);
430430
}
431431

432432
/**
433-
* @return 栈内fragmentClass的子framgent对象
433+
* @return 栈内fragmentClass的子fragment对象
434434
*/
435435
@Override
436436
public <T extends SupportFragment> T findChildFragment(Class<T> fragmentClass) {

0 commit comments

Comments
 (0)