Skip to content

Commit 83b96ff

Browse files
committed
see 02/13 log
1 parent 9ded679 commit 83b96ff

File tree

15 files changed

+463
-157
lines changed

15 files changed

+463
-157
lines changed

README-CN.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,26 @@ getFileExtension : 根据全路径获取文件拓展名
212212

213213
> - **Fragment相关→[FragmentUtils.java][fragment.java][Demo][fragment.demo]**
214214
```
215-
addFragment : 新增fragment
216-
popFragment : 出栈fragment
217-
popToFragment : 出栈到指定fragment
218-
popAddFragment : 先出栈后新增fragment
219-
hideFragment : 隐藏fragment
220-
showFragment : 显示fragment
221-
hideShowFragment : 先隐藏后显示fragment
222-
replaceFragment : 替换fragment
223-
getTopFragment : 获得栈顶fragment
224-
getTopShowFragment : 获得栈顶可见fragment
225-
getPreFragment : 获取目标fragment的前一个fragment
226-
findFragment : 寻找fragment
227-
getFragments : 获取同级别的fragment
228-
getAllFragments : 获取所有fragment
229-
setBackgroundColor : 设置背景色
230-
setBackgroundResource: 设置背景资源
231-
setBackground : 设置背景
215+
addFragment : 新增fragment
216+
popFragment : 出栈fragment
217+
popToFragment : 出栈到指定fragment
218+
popAddFragment : 先出栈后新增fragment
219+
hideFragment : 隐藏fragment
220+
showFragment : 显示fragment
221+
hideShowFragment : 先隐藏后显示fragment
222+
replaceFragment : 替换fragment
223+
getLastAddFragment : 获取最后加入的fragment
224+
getLastAddFragmentInStack: 获取栈中最后加入的fragment
225+
getTopShowFragment : 获取顶层可见fragment
226+
getTopShowFragmentInStack: 获取栈中顶层可见fragment
227+
findFragment : 查找fragment
228+
getPreFragment : 获取目标fragment的前一个fragment
229+
getFragments : 获取同级别的fragment
230+
getAllFragments : 获取所有fragment
231+
getAllFragmentsInStack : 获取栈中所有fragment
232+
setBackgroundColor : 设置背景色
233+
setBackgroundResource : 设置背景资源
234+
setBackground : 设置背景
232235
```
233236

234237
> - **Handler相关→[HandlerUtils.java][handler.java][Demo][handler.demo]**

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,15 @@ hideFragment
220220
showFragment
221221
hideShowFragment
222222
replaceFragment
223-
getTopFragment
223+
getLastAddFragment
224+
getLastAddFragmentInStack
224225
getTopShowFragment
225-
getPreFragment
226+
getTopShowFragmentInStack
226227
findFragment
228+
getPreFragment
227229
getFragments
228230
getAllFragments
231+
getAllFragmentsInStack
229232
setBackgroundColor
230233
setBackgroundResource
231234
setBackground

app/src/main/java/com/blankj/androidutilcode/activity/FragmentActivity.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@
1818
*/
1919
public class FragmentActivity extends AppCompatActivity {
2020

21-
public Fragment fragment;
21+
public Fragment rootFragment;
2222

2323
@Override
2424
protected void onCreate(Bundle savedInstanceState) {
2525
super.onCreate(savedInstanceState);
2626
setContentView(R.layout.activity_fragment);
27-
fragment = FragmentUtils.addFragment(getSupportFragmentManager(), Demo0Fragment.newInstance(), R.id.fragment_container);
27+
rootFragment = FragmentUtils.addFragment(getSupportFragmentManager(), Demo0Fragment.newInstance(), R.id.fragment_container);
2828
}
29-
}
29+
30+
31+
@Override
32+
public void onBackPressed() {
33+
if (!FragmentUtils.dispatchBackPress(getSupportFragmentManager())) {
34+
super.onBackPressed();
35+
}
36+
}
37+
}

app/src/main/java/com/blankj/androidutilcode/fragment/Demo0Fragment.java

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
import android.os.Bundle;
55
import android.support.annotation.Nullable;
66
import android.support.v4.app.Fragment;
7+
import android.support.v4.view.ViewCompat;
78
import android.view.LayoutInflater;
89
import android.view.View;
910
import android.view.ViewGroup;
11+
import android.widget.Button;
1012
import android.widget.TextView;
1113

1214
import com.blankj.androidutilcode.R;
15+
import com.blankj.androidutilcode.activity.FragmentActivity;
1316
import com.blankj.utilcode.utils.FragmentUtils;
1417
import com.blankj.utilcode.utils.ToastUtils;
1518

1619
import java.util.Random;
17-
import java.util.Stack;
1820

1921
/**
2022
* <pre>
@@ -25,7 +27,7 @@
2527
* </pre>
2628
*/
2729
public class Demo0Fragment extends Fragment
28-
implements View.OnClickListener {
30+
implements View.OnClickListener,FragmentUtils.OnBackClickListener {
2931

3032
private Fragment fragment1;
3133

@@ -38,10 +40,9 @@ public static Demo0Fragment newInstance() {
3840
return fragment;
3941
}
4042

43+
private Button btnShowAboutFragment;
4144
private TextView tvAboutFragment;
4245

43-
Stack<Fragment> stack = new Stack<>();
44-
4546
@Override
4647
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
4748
return inflater.inflate(R.layout.fragment_demo0, container, false);
@@ -50,8 +51,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
5051
@Override
5152
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
5253
super.onViewCreated(view, savedInstanceState);
53-
view.findViewById(R.id.btn_show_stack).setOnClickListener(this);
54-
view.findViewById(R.id.btn_add).setOnClickListener(this);
54+
btnShowAboutFragment = (Button) view.findViewById(R.id.btn_show_about_fragment);
55+
btnShowAboutFragment.setOnClickListener(this);
56+
view.findViewById(R.id.btn_add_hide).setOnClickListener(this);
57+
view.findViewById(R.id.btn_add_show).setOnClickListener(this);
5558
view.findViewById(R.id.btn_add_child).setOnClickListener(this);
5659
view.findViewById(R.id.btn_pop_to_root).setOnClickListener(this);
5760
view.findViewById(R.id.btn_pop_add).setOnClickListener(this);
@@ -71,35 +74,50 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
7174
public void onClick(View view) {
7275
tvAboutFragment.setText("");
7376
switch (view.getId()) {
74-
case R.id.btn_show_stack:
75-
tvAboutFragment.setText(FragmentUtils.getAllFragments(getFragmentManager()).toString());
77+
case R.id.btn_show_about_fragment:
78+
tvAboutFragment.setText("---all fragments---\n"
79+
+ FragmentUtils.getAllFragments(getFragmentManager()).toString()
80+
+ "\n-------------------\n\n"
81+
+ "---stack top---\n"
82+
+ FragmentUtils.getAllFragmentsInStack(getFragmentManager()).toString()
83+
+ "\n---stack bottom---\n\n"
84+
+ "\ntopFragment: " + FragmentUtils.getTopFragment(getFragmentManager()).getClass().getSimpleName()
85+
+ "\ntopShowFragment: " + FragmentUtils.getTopShowFragment(getFragmentManager()).getClass().getSimpleName()
86+
);
87+
break;
88+
case R.id.btn_add_hide:
89+
FragmentUtils.addFragment(getFragmentManager(), Demo1Fragment.newInstance(), R.id.fragment_container, true, true);
7690
break;
77-
case R.id.btn_add:
78-
stack.add(FragmentUtils.addFragment(getFragmentManager(), Demo1Fragment.newInstance(), R.id.fragment_container, true, true));
91+
case R.id.btn_add_show:
92+
FragmentUtils.addFragment(getFragmentManager(), Demo1Fragment.newInstance(), R.id.fragment_container, false, true);
7993
break;
8094
case R.id.btn_add_child:
8195
FragmentUtils.addFragment(getChildFragmentManager(), Demo2Fragment.newInstance(), R.id.child_fragment_container, false, true);
8296
break;
8397
case R.id.btn_pop_to_root:
8498
FragmentUtils.popToFragment(getFragmentManager(), Demo1Fragment.class, true);
85-
stack.clear();
8699
break;
87100
case R.id.btn_pop_add:
88-
if (!stack.isEmpty()) {
89-
stack.pop();
90-
}
91-
FragmentUtils.popAddFragment(getFragmentManager(), R.id.fragment_container, Demo2Fragment.newInstance(), true);
101+
ViewCompat.setTransitionName(btnShowAboutFragment, "addSharedElement");
102+
FragmentUtils.popAddFragment(getFragmentManager(), R.id.fragment_container, Demo2Fragment.newInstance(), true, new FragmentUtils.SharedElement(this.btnShowAboutFragment, "btnShowAboutFragment"));
92103
break;
93104
case R.id.btn_hide_show:
94-
if (!stack.isEmpty()) {
95-
FragmentUtils.hideShowFragment(this, stack.peek());
105+
Fragment fragment1 = FragmentUtils.findFragment(getFragmentManager(), Demo1Fragment.class);
106+
if (fragment1 != null) {
107+
FragmentUtils.hideShowFragment(this, fragment1);
96108
} else {
97-
ToastUtils.showLongToast("please add first!");
109+
ToastUtils.showLongToast("please add demo1 first!");
98110
}
99111
break;
100112
case R.id.btn_replace:
101-
FragmentUtils.replaceFragment(this, newInstance(), false);
113+
((FragmentActivity) getActivity()).rootFragment = FragmentUtils.replaceFragment(this, Demo3Fragment.newInstance(), false);
102114
break;
103115
}
104116
}
117+
118+
@Override
119+
public boolean onBackClick() {
120+
FragmentUtils.popToFragment(getFragmentManager(), Demo1Fragment.class, true);
121+
return false;
122+
}
105123
}

app/src/main/java/com/blankj/androidutilcode/fragment/Demo1Fragment.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* </pre>
2525
*/
2626
public class Demo1Fragment extends Fragment
27-
implements View.OnClickListener {
27+
implements View.OnClickListener{
2828

2929
public static Demo1Fragment newInstance() {
3030

@@ -45,7 +45,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
4545
@Override
4646
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
4747
super.onViewCreated(view, savedInstanceState);
48-
view.findViewById(R.id.btn_show_stack).setOnClickListener(this);
48+
view.findViewById(R.id.btn_show_about_fragment).setOnClickListener(this);
4949
view.findViewById(R.id.btn_hide_show).setOnClickListener(this);
5050
tvAboutFragment = (TextView) view.findViewById(R.id.tv_about_fragment);
5151
}
@@ -61,12 +61,28 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
6161
public void onClick(View view) {
6262
tvAboutFragment.setText("");
6363
switch (view.getId()) {
64-
case R.id.btn_show_stack:
65-
tvAboutFragment.setText(FragmentUtils.getAllFragments(getFragmentManager()).toString());
64+
case R.id.btn_show_about_fragment:
65+
tvAboutFragment.setText("---all fragments---\n"
66+
+ FragmentUtils.getAllFragments(getFragmentManager()).toString()
67+
+ "\n-------------------\n\n"
68+
+ "---stack top---\n"
69+
+ FragmentUtils.getAllFragmentsInStack(getFragmentManager()).toString()
70+
+ "\n---stack bottom---\n\n"
71+
+ "\ntopFragment: " + FragmentUtils.getTopFragment(getFragmentManager()).getClass().getSimpleName()
72+
+ "\ntopShowFragment: " + FragmentUtils.getTopShowFragment(getFragmentManager()).getClass().getSimpleName()
73+
);
6674
break;
6775
case R.id.btn_hide_show:
68-
FragmentUtils.hideShowFragment(this, ((FragmentActivity) getActivity()).fragment);
76+
FragmentUtils.hideShowFragment(this, ((FragmentActivity) getActivity()).rootFragment);
6977
break;
7078
}
7179
}
80+
81+
// @Override
82+
// public boolean onBackClick() {
83+
// LogUtils.d("onBackClick");
84+
// FragmentUtils.showFragment(((FragmentActivity) getActivity()).rootFragment);
85+
// FragmentUtils.popFragment(getFragmentManager());
86+
// return true;
87+
// }
7288
}

app/src/main/java/com/blankj/androidutilcode/fragment/Demo2Fragment.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.blankj.androidutilcode.R;
1313
import com.blankj.utilcode.utils.FragmentUtils;
14+
import com.blankj.utilcode.utils.LogUtils;
1415

1516
import java.util.Random;
1617

@@ -23,7 +24,7 @@
2324
* </pre>
2425
*/
2526
public class Demo2Fragment extends Fragment
26-
implements View.OnClickListener {
27+
implements View.OnClickListener ,FragmentUtils.OnBackClickListener{
2728

2829
public static Demo2Fragment newInstance() {
2930

@@ -44,7 +45,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
4445
@Override
4546
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
4647
super.onViewCreated(view, savedInstanceState);
47-
view.findViewById(R.id.btn_show_stack).setOnClickListener(this);
48+
view.findViewById(R.id.btn_show_about_fragment).setOnClickListener(this);
4849
view.findViewById(R.id.btn_pop).setOnClickListener(this);
4950
tvAboutFragment = (TextView) view.findViewById(R.id.tv_about_fragment);
5051
}
@@ -60,12 +61,26 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
6061
public void onClick(View view) {
6162
tvAboutFragment.setText("");
6263
switch (view.getId()) {
63-
case R.id.btn_show_stack:
64-
tvAboutFragment.setText(FragmentUtils.getAllFragments(getFragmentManager()).toString());
64+
case R.id.btn_show_about_fragment:
65+
tvAboutFragment.setText("---all fragments---\n"
66+
+ FragmentUtils.getAllFragments(getFragmentManager()).toString()
67+
+ "\n-------------------\n\n"
68+
+ "---stack top---\n"
69+
+ FragmentUtils.getAllFragmentsInStack(getFragmentManager()).toString()
70+
+ "\n---stack bottom---\n\n"
71+
+ "\ntopFragment: " + FragmentUtils.getTopFragment(getFragmentManager()).getClass().getSimpleName()
72+
+ "\ntopShowFragment: " + FragmentUtils.getTopShowFragment(getFragmentManager()).getClass().getSimpleName()
73+
);
6574
break;
6675
case R.id.btn_pop:
6776
FragmentUtils.popFragment(getFragmentManager());
6877
break;
6978
}
7079
}
80+
81+
@Override
82+
public boolean onBackClick() {
83+
LogUtils.d("demo2 onBackClick");
84+
return false;
85+
}
7186
}

0 commit comments

Comments
 (0)