1 //1,fragment静态加载:MainActivity MainActivty1_2 Myfragment1 2 //2,fragment动态加载 :MainActivity Myfragment2 3 //3,fragment生命周期: Myfragment4_life 4 //4,fragment和:Activity之间通信:::MainActivity4 Myfragment5 5 public class MainActivity extends Activity implements OnCheckedChangeListener { 6 private RadioGroup group; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) {10 super.onCreate(savedInstanceState);11 setContentView(R.layout.main);12 group = (RadioGroup) findViewById(R.id.radioGroup1);13 group.setOnCheckedChangeListener(this);14 15 } 16 17 @Override18 public void onCheckedChanged(RadioGroup group, int checkedId) {19 // TODO Auto-generated method stub20 switch (checkedId) {21 case R.id.radio0: {22 //静态加载fragment:23 //第一个Activity跳转Activity1_2(xml布局中是含有android:name="com.example.fragment.Myfragment1"的fragment标签)24 //Myfragment1类继承fragment25 //Myfragment1类中的xml布局含有TextView和Button26 Intent intent = new Intent(MainActivity.this, MainActivty1_2.class);27 startActivity(intent);28 break;29 }30 //动态加载fragment:31 case R.id.radio1: {32 //创建Myfragment2类继承fragment33 //Myfragment2类中的xml布局含有TextView和Button34 //把fragment2添加到main中的xml中的 LinearLayout布局(id=R.id.frame)35 Myfragment2 fragment2 = new Myfragment2();36 FragmentManager fragmentManager = getFragmentManager();37 FragmentTransaction beginTransaction = fragmentManager.beginTransaction();38 beginTransaction.add(R.id.frame, fragment2);//把fragment2添加到拿个布局39 beginTransaction.addToBackStack(null);//设置物理连接返回键40 beginTransaction.commit();41 42 break;43 }44 case R.id.radio2: {45 46 break;47 }48 49 case R.id.radio3: {50 Intent intent = new Intent(MainActivity.this, MainActivity4.class);51 startActivity(intent);52 53 break;54 }55 56 }57 }58 59 }
1 public class MainActivty1_2 extends Activity { 2 3 private TextView textView; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 // TODO Auto-generated method stub 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.main2);10 Button button = (Button) findViewById(R.id.but);11 button.setText("改变");12 textView = (TextView) findViewById(R.id.text);13 button.setOnClickListener(new OnClickListener() {14 15 @Override16 public void onClick(View v) {17 // TODO Auto-generated method stub18 textView.setText("textView改变了");19 }20 });21 }22 }
public class Fragment1 extends android.support.v4.app.Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.view1, container, false); }}
1 public class Myfragment2 extends Fragment{ 2 3 4 @Override 5 public View onCreateView(LayoutInflater inflater, ViewGroup container, 6 Bundle savedInstanceState) { 7 // TODO Auto-generated method stub 8 //resource:需要加载的布局文件 9 //root:layout的viewGrup10 // attachToRoot:false 不返回父viewGrup11 View view = inflater.inflate(R.layout.fragment, container, false);12 TextView textView=(TextView) view.findViewById(R.id.text);13 textView.setText("动态加载fragment");14 return view;15 }16 17 }
、、//--------------------生命周期--------------------
1 public class Myfragment4_life extends Fragment { 2 3 // 1.当Fragment被添加到Activity时候会回调这个方法,并且只调用一次 4 @Override 5 public void onAttach(Activity activity) { 6 // TODO Auto-generated method stub 7 super.onAttach(activity); 8 } 9 10 // 2.创建fragment时会回调,只调用一次11 @Override12 public void onCreate(Bundle savedInstanceState) {13 // TODO Auto-generated method stub14 super.onCreate(savedInstanceState);15 }16 17 // 3.每次创建都会绘制Fragment的view组件时回调该方法18 @Override19 public View onCreateView(LayoutInflater inflater, ViewGroup container,20 Bundle savedInstanceState) {21 // TODO Auto-generated method stub22 View view = inflater.inflate(R.layout.fragment, container, false);23 TextView textView = (TextView) view.findViewById(R.id.text);24 25 return view;26 }27 28 // 4.当fragment所在的Activity启动完成后调用29 @Override30 public void onActivityCreated(Bundle savedInstanceState) {31 // TODO Auto-generated method stub32 super.onActivityCreated(savedInstanceState);33 }34 35 // 5.进入启动fragment36 @Override37 public void onStart() {38 // TODO Auto-generated method stub39 super.onStart();40 }41 42 // 6.恢复fragment时会被调用,调用onStart()方法后面一定会调用onResume()43 @Override44 public void onResume() {45 // TODO Auto-generated method stub46 super.onResume();47 }48 49 // 7.暂停fragment时会被调用50 @Override51 public void onPause() {52 // TODO Auto-generated method stub53 super.onPause();54 }55 56 // 8.停止fragment时会被调用57 @Override58 public void onStop() {59 // TODO Auto-generated method stub60 super.onStop();61 }62 63 // 9.销毁fragment所包含的view组件时64 @Override65 public void onDestroyView() {66 // TODO Auto-generated method stub67 super.onDestroyView();68 }69 70 // 10.销毁fragment时会被调用71 @Override72 public void onDestroy() {73 // TODO Auto-generated method stub74 super.onDestroy();75 }76 77 // 11.fragment从activity中删除时会回调该方法。只调用一次78 @Override79 public void onDetach() {80 // TODO Auto-generated method stub81 super.onDetach();82 }83 }
//---------------------fragment与activity通信----------------------------
// Fragment与Activity通信// (1)Fragment可以调用getActivity()方法获取它所在的Activity;// (2)Activity可以调用FragmentManager的findFragmentById()// 或者findFragmentByTag()方法获取Fragment。// Activity --> Fragment:在Activity中创建Bundle数据包,// 并调用Fragment的setArguments(Bundle bundle)方法,// 然后在Fragment中使用getArguments()获取Bundle数据包。// Fragment --> Activity:需要在Fragment中定义一个内部回调接口,// 再让包含该Fragment的Activity实现该回调接口。// 这样Fragment可调用该回调方法将数据传递给Activity。public class MainActivity4 extends Activity implements MyListener { private Button button; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main4); button = (Button) findViewById(R.id.send); editText = (EditText) findViewById(R.id.editText); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String text = editText.getText().toString().trim(); Myfragment5 fMyfragment5 = new Myfragment5();// Activity --> Fragment:在Activity中创建Bundle数据包,// 并调用Fragment的setArguments(Bundle bundle)方法,// 然后在Fragment中使用getArguments()获取Bundle数据包。 Bundle bundle = new Bundle(); bundle.putString("name", text); fMyfragment5.setArguments(bundle); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction beginTransaction = fragmentManager.beginTransaction(); beginTransaction.add(R.id.layout, fMyfragment5, "fragment5"); beginTransaction.commit(); Toast.makeText(MainActivity4.this, "向fragment发送数据"+text, 1).show(); } }); // 静态加载 FragmentManager fragmentManager = getFragmentManager(); Fragment findFragmentById = fragmentManager.findFragmentById(R.id.f); Myfragment1 frag = (Myfragment1) findFragmentById; frag.setAa("xxxxxxxxxxxx静态传值xxx"); } @Override public void thank(String code) { // TODO Auto-generated method stub Toast.makeText(MainActivity4.this, "成功接收到。。"+code, 1).show(); }}
1 public class Myfragment5 extends Fragment { 2 private String code = "thank you"; 3 public MyListener listener; 4 // Fragment --> Activity:需要在Fragment中定义一个内部回调接口, 5 // 再让包含该Fragment的Activity实现该回调接口。class MainActivity4 extends Activity implements MyListener 6 // 这样Fragment可调用该回调方法将数据传递给Activity。 7 public interface MyListener { 8 public void thank(String code); 9 10 }11 12 // 1.当Fragment被添加到Activity时候会回调这个方法,并且只调用一次13 @Override14 public void onAttach(Activity activity) {15 // TODO Auto-generated method stub16 listener = (MyListener) activity;17 super.onAttach(activity);18 }19 //-------------------------------接收activity传值-开始--------------------20 @Override21 public View onCreateView(LayoutInflater inflater, ViewGroup container,22 Bundle savedInstanceState) {23 // TODO Auto-generated method stub24 View view = inflater.inflate(R.layout.fragment, container, false);25 TextView textView = (TextView) view.findViewById(R.id.text);26 //获取Bundle()传过来的数据包27 String text = getArguments().get("name") + "";28 textView.setText(text);29 Toast.makeText(getActivity(), "已成功接收" + text, 1).show();30 31 //---------------------------------接收结束------------------- 32 33 Toast.makeText(getActivity(), "向ACtivity发送" + code, 1).show();34 35 listener.thank(code);36 37 return view;38 }39 }
Fragment与Activity通信(1)Fragment可以调用getActivity()方法获取它所在的Activity;(2)Activity可以调用FragmentManager的findFragmentById()或者findFragmentByTag()方法获取Fragment。Activity --> Fragment:在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法,然后在Fragment中使用getArguments()获取Bundle数据包。Fragment --> Activity:需要在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口。这样Fragment可调用该回调方法将数据传递给Activity。
将数据(这里是字符串 code) 从Fragment 传给 Activity:1.定义一个接口 MyListener,里面定义一个无方法体的方法thank( String str);2.创建 MyListener对象 listener;3.重写方法 onAttach(Activity activity);将activity强制转换为MyListener并赋值给 listener;4.在方法 onCreateView中写上 listener.thank(code); 实际上就是把数据 code 传递给了 activity!