现在的位置: 首页 > 综合 > 正文

Fragment之Creating event callbacks to activity实例

2018年04月22日 ⁄ 综合 ⁄ 共 7103字 ⁄ 字号 评论关闭

在看Android帮助文档的时候,会出现一些例子,但是例子总是穿插着多种知识,让还没有系统学过Android的人读起来很费神难懂,所以就自己写了一个creating event callbacks to activity的例子.

我把Activity的UI分为两个部分,左边和右边,左边用来放置点击的按钮(LeftFragment),右边用来放置对应点击后显示的信息(RightFragment).

Activity的布局layout文件:main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/left_layout"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="fill_parent"  
  11.         android:layout_weight="1"  
  12.         android:orientation="vertical" >  
  13.     </LinearLayout>  
  14.   
  15.     <LinearLayout  
  16.         android:id="@+id/right_layout"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="fill_parent"  
  19.         android:layout_weight="10"  
  20.         android:orientation="vertical" >  
  21.     </LinearLayout>  
  22.   
  23. </LinearLayout>  

LeftFragment的布局layout:leftfragment.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/first_button"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/first_button" />  
  12.   
  13.     <Button  
  14.         android:id="@+id/second_button"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="@string/second_button" />  
  18.   
  19.     <Button  
  20.         android:id="@+id/third_button"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="@string/third_button" />  
  24.   
  25. </LinearLayout>  

RightFragment的布局layout:rightfragment.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/right_show_message"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:background="@android:color/holo_orange_dark"  
  12.         android:textColor="@android:color/white" />  
  13.   
  14. </LinearLayout>  

以上是两个fragment和一个Activity的布局文件,下面来看他们的java文件

Activity: 

  1. public class FirstActivity extends Activity implements MyListener  
  2. {  
  3.     /** 
  4.      * 实现MyListener,当LeftFragment中点击第一页的时候,让RightFragment显示第一页信息,同理当点击第二页的时候,RightFragment显示第二页信息 
  5.      *  
  6.      * @param index 
  7.      *            显示的页数 
  8.      */  
  9.     public void showMessage(int index)  
  10.     {  
  11.         if (1 == index)  
  12.             showMessageView.setText(R.string.first_page);  
  13.         if (2 == index)  
  14.             showMessageView.setText(R.string.second_page);  
  15.         if (3 == index)  
  16.             showMessageView.setText(R.string.third_page);  
  17.     }  
  18.   
  19.     /** 得到RightFragment中显示信息的控件 */  
  20.     private TextView showMessageView;  
  21.   
  22.     /** Called when the activity is first created. */  
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState)  
  25.     {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.         System.out.println("Activity--->onCreate");  
  29.   
  30.         FragmentManager manager = getFragmentManager();  
  31.         FragmentTransaction transaction = manager.beginTransaction();  
  32.         // 动态增加Fragment  
  33.         RightFragment rightFragment = new RightFragment();  
  34.         LeftFragment leftFragment = new LeftFragment();  
  35.         transaction.add(R.id.left_layout, leftFragment, "leftfragment");  
  36.         transaction.add(R.id.right_layout, rightFragment, "rightfragment");  
  37.         transaction.commit();  
  38.   
  39.     }  
  40.   
  41.     @Override  
  42.     protected void onResume()  
  43.     {  
  44.         super.onResume();  
  45.         System.out.println("Activity--->onResume");  
  46.         showMessageView = (TextView) findViewById(R.id.right_show_message);  
  47.     }  
  48. }  

LeftFragment:

  1. public class LeftFragment extends Fragment  
  2. {  
  3.     /** Acitivity要实现这个接口,这样Fragment和Activity就可以共享事件触发的资源了 */  
  4.     public interface MyListener  
  5.     {  
  6.         public void showMessage(int index);  
  7.     }  
  8.   
  9.     private MyListener myListener;  
  10.     private Button firstButton;  
  11.     private Button secondButton;  
  12.     private Button thirdButton;  
  13.   
  14.     /** Fragment第一次附属于Activity时调用,在onCreate之前调用 */  
  15.     @Override  
  16.     public void onAttach(Activity activity)  
  17.     {  
  18.         super.onAttach(activity);  
  19.         System.out.println("LeftFragment--->onAttach");  
  20.   
  21.         myListener = (MyListener) activity;  
  22.     }  
  23.   
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState)  
  26.     {  
  27.         super.onCreate(savedInstanceState);  
  28.         System.out.println("LeftFragment--->onCreate");  
  29.     }  
  30.   
  31.     @Override  
  32.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  
  33.     {  
  34.         System.out.println("LeftFragment--->onCreateView");  
  35.         return inflater.inflate(R.layout.leftfragment, container, false);  
  36.     }  
  37.   
  38.     @Override  
  39.     public void onResume()  
  40.     {  
  41.         super.onResume();  
  42.         System.out.println("LeftFragment--->onResume");  
  43.   
  44.         firstButton = (Button) getActivity().findViewById(R.id.first_button);  
  45.         secondButton = (Button) getActivity().findViewById(R.id.second_button);  
  46.         thirdButton = (Button) getActivity().findViewById(R.id.third_button);  
  47.   
  48.         MyButtonClickListener clickListener = new MyButtonClickListener();  
  49.         firstButton.setOnClickListener(clickListener);  
  50.         secondButton.setOnClickListener(clickListener);  
  51.         thirdButton.setOnClickListener(clickListener);  
  52.     }  
  53.   
  54.     /** 按钮的监听器 */  
  55.     class MyButtonClickListener implements OnClickListener  
  56.     {  
  57.         public void onClick(View v)  
  58.         {  
  59.             Button button = (Button) v;  
  60.             if (button == firstButton)  
  61.                 myListener.showMessage(1);  
  62.             if (button == secondButton)  
  63.                 myListener.showMessage(2);  
  64.             if (button == thirdButton)  
  65.                 myListener.showMessage(3);  
  66.         }  
  67.     }  
  68. }  

RightFragment:

  1. public class RightFragment extends Fragment  
  2. {  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState)  
  5.     {  
  6.         System.out.println("RightFragment--->onCreate");  
  7.         super.onCreate(savedInstanceState);  
  8.     }  
  9.   
  10.     @Override  
  11.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  
  12.     {  
  13.         System.out.println("RightFragment--->onCreateView");  
  14.         return inflater.inflate(R.layout.rightfragment, container, false);  
  15.     }  
  16. }  

注意,Fragment的生命周期和Activity生命周期之间的关系。在Activity里动态生成Fragment,首先是Activity调用onCreate()方法,但是这时候还没有加载到Fragment里的组件,当Fragment调用其onCreateView()方法后,Activity才能得到Fragment中的组件.以下可以看到它们的生命周期情况:

图:

这里最关键的就是Fragment要有一个接口和这个接口的引用,而这个接口需要Activity去实现它。当Fragment调用onAttach(Activity acitivity)方法的

抱歉!评论已关闭.