fragment 与activity通信

1、fragment简单套用(静态调用):

新建一个fragment,其xml文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00" >  

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is fragment 1"
        android:textColor="#000000"
        android:textSize="25sp" />  

</LinearLayout>  

其java文件如下:

 public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }
} 

在主Activity的布局中加入:

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragmentdemo.Fragment1"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" /> 

这样就可以了;

2、动态使用fragment

首先,Activity的布局中不用加fragment:

<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" >  

</LinearLayout>  

在java中:

Fragment1 fragment1 = new Fragment1();
            getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit(); 

分四步:

1.获取到FragmentManager,在Activity中可以直接通过getFragmentManager得到。

2.开启一个事务,通过调用beginTransaction方法开启。

3.向容器内加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。

4.提交事务,调用commit方法提交。

原文地址:https://www.cnblogs.com/zhaozilongcjiajia/p/10425180.html

时间: 2024-08-01 14:44:41

fragment 与activity通信的相关文章

使用EventBus进行Fragment和Activity通信

使用EventBus进行Fragment和Activity通信 本文介绍EventBus的基本使用,以及用于Fragment和Activity之间通信. github地址: https://github.com/greenrobot/EventBus 版本是 EventBus-2.4.0 release EventBus是基于订阅和发布的一种通信机制,使用流程如下: 实例化EventBus对象 注册订阅者 发布消息 接受消息 对应代码 EventBus eventBus = new EventB

Android学习——Fragment与Activity通信(二)

接下来就要到Fragment向Activity传输数据了.主要的思路,就是在Fragment中创建一个回调接口,利用该回调接口实现Fragment向Activity传输数据的功能. 回调函数(接口) 在学习利用回调接口实现Fragment向Activity传输数据之前,首先要对回调函数有所了解,下面引用知乎用户futeng的回答,侵删:https://www.zhihu.com/question/19801131/answer/26586203. 简单来说,回调函数就是当在一个类A中去调用类B的

Fragment 与 Activity 通信

先说说背景知识: (From:http://blog.csdn.net/t12x3456/article/details/8119607) 尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例.Fragment可以调用getActivity()方法很容易的得到它所在的activity的对象,然后就可以查找activity中的控件们(findViewById()).例如:ViewlistView

Fragment与Activity通信

在Fragment类中,有个方法 public void onAttach(Activity activity),在子Fragment中重写该方法,从该方法的参数中可以看出,该方法中的Activity是加载Fragment的Activity,所以可以通过该方法寻找Fragment的Activity; 在Activity类中,有个方法 public void onAttachFragment(Fragment fragment),api中是这么描述该方法的作用: /** * Called when

Fragment和Activity通信不过如此

// 在创建fragment的时候将值传递给fragment MyFragmentOne one = new MyFragmentOne(); Bundle bundle = new Bundle(); bundle.putInt("id", 1001); one.setArguments(bundle); manager.beginTransaction().add(R.id.content, one).commit(); // 宿主activity获取到fragment中控件的值

Android——Fragment和Activity之间的通信+Frangment生命周期

Android--Fragment和Activity之间的通信+Frangment生命周期 Fr'agment和Activity之间的通信 1.在Fragment中声明一个接口. 2.在Activity中实现在Fargment中声明的接口. 3.在Fragment中声明一个接口对象. 4.在Frangment的生命周期Onattach方法中判断当前Activity是否实现了此Fragment中声明的接口.如果已实现,就把当前Activity转换成接口对象. 5.调用Activity中实现的方法=

android中fragment与activity之间通信原理以及例子

参考文章 http://blog.csdn.net/guozh/article/details/25327685#comments Activity和fragment通信方式一般有3种方法 1.在fragment中定义接口, Activity去实现接口--->查看上面的参考文章 2.使用广播机制 3.使用EventBus 这3种方式 推荐使用EventBus 下面介绍第2种方式广播通信机制: 首先MainActivity中引入一个fragment, activity的布局很简单,里面只有一个 f

android Fragment与Activity交互,互相发数据(附图具体解释)

笔者最近看官方training.发现了非常多实用又好玩的知识. 当中.fragment与Activity通信就是一个. fragment与Activity通信主要是两点: 1.fragment传递信息给Activity 此点是通过在fragment中定义接口与Activity共享数据. 2.Activity传递信息给fragment 此点主要是通过fragment的getArgument()和setArgument()两个函数传递bundle来传递. 效果:(最后附上源代码) 主要流程: 1.在

Fragment与Acitvity通信

Fragment与Activity通信的方式如下: 一.通过初始化函数提供 1.在动态添加Fragment的过程中,我们在Activity中通过Fragment.setArguments()的方法为Fragment提供数据: 2.在Fragment中,在onAttach()函数中通过调用getArguments()获得一个Bundle对象,从而获取我们提供的数据. 二.创建回调接口 比如说:新闻浏览情境下,共有两个Fragment,一个是用来显示新闻标题:另外一个用来显示新闻内容.当我们点击新闻