fragment的简单实用

1、他妈是谁

1)为了适应小屏幕的手机和大屏幕的平板及电视,以前经常需要开发两套APP。

2)动态和灵活设计UI。(其实正是通过第二个问题来解决第一个问题)

为了避免这俩问题,才有了Fragment。

2、自己是什么玩意

可以将它当做一个容器,需要的地方添加即可。

 

3、有啥特性

优点:不需要你亲自来管理view hierarchy的复杂变化,可在运行时修改activity的外观,并且由activity管理的back stack中保存这些变化。

 

4、兼容性

3.0版本引入的,如果用的之前的系统,需导入android-support-v4包。

android-support-v4包中用法:

1)宿主activity要继承FragmentActivity而不是Activity;

2)获取FragmentManager:getSupportFragmentManager();

二、常用用法
1、两种用法

1)静态添加fragment

原理:第一步:写fragment类;第二步:在activity的xml中引用。

第一步:定义xml,实现fragment

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     android:layout_width="match_parent"    
  3.     android:layout_height="match_parent"    
  4.     android:background="#ffff00" >    
  5.     
  6.     <TextView    
  7.         android:layout_width="wrap_content"    
  8.         android:layout_height="wrap_content"    
  9.         android:text="This is fragment 2"    
  10.         android:textColor="#000000"    
  11.         android:textSize="25sp" />    
  12.     
  13. </LinearLayout>  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public class Fragment1 extends Fragment {    
  2.     
  3.     @Override    
  4.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    
  5.         return inflater.inflate(R.layout.fragment1, container, false);    
  6.     }    
  7. }    

fragment.java文件中怎么访问xml中定义的控件:通过getActivity().findViewById();

第二步:布局中引入。使用fragment:name前缀来引用具体的fragment

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     android:layout_width="match_parent"    
  3.     android:layout_height="match_parent"    
  4.     android:baselineAligned="false" >    
  5.     
  6.     <fragment    
  7.         android:id="@+id/fragment1"    
  8.         android:name="com.example.fragmentdemo.Fragment1"    
  9.         android:layout_width="0dip"    
  10.         android:layout_height="match_parent"    
  11.         android:layout_weight="1" />    
  12.     
  13. </LinearLayout>  

2)动态添加fragment

 

原理:第一步:写fragment类;第二步:activity代码文件中添加。

第一步同上。

第二步:activity代码文件中添加:先获取FragmentManager;再开启一个事务,向容器中加入Fragment;最后提交事务。

注意:向容器中加入而不是控件(Button是控件),如果用控件会报错。

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public class MainActivity extends Activity {    
  2.     
  3.     @Override    
  4.     protected void onCreate(Bundle savedInstanceState) {    
  5.         super.onCreate(savedInstanceState);    
  6.         setContentView(R.layout.activity_main);    
  7.          
  8.         Fragment1 fragment1 = new Fragment1();    
  9.         getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();           
  10.     }    
  11. }   
2、fragment之间通信

fragment2要访问fragment1上的控件,需在fragment2.java的onActivityCreated()中实现:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. @Override    
  2. public void onActivityCreated(Bundle savedInstanceState) {    
  3.    super.onActivityCreated(savedInstanceState);    
  4.    Button button = (Button) getActivity().findViewById(R.id.button);    
  5.    button.setOnClickListener(new OnClickListener() {    
  6.       @Override    
  7.       public void onClick(View v) {    
  8.           TextView textView = (TextView) getActivity().findViewById(R.id.fragment1_text);    
  9.           Toast.makeText(getActivity(), textView.getText(), Toast.LENGTH_LONG).show();    
  10.       }    
  11.    });    
  12. }    

 

3、和activity通信

因所有的fragment都依附于activity,所以通信起来并不复杂,大概归纳为:

1)如果activity中包含自己管理的fragment的引用,可通过引用直接访问所有的fragment的public方法;

2)如果activity中未保存任何fragment的引用,每个fragment都有一个唯一的TAG或ID,可通过getFragmentManager.findViewByTag()或者findFragmentById()获得任何fragment实例,然后进行操作;

3)fragment中可通过getActivity()得到当前绑定的activity的实例,然后进行操作。

注:如果在Fragment中需要Context,可以通过调用getActivity(),如果该Context需要在Activity被销毁后还存在,则使用getActivity().getApplicationContext()。

4、生命周期

 

因Fragment依附于activity,所以activity的生命周期会影响到Fragment的生命周期。两者生命周期的关系:

onAttach(Activity):当Fragment和Activity发生关系时调用。

onCreateView(LayoutInflater, ViewGroup, Bundle):创建该Fragment的视图。

onActivityCreated(Bundle):当Activity的onCreate()返回时调用。

onDestroyView():当该Fragment的视图被移除时调用。

onDetach():与activity解除绑定时调用。

注意:除了onCrateView(),你如果要重写其他的所有方法,必须调用父类对于该方法的实现。

posted @ 2015-09-23 09:36  壮汉请留步  阅读(536)  评论(0编辑  收藏  举报