Eventbus框架的使用
说明:这是一个非常好的android组件间消息传递和消息通信的框架,传递数据非常的方便快捷,这里写一个简单的小例子去使用它,我让eventbus实现在两个fragemnt之间的消息传递
1.先看效果图
2.在builder.gradle中,添加依赖
compile 'org.greenrobot:eventbus:3.0.0'
3.创建一个消息类,用于数据处理的
public class StringEvent {
private String texto;
public StringEvent(String texto) {
this.texto = texto;
}
public String getTexto() {
return texto;
}
public void setTexto(String texto) {
this.texto = texto;
}
}
4.主界面啥也不干,就放置两个fragment就可以了,下面书主界面的layout布局文件
<?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">
<fragment
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:name="com.mac.fireflies.wgt.eventbus.ItemsFragment"
android:id="@+id/fragment"
tools:layout="@layout/fragment_items" />
<fragment
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:name="com.mac.fireflies.wgt.eventbus.ContentFragment"
android:id="@+id/fragment2"
tools:layout="@layout/fragment_content" />
</LinearLayout>
5.大家仔细看上面的布局文件,可以看到有两个fragemnt 这个就是我们的主角,一个接收页面,一个发送页面
先写发送页面 ItemsFragment
package com.mac.fireflies.wgt.eventbus;
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.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
/**
* A simple {@link Fragment} subclass.
*/
public class ItemsFragment extends Fragment {
private Button button;
private EditText textView;
public ItemsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_items, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
button = (Button) view.findViewById(R.id.item_button);
textView = (EditText) view.findViewById(R.id.item_et);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String res = textView.getText().toString().trim();
EventBus.getDefault().post(new StringEvent(res));
}
});
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Subscribe
public void ShowTextEventAnswer(StringEvent answer){
}
}
发送的布局文件,一个书edittext 还有一个书button 因为过于简单,这里不写了,大家参考上面的效果图来写布局
6.在写一个接收的fragment
package com.mac.fireflies.wgt.eventbus;
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;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
/**
* A simple {@link Fragment} subclass.
*/
public class ContentFragment extends Fragment {
private TextView textView;
public ContentFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_content, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
textView = (TextView) view.findViewById(R.id.content_text);
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Subscribe
public void showTextEvent(StringEvent stringEvent){
textView.setText(stringEvent.getTexto());
}
}
运行一下,会发现,string字段消息在两个fragment之间进行传递
联系方式:qq:2494075190