简单的Fragment与Fragment传值

来源:互联网 发布:dota2个人数据查询 编辑:程序博客网 时间:2024/06/04 17:58

//主布局文件

<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;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubview = inflater.inflate(R.layout.fragment1, container, false);return view;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic 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;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubview = inflater.inflate(R.layout.fragment2, container, false);return view;}@Overridepublic void onActivityCreated(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onActivityCreated(savedInstanceState);textView_Fragment2 = (TextView) view.findViewById(R.id.textView_Fragment2);//拿到第一个IDFrament1 Fragment=(Frament1) getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment1);Fragment.setOnTitleSelectedListener(new OnTitleSelectedListener() {@Overridepublic void setOnTitleSelect(int position) {textView_Fragment2.setText(strs[position]);}});}}


原创粉丝点击