简单的Fragment与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">
    <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]);
			}
		});
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值