//主布局文件
<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">
<fragment
android:id="@+id/fragment1"
android:name="com.example.demo02.Frament1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<fragment
android:id="@+id/fragment2"
android:name="com.example.demo02.Frament2"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
/>
</LinearLayout>
//第一个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="#CCffff"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_Fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是第一个Fragment" />
<ListView
android:id="@+id/listView_Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
//第二个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="#CCff00"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_Fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是第二个Fragment" />
</LinearLayout>
//Fragment
public class Frament1 extends Fragment{
private View view;
private TextView textView_Fragment1;
private ListView listView_Fragment;
private String[] strs = new String[] { "林青霞", "张曼玉", "王祖贤", "朱茵", "蔡少芬","宁静", "谢娜", "大S", "小S" };
private OnTitleSelectedListener onTitleSelectedListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.fragment1, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
//获取控件
textView_Fragment1 = (TextView) view.findViewById(R.id.textView_Fragment1);
listView_Fragment = (ListView) view.findViewById(R.id.listView_Fragment);
//创建适配器
listView_Fragment.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1, strs));
//点击事件
listView_Fragment.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
onTitleSelectedListener.setOnTitleSelect(arg2);
}
});
}
//创建接口
interface OnTitleSelectedListener{
void setOnTitleSelect(int position);
}
//第二部:写一个公共的方法,要求传递接口进来,转成成员变量
public void setOnTitleSelectedListener(OnTitleSelectedListener onTitleSelectedListener){
this.onTitleSelectedListener=onTitleSelectedListener;
}
}
//第二个Fragment
public class Frament2 extends Fragment{
private View view;
private String[] strs = new String[] { "林青霞", "张曼玉", "王祖贤", "朱茵", "蔡少芬","宁静", "谢娜", "大S", "小S" };
private TextView textView_Fragment2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.fragment2, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
textView_Fragment2 = (TextView) view.findViewById(R.id.textView_Fragment2);
//拿到第一个ID
Frament1 Fragment=(Frament1) getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment1);
Fragment.setOnTitleSelectedListener(new OnTitleSelectedListener() {
@Override
public void setOnTitleSelect(int position) {
textView_Fragment2.setText(strs[position]);
}
});
}
}