服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android应用开发中Fragment与Activity间通信示例讲解

Android应用开发中Fragment与Activity间通信示例讲解

2021-06-18 15:25泡在网上的日子 Android

这篇文章主要介绍了Android应用开发中Fragment与Activity间通信实例讲解,需要的朋友可以参考下

首先,如果你想在android3.0及以下版本使用fragment,你必须引用android-support-v4.jar这个包
然后你写的activity不能再继承自activity类了,而是要继承android.support.v4.app.fragmentactivity,一些其他的父类也有相应的变化.
由于在android的实现机制中fragment和activity会被分别实例化为两个不相干的对象,他们之间的联系由activity的一个成员对象fragmentmanager来维护.fragment实例化后会到activity中的fragmentmanager去注册一下,这个动作封装在fragment对象的onattach中,所以你可以在fragment中声明一些回调接口,当fragment调用onattach时,将这些回调接口实例化,这样fragment就能调用各个activity的成员函数了,当然activity必须implements这些接口,否则会包classcasterror
fragment和activity的回调机制又是oop的一次完美演绎!
下面通过一个例子来说明:
首先,我们看下界面

Android应用开发中Fragment与Activity间通信示例讲解

Android应用开发中Fragment与Activity间通信示例讲解

左边的textview会根据右边点击button的不同而改变。

下面开始介绍代码:

1.在layout里新建fragment1.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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="#00ff00"
 android:orientation="vertical" >
 <textview
 android:id="@+id/fragment_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="this is fragment 1"
 android:textcolor="#000000"
 android:textsize="25sp" />
</linearlayout>

可以看出,这里就只有一个textview

2.在layout里新建fragment2.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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="#ffff00"
 android:orientation="vertical" >
 <textview
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="this is fragment 2"
 android:textcolor="#000000"
 android:textsize="25sp" />
 <button
 android:id="@+id/button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="num 1" />
 <button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="num 2" />
 <button
 android:id="@+id/button3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="num 3" />
</linearlayout>

这里是三个button

3.创建类fragment1继承fragment

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package lgx.fram.framents;
 
import android.app.fragment;
import android.os.bundle;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
 
public class fragment1 extends fragment {
 @override
 public view oncreateview(layoutinflater inflater, viewgroup container,
  bundle savedinstancestate) {
 return inflater.inflate(r.layout.fragment1, container, false);
 }
}

重写oncreateview()方法,这里 return inflater.inflate(r.layout.fragment1, container, false); 这句话是重点

4.创建类fragment2继承fragment

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package lgx.fram.framents;
 
import android.app.fragment;
import android.os.bundle;
import android.view.layoutinflater;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.viewgroup;
import android.widget.button;
import android.widget.textview;
 
public class fragment2 extends fragment {
 @override
 public view oncreateview(layoutinflater inflater, viewgroup container,
  bundle savedinstancestate) {
 return inflater.inflate(r.layout.fragment2, container, false);
 }
 
 textview textview;
 button button, button2, button3;
 
 @override
 public void onactivitycreated(bundle savedinstancestate) {
 super.onactivitycreated(savedinstancestate);
 button = (button) getactivity().findviewbyid(r.id.button);
 button2 = (button) getactivity().findviewbyid(r.id.button2);
 button3 = (button) getactivity().findviewbyid(r.id.button3);
 textview = (textview) getactivity().findviewbyid(r.id.fragment_text);
 button.setonclicklistener(new onclicklistener() {
 
  @override
  public void onclick(view v) {
  textview.settext(button.gettext());
  }
 });
 button2.setonclicklistener(new onclicklistener() {
 
  @override
  public void onclick(view v) {
  textview.settext(button2.gettext());
  }
 });
 button3.setonclicklistener(new onclicklistener() {
 
  @override
  public void onclick(view v) {
  textview.settext(button3.gettext());
  }
 });
 }
}
button = (button) getactivity().findviewbyid(r.id.button);

通过这种方法来得到fragment上面的控件

5.activity_fragment.xml里面的代码是这个样子的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/main_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:baselinealigned="false"
 android:orientation="horizontal" >
 <fragment
 android:id="@+id/fragment1"
 android:name="lgx.fram.framents.fragment1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1" />
 <fragment
 android:id="@+id/fragment2"
 android:name="lgx.fram.framents.fragment2"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_weight="1" />
</linearlayout>

注意:控件fragment里的android:name=" "里面填写的是你的fragment类的绝对路径(脑子突然短路,是这样说的吗??),id用来标示fragment。

6.fragmentactivity是最简单的,就只是setcontentview,并没有进行其他改变。看下面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package lgx.fram.framents;
 
import android.app.activity;
import android.os.bundle;
 
 
public class fragmentactivity extends activity {
 
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_fragment);
 }
 
}

在这里我的整个小应用就做完了。我这里的fragment通过布局文件加入到activity里的,还有另一种方式是通过编程的方式将fragment加入activity里。这里我简单叙述

上面的1,2,3,4都不需要动。

第5步骤,activity_fragment.xml里面的代码变成下面的

?
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/main_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:baselinealigned="false"
 android:orientation="horizontal" >
</linearlayout>

你会发现我知识去掉了两个fragment,整个linearlayout加进去了id

第6个步骤,里面的注释,已经写得很清楚了:

?
1
2
3
4
5
6
package lgx.fram.framents;
 
import android.os.bundle;
import android.app.activity;
import android.view.display;
import android.view.menu;

 @author lenovo 动态添加fragment主要分为4步:
(1)获取到fragmentmanager,在activity中可以直接通过getfragmentmanager得到。
(2)开启一个事务,通过调用begintransaction方法开启。
(3)向容器内加入fragment,一般使用replace方法实现,需要传入容器的id和fragment的实例。
(4)提交事务,调用commit方法提交。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class fragmentactivity extends activity {
 
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_fragment);
 display display = getwindowmanager().getdefaultdisplay();
 if (display.getwidth() > display.getheight()) {
  fragment1 fragment1 = new fragment1();
  getfragmentmanager().begintransaction()
   .replace(r.id.main_layout, fragment1).commit();
 } else {
  fragment2 fragment2 = new fragment2();
  getfragmentmanager().begintransaction()
   .replace(r.id.main_layout, fragment2).commit();
 }
 }
 
}

这个代码的意思是,横竖屏显示不同的fragment。如果是模拟机测试,请按ctrl+f11。

延伸 · 阅读

精彩推荐