• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > Android中使用开源框架EventBus3.0实现Fragment之间的通信交互,fragment开源框架

Android中使用开源框架EventBus3.0实现Fragment之间的通信交互,fragment开源框架

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了Android中使用开源框架EventBus3.0实现Fragment之间的通信交互,fragment开源框架等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

Android中使用开源框架EventBus3.0实现Fragment之间的通信交互,fragment开源框架


1.概述

在之前的博文中简单介绍过如何实现fragment之间的信息交互:《Android中Fragment与Activity之间的交互(两种实现方式)》,今天继续给大家介绍一种可以实现此效果的另外一种方式EventBus。(相比于handler,接口回调,bundle传参,这个简单好用到哭)

EventBus是Android下高效的发布/订阅事件的消息总线。作用是可以代替传统的Intent,Handler,Broadcast或接口函数在Fragment、Activity、Service、线程之间传递数据进行通信,执行方法。做为消息总线,有三个主要元素:

(1)Event:事件

(2)Subscriber:事件订阅者,接受特定的事件

(3)Publisher:事件发布者,用于通知Subscriber有事件发生

结合EventBus以上的三个元素,我们也可以称其为一种观察者设计模式。

EventBus 官网链接http://greenrobot.org/eventbus/

EventBus GitHub链接https://github.com/greenrobot/EventBus

前期相关博文链接:

Android中Fragment与Activity之间的交互(两种实现方式)

Android中Fragment的两种创建方式

2.Demo示例

(1)示例中左侧的按钮,潘侯爷与碧空海触发的事件为EventBus的普通事件发布 (2)左侧粘性事件按钮发布的为粘性事件

3.实现步骤

本次Demo架构:

3.1导依赖包

使用AndroidStudio2.2。仍然采用在build.gradle下中dependencies下直接添加如下代码:

compile 'org.greenrobot:eventbus:3.0.0'

同步后完成依赖添加。

3.2布局文件

(1)layout中主布局文件,activity_main.xml文件

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.mly.panhouye.eventbustest.MainActivity">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="#6f6669">
        <Button
            android:layout_gravity="center_horizontal"
            android:id="@+id/panhouye"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="?" />
        <Button
            android:layout_gravity="center_horizontal"
            android:id="@+id/bikonghai"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="??" />
        <Button
            android:layout_gravity="center_horizontal"
            android:id="@+id/postSticky"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="?Д?" />
    </LinearLayout>
    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"></FrameLayout>
</LinearLayout>

(2)layout中右侧的fragment布局文件fragment_msg.xml文件

<?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/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="no data"
        android:textSize="50sp"
        android:gravity="center_horizontal"/>
</LinearLayout>

(3)layout中粘性事件的演示界面布局activity_main2.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mly.panhouye.eventbustest.Main2Activity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:gravity="center_horizontal"
        android:id="@+id/tv"
        android:text="no data"/>
</RelativeLayout>

3.3java实现代码

(1)自定义事件类

本次演示最简单事件的发布,事件仅发布字符串数据,MessageEvent.java文件如下:

package com.mly.panhouye.eventbustest;

/**
 * Created by panchengjia on 2017/2/19 0019.
 */

public class MessageEvent {
    String data;
    public MessageEvent(String data) {
        this.data = data;
    }
}

(2)MsgFragment.java

右侧fragment对应的java类,除了在其中关联其对应的fragment布局外,还需要添加修改fragment中文本的方法,如下:

package com.mly.panhouye.eventbustest;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by panchengjia on 2017/2/20 0020.
 */

public class MsgFragment extends Fragment {
    TextView tv;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_msg,container,false);
        tv = (TextView) view.findViewById(R.id.tv);
        return view;
    }
    public void setText(String message){
        tv.setText(message);
    }
}

(3)MainActivity.java

MainActivity.java对应的布局为主布局,右侧的fragment附属于该布局,所以需要在该类中注册EventBus,将当前的Activity注册为事件订阅者,具体代码如下:

package com.mly.panhouye.eventbustest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button panhouye,bikonghai,



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Android中使用开源框架EventBus3.0实现Fragment之间的通信交互,fragment开源框架

相关文章

  • 2017-05-26android开发之路03,android之路03
  • 2017-05-26Android开发1:基本UI界面设计——布局和组件,androidui
  • 2017-05-26热修复-Nuwa学习篇,热修复-nuwa
  • 2017-05-26自定义组件,android自定义组件
  • 2017-05-26Android开发6:Service的使用(简单音乐播放器的实现),androidservice
  • 2017-05-26Android学习资料整理
  • 2017-05-228.3.6 Paint API之—— Xfermode与PorterDuff详解(三)
  • 2017-05-26JAVA Web day01--- Android小白的第一天学习笔记,day01---android
  • 2017-05-26android-studio的gradle plugin配置相关的一些记录,androidstudiogradle
  • 2017-05-26SwipeRefreshLayout下拉刷新,swiperefreshlayout

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 安卓开发—简单的登陆界面,安卓登陆界面
    • Android学习资料整理
    • 从源码角度入手实现RecyclerView的Item点击事件,recyclerviewitem
    • 安卓开发树形控件之ExpandableListView(一),expandablelistview
    • 关于我,关于我们
    • Android开发找工作之前先看看这些知识点吧
    • 手机无须ROOT不用修改hosts即可在本地测试安卓、苹果APP和H5应用,安卓h5
    • 安卓--获取应用版本名称与版本号,安卓--版本版本号
    • Android四大组件之Activity
    • 1、初识Activity,初识activity

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有