Fragment与Activity的通信
来源:互联网 发布:集思宝g120数据导出 编辑:程序博客网 时间:2024/06/15 23:09
package com.zdsoft.activityfragment1205;import android.support.v4.app.Fragment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity implements ListFragment.OnItemSelectListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void OnSelect(int index) { String str = ""; //获得frament ContentFragment fragment = (ContentFragment) getSupportFragmentManager().findFragmentById(R.id.f_content); switch (index) { case 0: str = "你好,我是Android"; fragment.showMessage(str); break; case 1: str = "你好,我是IOS"; fragment.showMessage(str); break; case 2: str = "你好,我是WP"; fragment.showMessage(str); break; default: break; } }}
package com.zdsoft.activityfragment1205;import android.content.Context;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;/** * A simple {@link Fragment} subclass. */public class ListFragment extends Fragment { private ListView lv_show; private String[] array = {"android", "IOS", "WP"}; OnItemSelectListener listener; public ListFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_list, container, false); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array); lv_show = (ListView) view.findViewById(R.id.lv_show); lv_show.setAdapter(adapter); lv_show.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //调用接口内方法 listener.OnSelect(position); } }); return view; } /** * 定义接口 */ public interface OnItemSelectListener { public void OnSelect(int index); } /** * 重写onAttach方法实例化listener; * * @param context */ @Override public void onAttach(Context context) { super.onAttach(context); listener = (OnItemSelectListener) context; }}
package com.zdsoft.activityfragment1205;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;/** * A simple {@link Fragment} subclass. */public class ContentFragment extends Fragment { private TextView tv_show; public ContentFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_content, container, false); tv_show = (TextView) view.findViewById(R.id.tv_show); return view; } public void showMessage(String msg) { tv_show.setText(msg); }}
<?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:orientation="horizontal"> <fragment android:id="@+id/f_list" android:name="com.zdsoft.activityfragment1205.ListFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/f_content" android:name="com.zdsoft.activityfragment1205.ContentFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" /></LinearLayout>
<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:background="@color/colorAccent" android:orientation="vertical"> <!-- TODO: Update blank fragment layout --> <ListView android:id="@+id/lv_show" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
<FrameLayout 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" tools:context="com.zdsoft.activityfragment1205.ContentFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="match_parent" /></FrameLayout>
0 0
- Fragment与Activity的通信:
- Fragment与Activity的通信
- Fragment与Activity的通信
- Activity与Fragment,Fragment与Fragment之间的通信
- Fragment与Activity通信
- Fragment与Activity通信
- Fragment与Activity通信
- Fragment与Activity通信
- Fragment与Activity通信
- Fragment与Activity通信
- Fragment与Activity通信
- Activity与Fragment通信
- Fragment与Activity通信
- Fragment与Activity通信
- Fragment与Activity之间的通信
- android Activity与Fragment之间的通信
- fragment与activity之间的通信
- Activity与Fragment之间的通信详解
- 商务统邀请框openZoosUrl还可以这么玩
- Linux文件系统
- Spring+Hibernate进行单元测试时出现read-only mode错误
- make: execvp: /usr/local/cuda: 权限不够 Makefile:588: recipe for target '.build_release/cuda/src/caffe/l
- Android 地址空间随机化(ALSR)缓解技术
- Fragment与Activity的通信
- Spring学习笔记(11.Spring Bean装配注释)
- IT人生法则
- 网络抓包神器-Charles使用指南
- linux用户管理和分区
- python 装饰器
- U-Boot学习笔记(四):TEXT_BASE的理解
- Android将商品加入购物车的动画效果
- VirtualBox的虚拟硬盘vdi扩容