靜態加載Fragment,動態加載Fragment都學習了,不過寫得比較片面,只是一個簡單的用法,實際情況可能會複雜一些,但會用了再拓展一下應該不是很難。
純粹的加載頁面佈局了,當然是不夠了,這一節我們學習一下activity與fragment之間的數據互傳。
這一節內容也不好理解,建議是邊看邊理解,然後再敲,再理解。
首頁看看main_activity的佈局頁面:
嵌套linearLauyout是爲加載fragment用的, 下面的輸入框個按鈕的目的是:我們點擊按鈕,獲取輸入的內容,然後傳給fragment.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/main_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</LinearLayout>
<EditText
android:id="@+id/editText"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/sendButton"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="發送"
/>
</LinearLayout>
接下來看看MainActivity的處理:
先我們不看實現的接口,也不看接口實現的back方法 。
首先我們給佈局文件中的按鈕設置點擊事件,在點擊事件中,我們獲取到了文本框輸入的值,並動態加載了fragment
在動態加載中我們創建了一個bundle對象,我們將獲取到的輸入的值put到bundle對象中,然後再用setArgument將bundle對象放到fragment中,最後我們提交這個fragment事務,這樣我們就將值傳到了我們加載的fragment中了。
package com.example.thirdfragment;
import com.example.thirdfragment.Myfragment.BackListener;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements BackListener{
// 定義控件
private EditText editText;
private Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
editText = (EditText) findViewById(R.id.editText);
sendButton = (Button) findViewById(R.id.sendButton);
// 設置發送按鈕的點擊事件
sendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 獲取文本框輸入的數據
String text = editText.getText().toString();
// 獲取fragment對象
Myfragment fragment = new Myfragment();
// 創建一個bundle對象
Bundle bundle = new Bundle();
// 給值到創建的bundle對象
bundle.putString("text", text);
// 將帶值的bundle對象放入fragment中
fragment.setArguments(bundle);
// 動態加載fragment
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.main_layout, fragment);
fragmentTransaction.commit();
}
});
}
// 接口實現方法
@Override
public void back(String data) {
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}
}
activity已經將值傳到了fragment中,我們再看看fragment中怎麼樣獲取值的:
首先,我們只看onCreateView方法中的內容。
我們直接getArgument獲取了一個bundle對象,然後bundle對象直接get方法通過傳入的時候放的key獲取到值。
這樣就實現了activity向fragment傳值,並在fragment中接收值 。
然後我們看看fragment中怎麼向activity中傳值呢?
package com.example.thirdfragment;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Myfragment extends Fragment{
private String backData = "這是fragment返回的字符串";
private BackListener backListener;
public interface BackListener{
public void back(String data);
};
@Override
public void onAttach(Activity activity) {
backListener = (BackListener) activity;
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
// 接受activity傳遞到當前fragment的值
Bundle bundle = getArguments();
String text = bundle.get("text").toString();
// 將接收的值設置到頁面中
TextView textView = (TextView) view.findViewById(R.id.fragmentText);
textView.setText(text);
// 給接口設置返回給activity的值
backListener.back(backData);
return view;
}
}
我們建議是:在Fragment中定義一個內部回調接口,再讓activity實現該接口,我們通過接口將值傳給activity,並讓activity獲取到值。
具體的實現完美可以參考代碼:
1. 首先定義並初始化了回調的數據:
<span> </span>private String backData = "這是fragment返回的字符串";
2. 然後再定義了一個內部接口,並定義了一個接口中的一個回調方法,並給該接口定義了一個對象
<span> </span>private BackListener backListener;
<span> </span>public interface BackListener{
public void back(String data);
};
3. 接下來重寫onAttach方法,將fragment被加載到activity中的時候將會執行,把activity強轉成我們寫的內部接口並賦值給該接口。<span> </span>@Override
public void onAttach(Activity activity) {
backListener = (BackListener) activity;
super.onAttach(activity);
}
4. 我們將要回調的數據set到接口要回調的方法中
<span> </span>// 給接口設置返回給activity的值
backListener.back(backData);
通過以上四步呢我們就實現了frament想activity設值。。
那麼activity怎麼取值呢?
我們再回過頭去看看MainActivity.
1. 首先實現了在fragment中創建的內部接口BackListener
2. 實現了接口中的back方法,那麼參數中的data就是fragment傳給activity的值了 。