少年游

欲买桂花同载酒,终不似,少年游。

0%

android之Fragment跳转与传值

Fragment跳转

  • 开启碎片事务
  • 添加,删除,替换碎片
1
2
3
4
MPFragment mpFragment= new MPFragment();
transaction.replace(R.id.content,mpFragment);
transaction.add(R.id.content,mpFragment);
transaction.remove(R.id.content,mpFragment);
  • 添加到返回栈中(可选)
1
transaction.addToBackStack(null);
  • 提交事务,commit()!!!!!

Fragment跳转Fragment

1
2
3
4
5
6
7

MPFragment mpFragment= new MPFragment();
FragmentTransaction transaction =getFragmentManager().beginTransaction();
transaction.replace(R.id.content,mpFragment);
transaction.addToBackStack(null);
transaction.commit();

Activity舔砖Fragment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
DietFragment dietFragment;
...
...
if (dietFragment == null) {
// 如果Fragment为空,则创建一个并添加到界面上
dietFragment = new DietFragment();
transaction.add(R.id.content, dietFragment);
} else {
// 如果Fragment不为空,则直接将它显示出来
transaction.show(dietFragment);
}
transaction.commit();

Frament传值

Acitivity传值给Fragment,同Activity下Fragment传值给Fragment

  • 直接在Activity中添加全局变量,并设置getter和setter
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    private 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
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);
}

...
...

@Override
public void onClick(DialogInterface dialog, int id)
{
LoginInputListener listener = (LoginInputListener) getTargetFragment();
float result= np_integer.getValue() + np_decimal.getValue()*1.0f/10;
listener.onLoginInputComplete(result);
}

}

//Activity部分


public class MainActivity implements LoginInputListener{

...
...

@Override
public void onLoginInputComplete(float result) {
Toast.makeText(this, "輸入的數字是:" + result,
Toast.LENGTH_SHORT).show();

}

}

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);
    }

    ...
    ...

    @Override
    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{

    ...
    ...

    @Override
    public void onLoginInputComplete(float result) {
    Toast.makeText(this, "輸入的數字是:" + result,
    Toast.LENGTH_SHORT).show();

    }

    }