forked from Blankj/AndroidUtilCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo0Fragment.java
More file actions
165 lines (150 loc) · 6.82 KB
/
Copy pathDemo0Fragment.java
File metadata and controls
165 lines (150 loc) · 6.82 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.blankj.androidutilcode.fragment;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.transition.Fade;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.blankj.androidutilcode.R;
import com.blankj.androidutilcode.activity.FragmentActivity;
import com.blankj.androidutilcode.base.BaseFragment;
import com.blankj.androidutilcode.transition.DetailTransition;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ToastUtils;
import java.util.Random;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 17/02/02
* desc :
* </pre>
*/
public class Demo0Fragment extends BaseFragment
implements FragmentUtils.OnBackClickListener {
private Demo0Fragment demo0Fragment;
private FragmentUtils.SharedElement sharedElement;
Button btnShowAboutFragment;
ImageView ivSharedElement;
TextView tvAboutFragment;
public static Demo0Fragment newInstance() {
Bundle args = new Bundle();
Demo0Fragment fragment = new Demo0Fragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void initData(Bundle bundle) {
}
@Override
public int bindLayout() {
return R.layout.fragment_demo0;
}
@Override
public void initView(Bundle savedInstanceState, View view) {
Random random = new Random();
FragmentUtils.setBackgroundColor(this, Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
btnShowAboutFragment = (Button) view.findViewById(R.id.btn_show_about_fragment);
btnShowAboutFragment.setOnClickListener(this);
view.findViewById(R.id.btn_add_hide).setOnClickListener(this);
view.findViewById(R.id.btn_add_show).setOnClickListener(this);
view.findViewById(R.id.btn_add_child).setOnClickListener(this);
view.findViewById(R.id.btn_pop_to_root).setOnClickListener(this);
view.findViewById(R.id.btn_pop_add).setOnClickListener(this);
view.findViewById(R.id.btn_hide_show).setOnClickListener(this);
view.findViewById(R.id.btn_replace).setOnClickListener(this);
ivSharedElement = (ImageView) view.findViewById(R.id.iv_shared_element);
tvAboutFragment = (TextView) view.findViewById(R.id.tv_about_fragment);
demo0Fragment = this;
sharedElement = new FragmentUtils.SharedElement(ivSharedElement, getString(R.string.fragment_transition));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setExitTransition(new Fade());
}
}
@Override
public void doBusiness(Context context) {
}
@Override
public void onWidgetClick(View view) {
tvAboutFragment.setText("");
switch (view.getId()) {
case R.id.btn_show_about_fragment:
tvAboutFragment.setText("lastAdd: " + FragmentUtils.getLastAddFragment(getFragmentManager()).getClass().getSimpleName()
+ "\nlastAddInStack: " + (FragmentUtils.getLastAddFragmentInStack(getFragmentManager()) != null ? FragmentUtils.getLastAddFragmentInStack(getFragmentManager()).getClass().getSimpleName() : "null")
+ "\ntopShow: " + (FragmentUtils.getTopShowFragment(getFragmentManager()) != null ? FragmentUtils.getTopShowFragment(getFragmentManager()).getClass().getSimpleName() : "null")
+ "\ntopShowInStack: " + (FragmentUtils.getTopShowFragmentInStack(getFragmentManager()) != null ? FragmentUtils.getTopShowFragmentInStack(getFragmentManager()).getClass().getSimpleName() : "null")
+ "\n---all of fragments---\n"
+ FragmentUtils.getAllFragments(getFragmentManager()).toString()
+ "\n----------------------\n\n"
+ "---stack top---\n"
+ FragmentUtils.getAllFragmentsInStack(getFragmentManager()).toString()
+ "\n---stack bottom---\n\n"
);
break;
case R.id.btn_add_hide:
FragmentUtils.hideAddFragment(getFragmentManager(),
demo0Fragment,
addSharedElement(Demo1Fragment.newInstance()),
R.id.fragment_container,
false,
true,
sharedElement);
break;
case R.id.btn_add_show:
FragmentUtils.addFragment(getFragmentManager(),
addSharedElement(Demo1Fragment.newInstance()),
R.id.fragment_container,
false,
false,
sharedElement);
break;
case R.id.btn_add_child:
FragmentUtils.addFragment(getChildFragmentManager(),
Demo2Fragment.newInstance(),
R.id.child_fragment_container,
false,
true);
break;
case R.id.btn_pop_to_root:
FragmentUtils.popToFragment(getFragmentManager(),
Demo1Fragment.class,
true);
break;
case R.id.btn_pop_add:
FragmentUtils.popAddFragment(getFragmentManager(),
addSharedElement(Demo2Fragment.newInstance()),
R.id.fragment_container,
true,
sharedElement);
break;
case R.id.btn_hide_show:
Fragment fragment1 = FragmentUtils.findFragment(getFragmentManager(), Demo1Fragment.class);
if (fragment1 != null) {
FragmentUtils.hideShowFragment(this, fragment1);
} else {
ToastUtils.showLong("please add demo1 first!");
}
break;
case R.id.btn_replace:
((FragmentActivity) getActivity()).rootFragment = FragmentUtils.replaceFragment(this, addSharedElement(Demo3Fragment.newInstance()), false, sharedElement);
break;
}
}
private Fragment addSharedElement(Fragment fragment) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
fragment.setSharedElementEnterTransition(new DetailTransition());
fragment.setEnterTransition(new Fade());
fragment.setSharedElementReturnTransition(new DetailTransition());
}
return fragment;
}
@Override
public boolean onBackClick() {
// FragmentUtils.popToFragment(getFragmentManager(), Demo1Fragment.class, true);
return false;
}
}