Fragment跳转
- 开启碎片事务
- 添加,删除,替换碎片
1 | MPFragment mpFragment= new MPFragment(); |
- 添加到返回栈中(可选)
1 | transaction.addToBackStack(null); |
- 提交事务,commit()!!!!!
Fragment跳转Fragment
1 |
|
Activity舔砖Fragment
1 | FragmentManager fragmentManager = getSupportFragmentManager(); |
Frament传值
Acitivity传值给Fragment,同Activity下Fragment传值给Fragment
- 直接在Activity中添加全局变量,并设置getter和setter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16private Map<String,Double> percentMap;
public class MainAcitvity{
...
...
public Map<String, Double> getPercentMap() {
return percentMap;
}
public void setPercentMap(Map<String, Double> percentMap) {
this.percentMap = percentMap;
}
}
Fragment传值给Activity
- 在Fragment中写个内部接口
- 让宿主去实现该接口
- 在Fragment事件中强转宿主为该接口变为实例,返回给宿主
- 宿主实现接口中方法,获得结果。
1 | //Fragment部分 |
Fragment传值给Fragment(方法2)
- 为了防止混淆,这个方法和上面基本一致,仔细阅读便可发现.
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//Fragment部分
public class LoginDialogFragment extends DialogFragment{
private NumberPicker np_integer;
private NumberPicker np_decimal;
public interface LoginInputListener
{
void onLoginInputComplete(float result);
}
...
...
public void onClick(DialogInterface dialog, int id)
{
LoginInputListener listener = (LoginInputListener) getActivity();
float result= np_integer.getValue() + np_decimal.getValue()*1.0f/10;
listener.onLoginInputComplete(result);
}
}
//Activity部分
public class MainActivity implements LoginInputListener{
...
...
public void onLoginInputComplete(float result) {
Toast.makeText(this, "輸入的數字是:" + result,
Toast.LENGTH_SHORT).show();
}
}