首先來介紹的是我們的默認的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <fragment android:id="@+id/fragment1" android:name="com.example.chuang.frgone" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> <fragment android:id="@+id/fragment2" android:name="com.example.chuang.frgtwo" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
需要注意的屬性也就fragment中的那個name屬性,這個屬性很關鍵,作用就是在這個布局被加載時,fragment自動實例化對應的類
既然要加載兩個類文件,所以就必須加載對應的布局,
fragment_one 的布局文件:
<?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" > <ListView android:id="@+id/ls_show" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
fragment_two 的布局文件:
<?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/tv_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
然后我們需要做的就是將布局文件和類進行綁定
package com.example.chuang; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class frgtwo extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment_two, container); } }
其他的綁定也是類似的,運行的效果如下圖,在這里為你看清楚,我在Fragment_one中添加了背景顏色,
為了方便測試,我們定義了如下的字段
package com.example.chuang; import java.util.ArrayList; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; public class frgone extends Fragment { private ListView ls=null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_one, container); ls=(ListView)v.findViewById(R.id.ls_show); ls.setAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,GetArray())); return v; } private ArrayList<String> GetArray() { ArrayList<String> al=new ArrayList<String>(); al.add("我就是我"); al.add("點亮我的新"); al.add("你的心,我的鑫"); return al; } }
效果圖如下
那現在我們要做的呢就是在左邊點擊,右邊顯示,也就是實現Fragment之間的傳值
第一種方法,也就是利用回調
package com.example.chuang; import java.util.ArrayList; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; public class frgone extends Fragment { private CallBack back=null; private ListView ls=null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_one, container); ls=(ListView)v.findViewById(R.id.ls_show); ls.setAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,GetArray())); ls.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if(back!=null) { back.SetTeee(String.valueOf( parent.getItemAtPosition(position))); //back.SetTeee(getString(getId())); } } }); return v; } private ArrayList<String> GetArray() { ArrayList<String> al=new ArrayList<String>(); al.add("我就是我"); al.add("點亮我的新"); al.add("你的心,我的鑫"); return al; } public interface CallBack { void SetTeee(String s); } @Override public void onAttach(Activity activity) { super.onAttach(activity); if(activity instanceof CallBack) { back=(CallBack)activity; } } }
然后是主Activity的代碼
@Override public void SetTeee(String s) { View v= this.findViewById(R.id.fragment2); TextView t=(TextView)v.findViewById(R.id.tv_show); t.setText(s); }
效果圖如下,這樣我們就實現了第一種方法
第二種方法
使用第二種方法,我們首先需要來了解一下Fragment 和Activity的生命周期
我們可以根據上面的那個圖,可以知道,只有當fragment創建完畢,活動就會通知他們調用onActivityCreated()方法,來告知
所以我們就可以在這里獲取到主活動里面的一切,不是想干嘛就干嘛嗎0-0!
貼上代碼
package com.example.chuang; import java.util.ArrayList; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class frgone extends Fragment { private CallBack back=null; private ListView ls=null; private TextView tv=null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_one, container); ls=(ListView)v.findViewById(R.id.ls_show); ls.setAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,GetArray())); ls.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if(back!=null) { if(tv!=null) tv.setText(String.valueOf( parent.getItemAtPosition(position))); } } }); return v; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); View v= getActivity().findViewById(R.id.fragment2); tv=((TextView)v.findViewById(R.id.tv_show)); } private ArrayList<String> GetArray() { ArrayList<String> al=new ArrayList<String>(); al.add("我就是我"); al.add("點亮我的新"); al.add("你的心,我的鑫"); return al; }
第三種方法,那就是在Activity中來管理兩個Fragment,當然做法也是非常的簡單,只需要在Activity的start生命周期后就可以