简介:
为什么要用Fragment?使用Fragment可以在一个Activity中实现不同的界面。Fragment与Fragment之间的动画切换,远比Activity与Activity之间的动画切换变化方式多。很多时候,我们通过使用一个Activity,切换多个Fragment。本次博客,主要列举一下Fragment与它的Activity之间进行数据交换的方式。
1.Fragment中通过getActivity()然后进行强制转化,调用Activity中的公有方法
((XXXXActivity)getActivity()).fun();
2.Activity在切换Fragment的时候,通过setArguments向Fragment传递参数,Fragment通过getArguments();获得从activity中传递过来的值
3.Activity实现一个接口,Fragment在onAttach方法中,将该Activity转化为该接口,在需要调用的时候回调。
注意:本Demo是通过FragmentManager来管理Fragment的,通过FragmentManager管理,我们创建Fragment和销毁Fragment的时候,可以通过栈的方式:
a.FragmentTransaction的add方法,添加一个Fragment
b.FragmentTransaction的popBackStack()弹出该Fragment
演示实例:
fragment1.xml
[html]
view plain
copy
1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2. xmlns:tools="http://schemas.android.com/tools"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:background="#FFFFFFFF"
6. android:paddingBottom="@dimen/activity_vertical_margin"
7. android:paddingLeft="@dimen/activity_horizontal_margin"
8. android:paddingRight="@dimen/activity_horizontal_margin"
9. android:paddingTop="@dimen/activity_vertical_margin"
10. tools:context="com.example.testfragment.MainActivity$PlaceholderFragment" >
11. <TextView
12. android:id="@+id/tv"
13. android:layout_width="wrap_content"
14. android:layout_height="wrap_content"
15. android:layout_alignParentTop="true"
16. android:text="fragment1" />
17. <Button
18. android:id="@+id/btn"
19. android:layout_width="wrap_content"
20. android:layout_height="wrap_content"
21. android:layout_below="@id/tv"
22. android:text="跳转到Fragment2" />
23. </RelativeLayout>
MyFragment1.java
[java]
view plain
copy
1. /*
2. * $filename: MyFragment.java,v $
3. * $Date: 2014-5-16 $
4. * Copyright (C) ZhengHaibo, Inc. All rights reserved.
5. * This software is Made by Zhenghaibo.
6. */
7. package com.example.testfragment;
8.
9. import android.app.Activity;
10. import android.os.Bundle;
11. import android.support.v4.app.Fragment;
12. import android.view.LayoutInflater;
13. import android.view.View;
14. import android.view.View.OnClickListener;
15. import android.view.ViewGroup;
16. import android.widget.Button;
17.
18. /*
19. *@author: ZhengHaibo
20. *web:
21. *mail: zhb931706659@126.com
22. *2014-5-16 Nanjing,njupt,China
23. */
24. public class MyFragment1 extends Fragment {
25.
26. null;
27. Button btn;
28. @Override
29. public View onCreateView(LayoutInflater inflater, ViewGroup container,
30. Bundle savedInstanceState) {
31. View rootView = inflater.inflate(R.layout.fragment1, container,
32. false);
33. btn = (Button)rootView.findViewById(R.id.btn);
34. new OnClickListener() {
35.
36. @Override
37. public void onClick(View v) {
38. // TODO Auto-generated method stub
39. null);
40. }
41. });
42. return rootView;
43. }
44.
45. @Override
46. public void onAttach(Activity activity) {
47. // TODO Auto-generated method stub
48. super.onAttach(activity);
49. fragmentCallBack = (MainActivity)activity;
50. }
51. }
fragment2.xml
[html]
view plain
copy
1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2. xmlns:tools="http://schemas.android.com/tools"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:background="#FFFFFFFF"
6. android:paddingBottom="@dimen/activity_vertical_margin"
7. android:paddingLeft="@dimen/activity_horizontal_margin"
8. android:paddingRight="@dimen/activity_horizontal_margin"
9. android:paddingTop="@dimen/activity_vertical_margin"
10. tools:context="com.example.testfragment.MainActivity$PlaceholderFragment" >
11. <TextView
12. android:id="@+id/tv"
13. android:layout_width="wrap_content"
14. android:layout_height="wrap_content"
15. android:layout_alignParentTop="true"
16. android:text="fragment2" />
17. <Button
18. android:id="@+id/btn1"
19. android:layout_width="wrap_content"
20. android:layout_height="wrap_content"
21. android:layout_below="@id/tv"
22. android:text="直接调用Activity" />
23. <Button
24. android:id="@+id/btn2"
25. android:layout_width="wrap_content"
26. android:layout_height="wrap_content"
27. android:layout_below="@id/btn1"
28. android:text="回调Activity" />
29. </RelativeLayout>
MyFragment2.java
[java]
view plain
copy
1. /*
2. * $filename: MyFragment.java,v $
3. * $Date: 2014-5-16 $
4. * Copyright (C) ZhengHaibo, Inc. All rights reserved.
5. * This software is Made by Zhenghaibo.
6. */
7. package com.example.testfragment;
8.
9. import android.app.Activity;
10. import android.os.Bundle;
11. import android.support.v4.app.Fragment;
12. import android.view.LayoutInflater;
13. import android.view.View;
14. import android.view.View.OnClickListener;
15. import android.view.ViewGroup;
16. import android.widget.Button;
17. import android.widget.Toast;
18.
19. /*
20. *@author: ZhengHaibo
21. *web:
22. *mail: zhb931706659@126.com
23. *2014-5-16 Nanjing,njupt,China
24. */
25. public class MyFragment2 extends Fragment {
26.
27. null;
28. Button btn1;
29. Button btn2;
30. @Override
31. public View onCreateView(LayoutInflater inflater, ViewGroup container,
32. Bundle savedInstanceState) {
33. View rootView = inflater.inflate(R.layout.fragment2, container,
34. false);
35. //获得从activity中传递过来的值
36. "TEXT"), Toast.LENGTH_SHORT).show();
37. btn1 = (Button)rootView.findViewById(R.id.btn1);
38. new OnClickListener() {
39.
40. @Override
41. public void onClick(View v) {
42. // 直接调用Activity中的方法
43. ((MainActivity)getActivity()).changeButtonColor();
44. }
45. });
46. btn2 = (Button)rootView.findViewById(R.id.btn2);
47. new OnClickListener() {
48.
49. @Override
50. public void onClick(View v) {
51. // 回调的方式
52. null);
53. }
54. });
55. return rootView;
56. }
57.
58. @Override
59. public void onAttach(Activity activity) {
60. // TODO Auto-generated method stub
61. super.onAttach(activity);
62. fragmentCallBack = (MainActivity)activity;
63. }
64. }
回调接口:
[java]
view plain
copy
1. /*
2. * $filename: FragmentCallBack.java,v $
3. * $Date: 2014-5-16 $
4. * Copyright (C) ZhengHaibo, Inc. All rights reserved.
5. * This software is Made by Zhenghaibo.
6. */
7. package com.example.testfragment;
8.
9. import android.os.Bundle;
10.
11. /*
12. *@author: ZhengHaibo
14. *mail: zhb931706659@126.com
15. *2014-5-16 Nanjing,njupt,China
16. */
17. public interface FragmentCallBack {
18. public void callbackFun1(Bundle arg);
19.
20. public void callbackFun2(Bundle arg);
21. }
main.xml
[html]
view plain
copy
MainActivity.java
1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2. xmlns:tools="http://schemas.android.com/tools"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:paddingBottom="@dimen/activity_vertical_margin"
6. android:paddingLeft="@dimen/activity_horizontal_margin"
7. android:paddingRight="@dimen/activity_horizontal_margin"
8. android:paddingTop="@dimen/activity_vertical_margin" >
9. <Button
10. android:id="@+id/btn"
11. android:layout_width="match_parent"
12. android:layout_height="wrap_content"
13. android:layout_alignParentBottom="true"
14. android:text="切换" />
15. <RelativeLayout
16. android:id="@+id/rl_container"
17. android:layout_width="match_parent"
18. android:layout_height="wrap_content"
19. android:layout_above="@id/btn" />
20. </RelativeLayout>
[java]
view plain
copy
1. package com.example.testfragment;
2.
3. import android.graphics.Color;
4. import android.os.Bundle;
5. import android.support.v4.app.Fragment;
6. import android.support.v4.app.FragmentManager;
7. import android.support.v4.app.FragmentTransaction;
8. import android.support.v7.app.ActionBarActivity;
9. import android.view.View;
10. import android.view.View.OnClickListener;
11. import android.widget.Button;
12.
13. public class MainActivity extends ActionBarActivity implements FragmentCallBack{
14.
15. private Button btn;
16.
17. private MyFragment1 fragment1;
18. private MyFragment2 fragment2;
19. private FragmentManager fragmentManager;
20. private Fragment currentFragment;
21. @Override
22. protected void onCreate(Bundle savedInstanceState) {
23. super.onCreate(savedInstanceState);
24. setContentView(R.layout.activity_main);
25. fragmentManager = getSupportFragmentManager();
26.
27. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
28. fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
29. new MyFragment1();
30. new Bundle();
31. "TEXT", "这是Activiy通过Bundle传递过来的值");
32. //通过Bundle向Activity中传递值
33. //将fragment1设置到布局上
34. null);
35. fragmentTransaction.commitAllowingStateLoss();
36. currentFragment = fragment1;
37. //初始化button控件
38. btn = (Button)findViewById(R.id.btn);
39. new OnClickListener() {
40. @Override
41. public void onClick(View v) {
42. if(currentFragment instanceof MyFragment1){
43. switchFragment();
44. else{//当前是fragment2,因此,只需要将fragment2出栈即可变成fragment1
45. fragmentManager.popBackStack();
46. currentFragment = fragment1;
47. }
48. }
49. });
50. }
51. /**
52. * 切换Fragment
53. */
54. private void switchFragment(){
55. if(null == fragment2){//可以避免切换的时候重复创建
56. new MyFragment2();
57. }
58. new Bundle();
59. "TEXT", "传递给fragment2");
60. fragment2.setArguments(data);
61. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
62. fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
63. fragmentTransaction.add(R.id.rl_container,fragment2);
64. null);
65. fragmentTransaction.commitAllowingStateLoss();
66. currentFragment = fragment2;
67. }
68.
69. public void changeButtonColor(){
70. btn.setBackgroundColor(Color.RED);
71. }
72.
73. @Override
74. public void callbackFun1(Bundle arg) {
75. // TODO Auto-generated method stub
76. //通过回调方式切换
77. }
78.
79. @Override
80. public void callbackFun2(Bundle arg) {
81. // TODO Auto-generated method stub
82. //通过回调方式调用Activity中的方法
83. }
84.
85. }
初始画面
切换到第二个Fragment之后,通过Fragment2回调,改变按钮背景后的截图。
注意:
1.直接在Fragment中通过getActivity然后强转Activity的方式调用Activity的方法,这个方式不推荐!因为这会使Fragment的适配性变差。
解决方法:在使用之前,使用instanceof 判断一下Activity的类型
2.FragmentTransaction通过使用setCustomAnimations方法,可以为Fragment的切换增添各种不同的动画。变化方式远比Activity与Activity之间的切换动画要多。
3.多个Fragment之间,可以通过Activity复用很多代码,提高效率。
4.我们还可以通过ViewPager来管理Fragment,通过Adapter添加多个Fragment,然后通过setcurrentitem进行切换。我们同样可以通过setArguments向Fragment传递数据。