//第一種:靜態建立 //在主佈局文件中利用佈局文件建立 //在這個佈局文件加這個屬性android:name="com.example.fragment.LeftFragment" //通常會是:包名.類名,(包名加上你建立的FragMeant) //第二中則是代碼建立,如下示例 一個Activity 一個LeftFragment碎片,一個fragment_right碎片 //LeftFragment public class LeftFragment extends Fragment { private ListView listView_left; private FragmentManager fragmentManager; private FragmentTransaction transaction; private ArrayAdapter<String> adapter; @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } //onCreateView這個重寫方法通常是用來生成本身的佈局 @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_listview, container, false); //生成leftFrag本身的佈局控件 listView_left = (ListView) view .findViewById(R.id.listView_leftfragment); // 獲取字符串資源文件 String[] classname = getResources().getStringArray(R.array.calssname); adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, classname); listView_left.setAdapter(adapter); listView_left.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //這裏是監聽listview點擊時將數據傳到右碎片 //代碼動態建立FragMent核心代碼 fragmentManager = getFragmentManager(); transaction = fragmentManager.beginTransaction(); //右碎片RightFragment RightFragment RightFragment = new RightFragment(); transaction.replace(R.id.fragment_right, RightFragment); transaction.addToBackStack(null);// 按回退鍵時能夠往回看原來的內容 Bundle bundle = new Bundle(); String classname = adapter.getItem(position).toString(); bundle.putString("classname", classname); RightFragment.setArguments(bundle); // 上面兩個步驟那個建立先都無所謂,由於下面還要提交事務,因此順序沒什麼影響 transaction.commit(); } }); return view; } } //有碎片RightFragment,這邊接收,主要重寫onCreateView返回本身的視圖 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { Bundle bundle = getArguments();//接收數據 String classname = bundle.getString("classname"); View view = inflater.inflate(R.layout.Textview, container, false); //先找到佈局,再找佈局中的控件 TextView text= (ListView) view .findViewById(R.id.TextView_rightfragment); text.setText(classname ); return view; } //主佈局文件文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 這個是靜態建立的fragment --> <fragment android:id="@+id/fragment_left" android:name="com.example.fragment.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <!-- 下面這個是留給代碼動態建立的佈局,動態建立不用聲明,留一個佈局給它便可 --> <LinearLayout android:id="@+id/fragment_right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" android:orientation="vertical" > </LinearLayout> </LinearLayout> //leftFragmeng的佈局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 靜態的fragment的佈局listview --> <ListView android:id="@+id/listView_leftfragment" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>