Fragment加载方式与数据通信
一、加载方式
1. 静态加载
1.1 加载步骤
(1) 创建fragment:创建自定义Fragment类继承自Fragment类,同时将自定义Fragment类与Fragment视图绑定(将layout转换成View)
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
inflater用于绑定Fragment的布局文件,同时将该布局转换成View对象并返回;container为Fragment的UI所在的父容器。返回值为Fragment显示的UI,若不显示,则返回null。
inflate(int resource, ViewGroup root, boolean attachToRoot)
resource为Fragment需要加载的布局文件;root为加载Fragment的父ViewGroup,也就是onCreateView传递进来的container;attachToRoot为是否返回父ViewGroup。
(2) 使用fragment:在父视图中引入fragment,静态加载必须指定name属性以及一个唯一标识符,标识符可以为id或者tag
<!--指定在layout中实例化的Fragment类,需要为“包名.类名”的完整形式-->
android:name
<!--唯一标识,id和tag可任选其一,不可两者都没有-->
android:id
android:tag
(3) 监听事件:若在父视图对应的类中设置监听事件,可以直接访问fragment中的子组件;若在Fragment的类中设置,则必须通过inflate()返回的View对象访问Fragment中的子组件(view.findViewById(id))。
1.2 简单范例
MyFragment视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fragment_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MyFragment类:
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//将layout布局转换成View对象
View view = inflater.inflate(R.layout.myfragment, container, false);
//必须通过view对象对其子组件进行访问
TextView textView = (TextView) view.findViewById(R.id.fragment_text);
textView.setText("这里是fragment");
//返回Fragment显示UI
return view;
}
}
引用fragment的父视图:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.studying.StaticFragmentActivity">
<fragment
android:tag="fragment"
android:name="com.joahyau.studying.MyFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
父视图对应的类设置事件监听:
public class StaticFragmentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_static_fragment);
//可直接通过findViewById访问
findViewById(R.id.fragment_text).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(StaticFragmentActivity.this, "点击了文本", Toast.LENGTH_SHORT).show();
}
});
}
}
2. 动态加载
2.1 加载步骤
(1) 获取事务管理器:对Fragment进行的添加、移除、替换等操作,均为事务。需通过以下代码获取事务管理器,从而对fragment进行动态操作。
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
(2) 创建Fragment对象:创建需要加载的fragment,而后通过add或replace等方法实现动态加载。
2.2 简单范例
布局:
<?xml version="1.0" encoding="utf-8"?>
<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="vertical"
tools:context="io.github.joahyau.studying.DynamicFragmentActivity">
<Button
android:id="@+id/load"
android:text="加载"
android:layout_width="match_parent"
android:layout_height="80dp" />
<LinearLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</LinearLayout>
Java:
public class DynamicFragmentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic_fragment);
findViewById(R.id.load).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取事务管理器
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//创建fragment,并将其动态加载到id位container的布局中
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.container, myFragment);
//提交事务
fragmentTransaction.commit();
}
});
}
}
二、数据通信
3. Activity向Fragment传递数据
3.1 Activity向动态加载的Fragment传递数据
(1)在Activity中获取Fragment对象;
(2)创建Bundle对象并传入数据;
(3)将Bundle对象传递给Fragment对象;
(4)在Fragment中获取Bundle对象并拆包得到数据。
范例:Activity中只有一个id为send的Button,MyFragment中只有一个TextView,这里就不再放布局代码了。
Activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建Fragment对象
MyFragment myFragment = new MyFragment();
//创建Bundle对象并传入数据
Bundle bundle = new Bundle();
bundle.putString("info", "这里是向Fragment传递的数据");
myFragment.setArguments(bundle);
//加载Fragment
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.add(R.id.layout, myFragment, "myfragment");
beginTransaction.commit();
}
});
}
}
Fragment:
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
TextView tv = (TextView) view.findViewById(R.id.text);
//获取数据
String text = getArguments().get("info") + "";
tv.setText(text);
return view;
}
}
3.2 Activity向静态加载的Fragment传递数据
(1)在Fragment中创建作为容器的数据对象,并创建getter和setter;
(2)在Activity中获取FragmentManager;
(3)通过事务管理器的findFragmentById或findFragmentByTag方法,获得fragment对象;
(4)通过获得的fragment对象调用容器的setter方法进行传值。
范例:这里的布局与动态加载的布局唯一不同的就是将send按钮放在了Fragment里面,其它相同。
Fragment:
public class MyFragment extends Fragment {
private Button btn;
private String received;//作为容器的对象
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
TextView tv = (TextView) view.findViewById(R.id.text);
tv.setText("这里是Fragment");
btn = (Button) view.findViewById(R.id.send);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "成功接收\"" + getReceived() + "\"", Toast.LENGTH_SHORT).show();
}
});
return view;
}
public String getReceived() {
return received;
}
public void setReceived(String received) {
this.received = received;
}
}
Activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.my_fragment);
myFragment.setReceived("this is a test.");
}
}
4. Fragment向Activity传递数据
(1)在Fragment中写一个回调接口;
(2)在activity中实现这个回调接口,实现的函数用于传值;
(3)重写Fragment中onAttach,在其中创建一个接口对象,得到传递过来的activity(我的理解是这个接口其实相当于传递过来的activity的一个父类,这一步是用到了多态的特性);
(4)用得到的接口对象进行传值。
Fragment:
public class MyFragment extends Fragment {
private SendData sendData;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//获取实现的接口对象
sendData = (SendData) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
TextView tv = (TextView) view.findViewById(R.id.text);
tv.setText("这里是Fragment");
//通过接口对象传递数据
sendData.sendMsg("this is a test.");
return view;
}
//定义一个回调接口
public interface SendData{
void sendMsg(String str);
}
}
Activity:
public class MainActivity extends Activity implements MyFragment.SendData{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.send);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyFragment myFragment = new MyFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.add(R.id.layout, myFragment);
beginTransaction.commit();
}
});
}
//实现SendData接口,接收数据
@Override
public void sendMsg(String str) {
Toast.makeText(this, "成功接收\"" + str + "\"", Toast.LENGTH_SHORT).show();
}
}
Fragment加载方式与数据通信的更多相关文章
- Xamarin Android Fragment的两种加载方式
android Fragment的重点: 3.0版本后引入,即minSdk要大于11 Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fra ...
- Android系列之Fragment(一)----Fragment加载到Activity当中
Android上 的界面展示都是通过Activity实现的,Activity实在是太常用了.但是Activity也有它的局限性,同样的界面在手机上显示可能很好看, 在平板上就未必了,因为平板的屏幕非常 ...
- iOS 正确选择图片加载方式
正确选择图片加载方式能够对内存优化起到很大的作用,常见的图片加载方式有下面三种: //方法1 UIImage *imag1 = [UIImage imageNamed:@"image.png ...
- sql*loader的直接加载方式和传统加载方式的性能差异
1.确认数据库版本 2.数据准备 3.创建导入表及控制文件 4.直接加载方式演示 查看具体的日志: 5.传统加载方式演示 查看日志文件: 6.结论及两种方式的差异 经过比对direct比convent ...
- 浅谈Entity Framework中的数据加载方式
如果你还没有接触过或者根本不了解什么是Entity Framework,那么请看这里http://www.entityframeworktutorial.net/EntityFramework-Arc ...
- log4j加载方式导致的bae和sae部署异常
这2天改在bae上部署代码,为了便于程序的功能测试,引入了log4j日志,但是问题来了..测试程序采用的是spring3.2.8框架搭建,web.xml引入日志代码为: <context-par ...
- 链接库lib和dl的概念,加载方式的区别
使用LR进行基于windows socket协议做接口测试,只提供了lr_load_dll方法来动态加载动态链接库.之前学习阶段,对TinyXML的学习,使用的静态链接库,当时在程序调用的时候方法也跟 ...
- android开发之Fragment加载到一个Activity中
Fragments 是android3.0以后添加的.主要是为了方便android平板端的开发.方便适应不同大小的屏幕.此代码是为了最简单的Fragment的使用,往一个Activity中添加Frag ...
- Android Activity四种加载方式
Android之四种加载方式 (http://marshal.easymorse.com/archives/2950 图片) 在多Activity开发中,有可能是自己应用之间的Activity跳转,或 ...
随机推荐
- 使用Python的requests库进行接口测试——session对象的妙用
from:http://blog.csdn.net/liuchunming033/article/details/48131051 在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有 ...
- 记录:mac的浏览器访问任何域名、网址都跳转到本地127.0.0.1或固定网址
新年上班第一天,刚开机就遇到了个小坑,问题是这样,打开浏览器,输入任何网址都跳转到本地的一个项目,该项目在本地Apache配置下,监听的端口是8888,本机访问的形式是127.0.0.1:8888. ...
- 从此不再担心键盘遮住输入框OC(
从此不再担心键盘遮住输入框OC(二) 字数544 阅读1492 评论15 喜欢25 在我发布这篇文章没多久之前,我发布了一篇叫 从此不再担心键盘遮住输入框OC(一)的文章.我在那篇文章中介绍了我的键盘 ...
- Git 如何 clone 非 master 分支的代码
问题描述 我们每次使用命令 git clone git@gitlab.xxx.com:xxxxx.git 默认 clone 的是这个仓库的 master 分支.如果最新的代码不在 master 分支上 ...
- VC下防止反汇编的办法(1)
最近在看IDA的书,讲汇编语言的部分提到了一种防止递归向下汇编器逆向程序的方法 这里esp指向栈顶,也就是调用方最后入栈的返回地址.然而实际在VC2017里用内联汇编这么做是不行的,原因可以看看VC生 ...
- sql server两个时间段内,求出周末的量
公司有个表记录了出差(加班)的初始时间和截止时间,现在要计算出加班时间,之前的设计并没有考虑到这部分,因此本人通过sql重新计算周末数 表formmain starttime endtime 使用游标 ...
- tensorflow import 没找到cudnn库问题解决
ImportError: libcudnn.so.5: cannot open shared object file: No such file or directory 将cuda下lib64中的l ...
- iOS应用如何得知用户有新拍的图片?
首先,应用要知道图片库中的新图片,最重要是要有图片库的访问权限.然后每张图片除了图片本身的构成要素(像素)外,还会保存图片的拍摄时间(时间戳),地点等相关信息.时间戳就是判断新拍照片的最主要依据.
- 动态规划算法的java实现
一:动态规划 1)动态规划的向前处理法 java中没有指针,所以邻接表的存储需要转化一中形式,用数组存储邻接表 用三个数组u,v,w存储边,u数组代表起点,v数组代表终点,w代表权值;例如:1--&g ...
- 情景linux--如何解决read命令产生的硬编码问题
情景 我们知道,read命令可以读取文件内容,并把内容赋值给变量. 以如下的数据文件为例. $ cat data.txt 1 201623210021 wangzhiguo 25 2 20162321 ...