Fragment使用(一)

一 首先是引用包的问题

Fragment是Android 3.0出现的,旧版本要兼容使用Fragment必须引入包android-support-v4.jar,可以兼容到1.6。

它们的区别:

1.在创建FragmentTransaction实例时: 

  如果import android.app.Fragment;使用的是FragmentManager fragmentManager = getFragmentManager()

  如果import android.support.v4.app.FragmentManager;那么使用的是:FragmentManager fragmentManager = getSupportFragmentManager();

2.它们都可以使用<fragment>标签。

  如果import android.app.Fragment;加入Fragment布局的Activity继承Activity。

  如果import android.support.v4.app.FragmentManager;加入Fragment布局的Activity继承FragmentActivity。

  否则报异常。android.app.Fragment$InstantiationException:Trying to instantiate a class XXX  is not a fragment。或者抛出android.view.InflateException: Binary XML file line #7: Error inflating class fragment异常。

3.两者不能一起用。

二开始使用Fragment。

Fragment主要有两种加载方式,两种加载方式可以同时用。

1 静态添加Fragment,即通过某Activity的布局文件将Fragment加入该Activity。

  1)在layout文件夹定义example_fragment.xml作为Fragment的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     <TextView  
        android:id="@+id/staticfragment_text"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="这是一个静态添加的Fragment"  
        android:textColor="#000000"  
        android:textSize="25sp" /> 

</LinearLayout>
View Code

  2)定义ExampleFragment.java继承Fragment,在onCreateView方法中设置以R.layout.example_fragment作为资源的布局。

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class ExampleFragment extends Fragment {
    private String TAG = "ExampleFragment";
    /******************当被创建时经历的状态,对应Activity的Created********************/
    @Override
    public void onAttach(Activity activity) {
        Log.d(TAG, "onAttach");
        super.onAttach(activity);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {//必须实现的回调函数。只被调一次,用这里应该初始化相关的组件,一些即便是被暂停或者被停止时依然需要保留的东西。
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
    }
    /*****************当对用户可见时*****************/
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,//必须实现的回调函数。当每次可见时系统调用这个方法,必须返回一个View,如果Fragment不提供UI也可以返回null。
            Bundle savedInstanceState) {//container参数代表该Fragment在Activity中的父控件;savedInstanceState提供了上一个实例的数据。
        Log.d(TAG, "onCreateView");
        View view = inflater.inflate(R.layout.example_fragment,container, false);//第一个是resource ID,指明了当前的Fragment对应的资源文件;第二个参数是父容器控件;第三个布尔值参数表明是否连接该布局和其父容器控件,在这里的情况设置为false,因为系统已经插入了这个布局到父控件,设置为true将会产生多余的一个View Group。
        return view;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.d(TAG, "onActivityCreated");
        super.onActivityCreated(savedInstanceState);
    }
    /**********************************/
    //对应Activity的Started
    @Override
    public void onStart() {
        Log.d(TAG, "onStart");
        super.onStart();
    }
    //对应Activity的Resumed状态
    @Override
    public void onResume() {
        Log.d(TAG, "onResume");
        super.onResume();
    }
    /*****************当进入后台模式,不可见*****************/
    //Paused
    @Override
    public void onPause() {//必须实现的回调函数。当用户离开Fragment时第一个调用这个方法,需要提交一些变化,因为用户很可能不再返回来。
        Log.d(TAG, "onPause");
        super.onPause();
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        Log.d(TAG, "onSaveInstanceState");
        super.onSaveInstanceState(outState);
    }
    //Stopped
    @Override
    public void onStop() {
        Log.d(TAG, "onStop");
        super.onStop();
    }
    /*****************对应Destroyed状态*****************/
    @Override
    public void onDestroyView() {
        Log.d(TAG, "onDestroyView");
        super.onDestroyView();
    }
    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        super.onDestroy();
    }
    @Override
    public void onDetach() {
        Log.d(TAG, "onDetach");
        super.onDetach();
    }
    
    
}
View Code

  3)在main.xml文件中定义<fragment>标签,标签name为自己定义的ExampleFragment.java的完整的类名。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical"
    android:id="@+id/main_linearlayout"
     >  
      <!-- android:name属性填上你自己创建的fragment的完整类名。 -->
    <fragment  android:name="com.example.ExampleFragment"
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_weight="1" />  
</LinearLayout> 
View Code

  4)在MainActivity设置main.xml布局,启动看看吧。

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

}
View Code

2 动态添加Fragment,即通过编程的方式将Fragment加入到一个ViewGroup中。前两步同上,不贴代码了。

  1)在layout文件夹定义example_fragment.xml作为Fragment的布局文件。

  2)定义ExampleFragment.java继承Fragment,在onCreateView方法中设置以R.layout.example_fragment作为资源的布局。

  3)在main.xml文件中定义一个<LinearLayout>或者其他的布局,用来做为动态生成的Fragment的父容器,id设置一个值”dynimic_layout“做为父容器的id

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical"
    android:id="@+id/main_linearlayout"
     >  
     <LinearLayout
        android:id="@+id/dynimic_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        /> 
  
</LinearLayout> 
View Code

  4)在MainActivity中使用FragmentTransaction的add()方法添加Fragment。over。

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //动态添加Fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        DynamicFragment fragment = new DynamicFragment();
        fragmentTransaction.add(R.id.dynimic_layout, fragment);
        fragmentTransaction.commit();
    }

}
View Code
原文地址:https://www.cnblogs.com/chrono/p/3993448.html