两个单独的Fragment之间是不应该进行通信的。应该使用他们所存在的Activity作为沟通的纽带。
Activity中可以获得Fragment的管理器
//获得Fragment的管理器
FragmentManager manager = this.getFragmentManager();
//使用findFragmentById获得Fragment的id
SecondFragment second = (SecondFragment) manager
.findFragmentById(R.id.secondFragment);
Fragment中可以获得可以获得自己的Activity
//Fragment可以获得Activity,调用Activity的的方法
MainActivity ac = (MainActivity) this.getActivity();
效果图:
点击左边的Fragment修改右边的Fragment
左边的Fragment的xml
<?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:background="#cc00cc"
android:orientation="horizontal" >
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2,右边的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:background="#cc0000"
android:orientation="horizontal" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是一个文本" />
</LinearLayout>
3,操作左边xml文件的java类
public class FirstFragment extends Fragment implements OnItemClickListener {
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_fragment, container, true);
listView = (ListView) view.findViewById(R.id.listView);
String[] items = { "语言和输入", "亮度", "存储", "网络" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this.getActivity(),
android.R.layout.simple_expandable_list_item_1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
return view;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// 获得被点击的条目文本
String str = (String) parent.getAdapter().getItem(position);
//Fragment可以获得Activity,调用Activity的的方法
MainActivity ac = (MainActivity) this.getActivity();
ac.change(str);
}
}
3, SecondFragment 的类操作右边的xml
public class SecondFragment extends Fragment {
private TextView tView;
static String TAG = "AC";
@Override
public void onAttach(Activity activity) {
Log.i(TAG, "---------------->onAttach");
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "---------------->onCreate");
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.i(TAG, "---------------->onActivityCreated");
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
Log.i(TAG, "---------------->onStart");
super.onStart();
}
@Override
public void onResume() {
Log.i(TAG, "---------------->onResume");
super.onResume();
}
@Override
public void onPause() {
Log.i(TAG, "---------------->onPause");
super.onPause();
}
@Override
public void onStop() {
Log.i(TAG, "---------------->onStop");
super.onStop();
}
@Override
public void onDestroyView() {
Log.i(TAG, "---------------->onDestroyView");
super.onDestroyView();
}
@Override
public void onDestroy() {
Log.i(TAG, "---------------->onDestroy");
super.onDestroy();
}
@Override
public void onDetach() {
Log.i(TAG, "---------------->onDetach");
super.onDetach();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "---------------->onCreateView");
View view = inflater
.inflate(R.layout.second_fragment, container, false);
tView = (TextView) view.findViewById(R.id.text);
return view;
}
public TextView gettView() {
return tView;
}
}
4,定义主配置的xml文件 定义成fragment的组件
<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="horizontal"
tools:context=".MainActivity" >
<fragment
android:id="@+id/firstFragment"
android:name="com.example.fragmant.FirstFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/secondFragment"
android:name="com.example.fragmant.SecondFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
5,MainActivity 操作文件
public class MainActivity extends Activity {
static String TAG = "AC";
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//改变Fragment的方法 使用findFragmentById获得Fragment的id
public void change(String msg) {
//获得Fragment的管理器
FragmentManager manager = this.getFragmentManager();
//使用findFragmentById获得Fragment的id
SecondFragment second = (SecondFragment) manager
.findFragmentById(R.id.secondFragment);
//改变第二个Fragment的值
second.gettView().setText(msg);
}
}