简单的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]);}});}}
阅读全文
0 0
- 简单的Fragment与Fragment传值
- 简单的fragment传值
- 简单的fragment传值
- fragment与fragment,activity与fragment传值
- Fragment的简单实用与Fragment的生命周期
- Fragment与ViewPager嵌套的简单应用
- Fragment简单的创建与使用
- ViewPager与Fragment结合的简单使用
- Activity与Fragment的简单封装
- fragment的简单介绍
- Fragment的简单使用
- Fragment的简单介绍
- Fragment的简单显示
- Fragment的简单使用
- Fragment的简单使用
- Fragment的简单用法
- 简单的Fragment切换
- Fragment 的简单使用
- tp分页的实现(如何随心所欲生成页码的url参数)
- 请求上下文
- python刷题笔记2--Reverse Integer
- UTribe项目后台管理系统的前端实现
- RxJava+Retrofit+Mvp实现购物车
- 简单的Fragment与Fragment传值
- 【Java】基本类型之int(五)
- luogu 1055 ISBN号码
- JavaSE多线程
- Oracle:ORA-01789: 查询块具有不正确的结果列数
- github初使用
- 应用上下文
- 不使用JQuery选择HTML Element
- bug君你好啊之servlet页面读取jsp的url的值显示为null