当前位置: 代码迷 >> Android >> android初学者学习笔记27-Fragment的简单使用
  详细解决方案

android初学者学习笔记27-Fragment的简单使用

热度:46   发布时间:2016-04-28 00:01:42.0
android菜鸟学习笔记27----Fragment的简单使用

1.Fragment的生命周期:

简单在新建一个MyFragment继承自Fragment,重写各个生命周期回调方法,各个方法中直接输出标识相关函数被调用的信息。

重写MainActivity的各个生命周期回调方法,同样输出标识信息。

MyFragment.java:

  1 public class MyFragment extends Fragment {  2   3       @Override  4   5       public void onActivityCreated(Bundle savedInstanceState) {  6   7            // TODO Auto-generated method stub  8   9            super.onActivityCreated(savedInstanceState); 10  11            Log.i("FRAGMENT","onActivityCreated()"); 12  13       } 14  15       @Override 16  17       public View onCreateView(LayoutInflater inflater, ViewGroup container, 18  19                  Bundle savedInstanceState) { 20  21            // TODO Auto-generated method stub 22  23            Log.i("FRAGMENT","onCreateView()"); 24  25            TextView textView = new TextView(getActivity()); 26  27            textView.setText("This is a fragment"); 28  29            return textView; 30  31       } 32  33       @Override 34  35       public void onDestroyView() { 36  37            // TODO Auto-generated method stub 38  39            super.onDestroyView(); 40  41            Log.i("FRAGMENT","onDestroyView()"); 42  43       } 44  45       @Override 46  47       public void onAttach(Activity activity) { 48  49            // TODO Auto-generated method stub 50  51            super.onAttach(activity); 52  53            Log.i("FRAGMENT","onAttach()"); 54  55       } 56  57       @Override 58  59       public void onCreate(Bundle savedInstanceState) { 60  61            // TODO Auto-generated method stub 62  63            super.onCreate(savedInstanceState); 64  65            Log.i("FRAGMENT","onCreate()"); 66  67       } 68  69       @Override 70  71       public void onStart() { 72  73             // TODO Auto-generated method stub 74  75            super.onStart(); 76  77            Log.i("FRAGMENT","onStart()"); 78  79       } 80  81       @Override 82  83       public void onResume() { 84  85            // TODO Auto-generated method stub 86  87            super.onResume(); 88  89            Log.i("FRAGMENT","onResume()"); 90  91       } 92  93       @Override 94  95       public void onPause() { 96  97            // TODO Auto-generated method stub 98  99            super.onPause();100 101            Log.i("FRAGMENT","onPause()");102 103       }104 105       @Override106 107       public void onStop() {108 109            // TODO Auto-generated method stub110 111            super.onStop();112 113            Log.i("FRAGMENT","onStop()");114 115       }116 117       @Override118 119       public void onDestroy() {120 121            // TODO Auto-generated method stub122 123            super.onDestroy();124 125            Log.i("FRAGMENT","onDestroy()");126 127       }128 129       @Override130 131       public void onDetach() {132 133            // TODO Auto-generated method stub134 135            super.onDetach();136 137            Log.i("FRAGMENT","onDetach()");138 139       }140 141 }

注意:onCreateView()方法中需返回一个View对象。

MainActivity.java:

 1 public class MainActivity extends ActionBarActivity { 2  3     @Override 4  5     protected void onCreate(Bundle savedInstanceState) { 6  7         super.onCreate(savedInstanceState); 8  9         setContentView(R.layout.activity_main);10 11         Log.i("ACTIVITY","onCreate()");12 13     }14 15       @Override16 17       protected void onStop() {18 19            // TODO Auto-generated method stub20 21            super.onStop();22 23            Log.i("ACTIVITY","onStop()");24 25       }26 27       @Override28 29       protected void onDestroy() {30 31            // TODO Auto-generated method stub32 33            super.onDestroy();34 35            Log.i("ACTIVITY","onDestroy()");36 37       }38 39       @Override40 41       protected void onPause() {42 43            // TODO Auto-generated method stub44 45            super.onPause();46 47            Log.i("ACTIVITY","onPause()");48 49       }50 51       @Override52 53       protected void onResume() {54 55            // TODO Auto-generated method stub56 57            super.onResume();58 59            Log.i("ACTIVITY","onResume()");60 61       }62 63       @Override64 65       protected void onStart() {66 67            // TODO Auto-generated method stub68 69            super.onStart();70 71            Log.i("ACTIVITY","onStart()");72 73       }74 75       @Override76 77       protected void onRestart() {78 79            // TODO Auto-generated method stub80 81            super.onRestart();82 83            Log.i("ACTIVITY","onRestart()");84 85       }86 87 }

布局文件:

1 <fragment2 3         android:id="@+id/frag"4 5         android:name="cn.csc.fragment.MyFragment"6 7         android:layout_width="wrap_content"8 9         android:layout_height="wrap_content"/>

 

注意,添加的fragment需要通过name属性指定自己新建的fragment的权限定名。

运行结果:

 

按下模拟器的BACK按钮:

 

中间插入了dalvik的垃圾回收,不过整体的顺序是没有改变的:

整个生命周期各个回调函数的调用过程如下:

显示到前台:

MyFragment  onAttach()  附加到activity上

MyFragment  onCreate()  fragment创建

MyFragment  onCreateView()  fragment创建自己的视图

MainActivity  onCreate() activity创建

MyFragment   onActivityCreated() 

MainActivity  onStart()

MyFragment    onStart()

MainActivity  onResume()

MyFragment    onResume()

按后退键

MyFragment    onPause()

MainActivity  onPause()

MyFragment    onStop()

MainActivity   onStop()

MyFragment    onDestoryView()  销毁掉自己的视图

MyFragment    onDestory()

MyFragment    onDetach()  解除和activity的关系

MainActivity   onDetory()

 

2.一个简单的两个Fragmet联系的示例,点击其中一个另一个发生变化:

1)新建MyFragment继承自ListFragment:

public class MyFragment extends ListFragment {       @Override      public void onActivityCreated(Bundle savedInstanceState) {           // TODO Auto-generated method stub           super.onActivityCreated(savedInstanceState);           setListAdapter(new ArrayAdapter<String>(getActivity(),           android.R.layout.simple_list_item_activated_1, android.R.id.text1,           new String[]{"6:00","6:30","7:20","8:00","8:40"}));      }      @Override      public void onListItemClick(ListView l, View v, int position, long id) {           // TODO Auto-generated method stub           super.onListItemClick(l, v, position, id);           //设置为单选模式,单击选中           getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);           getListView().setItemChecked(position, true);           FragmentManager fm = getFragmentManager();           FragmentTransaction ft = fm.beginTransaction();           Fragment fr = new TmpFragment(position);           ft.replace(R.id.frame, fr);           ft.commit();      }}                   

2)新建TmpFragment继承自Fragment:

 1 public class TmpFragment extends Fragment { 2  3       private int index; 4  5       private static String[] items = new String[]{"起床洗漱","打拳散步","吃早饭","等公交","打卡"}; 6  7       public TmpFragment(int index) { 8  9            super();10 11            this.index = index;12 13       }14 15       @Override16 17       public View onCreateView(LayoutInflater inflater, ViewGroup container,18 19                  Bundle savedInstanceState) {20 21            // TODO Auto-generated method stub22 23            TextView tv = new TextView(getActivity());24 25            tv.setText(items[index]);26 27            return tv;28 29       }30 31 }

3)布局文件:

 1 <fragment 2  3         android:id="@+id/frag" 4  5         android:name="cn.csc.fragment1.MyFragment" 6  7         android:layout_width="0dp" 8  9         android:layout_height="match_parent"10 11         android:layout_weight="1"12 13         />14 15     <FrameLayout16 17         android:id="@+id/frame"18 19         android:layout_width="0dp"20 21         android:layout_height="wrap_content"22 23         android:layout_weight="1">24 25 </FrameLayout> 

4)运行结果:

 

 

以上就是Fragment的简单学习,需要时再认真学下……

  相关解决方案