<?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>
<?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>
<?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>
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;
}
}
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);
}
}
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,postSticky;
MsgFragment msgFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
panhouye= (Button) findViewById(R.id.panhouye);
bikonghai= (Button) findViewById(R.id.bikonghai);
postSticky= (Button) findViewById(R.id.postSticky);
panhouye.setOnClickListener(this);
bikonghai.setOnClickListener(this);
postSticky.setOnClickListener(this);
//添加fragment到右侧的帧布局中
msgFragment = new MsgFragment();
getSupportFragmentManager().beginTransaction().add(R.id.framelayout,msgFragment).commit();
}
/*个人建议在onResume注册EventBus
*在可见可交互状态下注册,尽可能少的占用内存
*/
@Override
protected void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}
/*个人建议在onPause注册EventBus(将当前Activity注册为事件订阅者)
*不影响功能的情况下提早解除注册,尽可能少的占用内存
*/
@Override
protected void onPause() {
super.onPause();
EventBus.getDefault().unregister(this);
}
/**
* 事件发布者(通过按钮点击事件进行事件发布)
* @param v
*/
@Override
public void onClick(View v) {
switch (v.getId()){
//(1)事件发布中所传参数可以作为右侧fragment文本的修改内容
//(2)事件发布中所传参数也可以用作事件订阅者执行方法的区分通知
case R.id.panhouye:
EventBus.getDefault().post(new MessageEvent("潘侯爷"));
break;
case R.id.bikonghai:
EventBus.getDefault().post(new MessageEvent("碧空海"));
break;
case R.id.postSticky:
//粘性事件发布
EventBus.getDefault().postSticky(new MessageEvent("粘性事件"));
startActivity(new Intent(this,Main2Activity.class));
break;
}
}
/**
* 事件订阅者自定义的接收方法
* @param event
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
// //(1)将事件发布者发布的数据作为文本修改内容
// msgFragment.setText(event.data);
//(2)将事件发布者发布的数据作为方法执行的区分
switch(event.data){
case "潘侯爷":
msgFragment.setText("panhouye");
break;
case "碧空海":
msgFragment.setText("bikonghai");
break;
}
}
}
package com.mly.panhouye.eventbustest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class Main2Activity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv = (TextView) findViewById(R.id.tv);
}
@Override
protected void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}
@Override
protected void onPause() {
super.onPause();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void onMessageEvent(MessageEvent event) {
// //(1)将事件发布者发布的数据作为文本修改内容
tv.setText(event.data);
//(2)将事件发布者发布的数据作为方法执行的区分
// switch(event.data){
// case "粘性事件":
// tv.setText("panhouye");
// break;
// }
}
}
MessageEvent stickyEvent = EventBus.getDefault().getStickyEvent(MessageEvent.class);
// Better check that an event was actually posted before
if(stickyEvent != null) {
// "Consume" the sticky event
EventBus.getDefault().removeStickyEvent(stickyEvent);
// Now do something with it
}
MessageEvent stickyEvent = EventBus.getDefault().removeStickyEvent(MessageEvent.class);
// Better check that an event was actually posted before
if(stickyEvent != null) {
// Now do something with it
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有