Android 之Fragment

Android 之 Fragment(三)


Fragment與Activity的消息傳遞

1. 從Activity將消息傳遞到Fragment中:

在Activity中創建Bundle數據包,並調用Fragment的setArguments()方法

//Activity_main.java 中的onCreate()方法
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //在Activity中給Button設置事件監聽
    findViewById(R.id.activity_main_btn).setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
            //獲取編輯框的輸入數據
               String text = ((EditText)findViewById(R.id.activity_main_et)).getText().toString();
               Fragment_main fragment = new Fragment_main(); //動態創建自己定義的Fragment_main的對象
               Bundle bundle = new Bundle(); //創建Bundle數據包對象
               bundle.putString("message", text); //向Bundle數據包中放入編輯框中的數據
               fragment.setArguments(bundle);//將數據傳遞給自定義的Fragment_main對象

              //獲取系統的FragmentManager的FragmentTransaction事物
               FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();

               beginTransaction.add(R.id.activity_main_layout, fragment, "fragment");//向事物中添加Fragment

               beginTransaction.commit();//向Activity提交事物
               Toast.makeText(Activity_main.this, "向Fragment發送" + text + " 消息", Toast.LENGTH_SHORT).show();
           }
       });
}



//Fragment_main.java中的onCreateView()方法
public View onCreateView(LayoutInflater inflater, ViewGroup conainer, Bundle savedInstanceState) {
    //解析Fragment的Layout文件
    View view = inflater.inflate(R.layout.fragment_main, conainer, false);
    //綁定Fragment的TextView控件
    textView = (TextView) view.findViewById(R.id.fragment_main_tv);

    //獲取從Activity傳遞過來數據包中的數據
    String message = getArguments().get("message") + "";
    textView.setText(message); //設置到TextView上

    Toast.makeText(getActivity(), "收到來自Activity的消息:" + message, Toast.LENGTH_SHORT).show();
    return view;
}

2. 從Fragment將消息傳遞到Activity中:

在Fragment中定義一個內部回調接口,在讓包含該Fragment的Activity實現該回調接口。這樣Fragment可調用該回調方法將數據傳遞給Activity

public class Fragment_main extends Fragment {
    private TextView textView;
    private SendMessage sendMessage; //創建回調接口的對象
    private String code = "Fragment向Activity發送消息.....";

    //在Fragment中創建一個回調接口
    public interface SendMessage { 
        void send(String code);
    }

    @Override
    public void onAttach(Context context) {
        sendMessage  = (SendMessage) context; //實例化回調接口對象
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup conainer, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_main, conainer, false);

        textView = (TextView) view.findViewById(R.id.fragment_main_tv);
        String message = getArguments().get("message") + "";
        textView.setText(message);

        Toast.makeText(getActivity(), "收到來自Activity的消息:" + message, Toast.LENGTH_SHORT).show();

        Toast.makeText(getActivity(), code, Toast.LENGTH_SHORT).show();
        sendMessage.send(code); //向Activity傳遞數據
        return view;
    }
}

//讓Activity_main實現Fragment_main中的回調接口
public class Activity_main extends Activity implements SendMessage{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.activity_main_btn).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = ((EditText)findViewById(R.id.activity_main_et)).getText().toString();
                Fragment_main fragment = new Fragment_main();
                Bundle bundle = new Bundle();
                bundle.putString("message", text);
                fragment.setArguments(bundle);

                FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();
                beginTransaction.add(R.id.activity_main_layout, fragment, "fragment");
                beginTransaction.commit();
                Toast.makeText(Activity_main.this, "向Fragment發送" + text + " 消息", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //實現回調接口中的方法,直接獲取數據
    public void send(String code) {
        Toast.makeText(this, "收到來自Fragment的消息:" + code, Toast.LENGTH_SHORT).show();
    }
}

You May Also Like