亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問如何將一些數據傳輸到另一個片段?

請問如何將一些數據傳輸到另一個片段?

C++
繁星點點滴滴 2019-10-16 13:09:19
如何將一些數據傳輸到另一個片段?如何將一些數據傳輸到另一個Fragment同樣地,它也是用extras為intents?
查看完整描述

3 回答

?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

Bundle..下面是一個例子:

Fragment fragment = new Fragment();Bundle bundle = new Bundle();bundle.putInt(key, value);fragment.setArguments(bundle);

bundle為許多數據類型提供了方法??匆?/trans>這,這個

然后在你的Fragment,檢索數據(例如,在onCreate()方法)與:

Bundle bundle = this.getArguments();if (bundle != null) {
        int myInt = bundle.getInt(key, defaultValue);}



查看完整回答
反對 回復 2019-10-17
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

為了擴展前面的答案,就像Ankit所說的,對于復雜的對象,您需要實現Serialable。例如,對于簡單對象:

public class MyClass implements Serializable {
    private static final long serialVersionUID = -2163051469151804394L;
    private int id;
    private String created;}

在你身上:

Bundle args = new Bundle();args.putSerializable(TAG_MY_CLASS, myClass);Fragment toFragment = new ToFragment();
toFragment.setArguments(args);getFragmentManager()
    .beginTransaction()
    .replace(R.id.body, toFragment, TAG_TO_FRAGMENT)
    .addToBackStack(TAG_TO_FRAGMENT).commit();

在你的房間里:

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

    Bundle args = getArguments();
    MyClass myClass = (MyClass) args        .getSerializable(TAG_MY_CLASS);



查看完整回答
反對 回復 2019-10-17
?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

使用片段到片段傳遞數據的完整代碼

Fragment fragment = new Fragment(); // replace your custom fragment class Bundle bundle = new Bundle();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                bundle.putString("key","value"); // use as per your need
                fragment.setArguments(bundle);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.replace(viewID,fragment);
                fragmentTransaction.commit();

在自定義片段類中

Bundle mBundle = new Bundle();mBundle = getArguments();mBundle.getString(key);  
// key must be same which was given in first fragment



查看完整回答
反對 回復 2019-10-17
  • 3 回答
  • 0 關注
  • 472 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號