Fragment創(chuàng)建靜態(tài)加載與動態(tài)加載詳細(xì)流程
1、靜態(tài)加載
1、fragment layout
?2、fragment類
?3、在對應(yīng)的activity layout中加載 fragment
1、fragment layout
2、fragment class
package?com.example.administrator.fragment2;
import?android.os.Bundle;
import?android.support.annotation.Nullable;
import?android.view.LayoutInflater;
import?android.view.View;
import?android.view.ViewGroup;
import?java.util.zip.Inflater;
/**
?*?Created?by?Administrator?on?2015/7/22.
?*/
public?class?Fragment?extends?android.app.Fragment?{
????@Nullable
????@Override
????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,?Bundle?savedInstanceState)?{
????????/**
?????????*?將?layout轉(zhuǎn)成?view對象
?????????*/
????????View?view?=?inflater.inflate(R.layout.fragment,container,false);
????????return?view;
????}
}3.在對應(yīng)的activity layout中加載 fragment
2動態(tài)加載
前兩步和靜態(tài)加載一樣
不同的地方在于
package?com.example.administrator.fragment2;
import?android.app.*;
import?android.support.v7.app.ActionBarActivity;
import?android.os.Bundle;
import?android.view.Menu;
import?android.view.MenuItem;
public?class?MainActivity?extends?ActionBarActivity?{
????@Override
????protected?void?onCreate(Bundle?savedInstanceState)?{
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.activity_main);
????????//動態(tài)加載?fragment
????????Fragment?fragment?=?new?Fragment();?//這個是我們自己定義的?Fragment
????????FragmentManager?fragmentManager?=?getFragmentManager();
????????FragmentTransaction?fragmentTransaction?=?fragmentManager.beginTransaction();
????????fragmentTransaction.addToBackStack(null);?//后退按鍵?允許
????????fragmentTransaction.add(R.id.linear,fragment);
????????fragmentTransaction.commit();
????}
} 




