EventBus笔记

EventBus笔记:
引入依赖:
compile 'org.greenrobot:eventbus:3.0.0'
总结:
【EventBus】
==================================================
·基于事件发布和订阅的,实现一处发布多处接收处理的框架
·跨线程、跨组件(四大组件、Fragment...),不能跨进程
·支持延时接收和处理
·取代广播、listener接口回调...
----------------------------------------
@引入依赖
·compile 'org.greenrobot:eventbus:3.0.0'
----------------------------------------
@发布事件
·EventBus.getDefault().post(object);
----------------------------------------
@订阅事件
·注册订阅者
EventBus.getDefault().register(this);
·注销订阅者
EventBus.getDefault().unregister(this);
·处理订阅事件
@Subscribe(threadMode = ThreadMode.MAIN)
public void dealGenerateEvent(GenerateEvent generateEvent){
generateCount++;
updateCount();
}
----------------------------------------
@ThreadMode——在哪条线程处理订阅事件
·MAIN 主线程
·BACKGROUND 后台线程,同步(AA关系:事件发布在子线程A,事件处理在线程A)
·ASYNC 后台线程,异步(AB关系:事件发布在子线程A,事件处理在线程B)
·POSTING 发送线程(默认)
----------------------------------------
@priority
·取值范围0~100,默认为0
·值越小优先级越低,即对事件处理的顺序靠后
----------------------------------------
@sticky
·发送时要以postSticky方式发送:
EventBus.getDefault().postSticky(new VoteEvent(obj));
·订阅方如果要做sticky方式的处理,需要声明@Subscribe(sticky=true)
·讨论是否延迟处理:
*不存在延迟处理:以postSticky方式发送的事件的时候,订阅方如果已经注册过了,非sticky方式的处理和sticky方式的处理都会执行
*存在延迟处理:以postSticky方式发送的事件的时候,如果未注册,那么随后只能执行sticky方式的处理
·同种sticky事件会向前覆盖:以postSticky方式发送的同种类型事件,如果订阅方存在延迟处理的话,则只能处理最后一次发送的该种类型事件
----------------------------------------
1 发送事件有普通发布与sticky类型发布:post(event)/postSticky(event)
2 订阅者处理事件有普通处理与sticky处理:@Subscribe(sticky=false)/@Subscribe(sticky=true)
3 sticky事件发布时,如果订阅者已经注册,则普通处理与sticky处理都会执行
4 sticky事件发布时,如果订阅者未注册,则稍后只有sticky处理会执行
5 连续发布多条同种类型的sticky事件,之后订阅者才注册,则只有最后一次sticky事件会被处理
=================== END ==========================
代码:
public class Fragment1 extends Fragment {

private View view;
private ImageView iv;
@Override
public void onAttach(Context context) {
super.onAttach(context);
EventBus.getDefault().register(this);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(view==null) {
view = inflater.inflate(R.layout.deng,null,false);
iv= (ImageView) view.findViewById(R.id.iv);
}
return view;
}

@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}

@Subscribe
public void onEvent(Boolean b){
if(b){
iv.setImageResource(R.mipmap.bulb_on);
}else{
iv.setImageResource(R.mipmap.bulb_off);
}
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public class Fragment2 extends Fragment {

private View view;
private ImageView iv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(view==null) {
view = inflater.inflate(R.layout.deng1,null,false);
iv= (ImageView) view.findViewById(R.id.iv);
}
return view;
}
public void onAttach(Context context) {
super.onAttach(context);
EventBus.getDefault().register(this);
}
@Subscribe
public void onEvent(Boolean b){
if(b){
iv.setImageResource(R.mipmap.bulb_on);
}else{
iv.setImageResource(R.mipmap.bulb_off);
}
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public class Fragment3 extends Fragment {
private View view;
private ImageView iv;
public void onAttach(Context context) {
super.onAttach(context);
EventBus.getDefault().register(this);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(view==null) {
view = inflater.inflate(R.layout.deng2,null,false);
iv= (ImageView) view.findViewById(R.id.iv);
}
return view;
}
@Subscribe
public void onEvent(Boolean b){
if(b){
iv.setImageResource(R.mipmap.bulb_on);
}else{
iv.setImageResource(R.mipmap.bulb_off);
}
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public class Fragment4 extends Fragment {
private View view;
ToggleButton tb;
CheckBox cb1,cb2,cb3;
TrafficEvents events=new TrafficEvents();
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (view == null) {
view=inflater.inflate(R.layout.switcher,null,false);
tb= (ToggleButton) view.findViewById(R.id.tb);
cb1= (CheckBox) view.findViewById(R.id.cb1);
cb2= (CheckBox) view.findViewById(R.id.cb2);
cb3= (CheckBox) view.findViewById(R.id.cb3);
tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
EventBus.getDefault().post(isChecked);
events.powerOn=isChecked;
postSticky();

}
});
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
events.redOn=isChecked;
postSticky();
}
});
cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

events.greenOn=isChecked;
postSticky();
}
});
cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
events.yellowOn=isChecked;
postSticky();
}
});
}
return view;
}
private void postSticky() {
EventBus.getDefault().postSticky(events);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public class MainActivity extends AppCompatActivity {

private ViewPager iv;
private MyAdapter adapter;
private List<Fragment> list;
private FragmentManager fm=getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ViewPager)findViewById(R.id.iv);
iv.setOffscreenPageLimit(3); //设置加载左右3页
intoData();
adapter=new MyAdapter(fm,list);
iv.setAdapter(adapter);
}
private void intoData() {
list=new ArrayList<>();
list.add(new Fragment4());
list.add(new Fragment1());
list.add(new Fragment2());
list.add(new Fragment3());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_deng,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.yk){
Intent intent = new Intent(this, YActivity.class);
startActivity(intent);
}
return true;
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public class MyAdapter extends FragmentPagerAdapter {
private List<Fragment> list;
public MyAdapter(FragmentManager fm,List<Fragment> list) {
super(fm);
this.list=list;
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public class TrafficEvents {
public boolean redOn;
public boolean greenOn;
public boolean yellowOn;
public boolean powerOn;
public TrafficEvents(boolean redOn, boolean greenOn, boolean yellowOn,boolean powerOn) {
this.redOn = redOn;
this.greenOn = greenOn;
this.yellowOn = yellowOn;
this.powerOn=powerOn;
}
public TrafficEvents() {
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public class TrafficEvents {

public boolean redOn;
public boolean greenOn;
public boolean yellowOn;
public boolean powerOn;
public TrafficEvents(boolean redOn, boolean greenOn, boolean yellowOn,boolean powerOn) {
this.redOn = redOn;
this.greenOn = greenOn;
this.yellowOn = yellowOn;
this.powerOn=powerOn;
}
public TrafficEvents() {
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
布局代码:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.eventbus.MainActivity">

<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:id="@+id/iv"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.eventbus.YActivity">

<LinearLayout
android:layout_width="350dp"
android:layout_centerInParent="true"
android:layout_height="200dp">

<ImageView
android:id="@+id/iv1"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@mipmap/bulb_off"
android:background="#f00"
android:layout_height="200dp" />

<ImageView
android:id="@+id/iv2"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@mipmap/bulb_off"
android:background="#0f0"
android:layout_height="200dp" />

<ImageView
android:id="@+id/iv3"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@mipmap/bulb_off"
android:background="#ff0"
android:layout_height="200dp" />

</LinearLayout>
</RelativeLayout>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="match_parent"
android:id="@+id/iv"
android:background="@mipmap/bulb_off"
android:layout_height="match_parent" />
</LinearLayout>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#0f0"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="match_parent"
android:id="@+id/iv"
android:background="@mipmap/bulb_off"
android:layout_height="match_parent" />
</LinearLayout>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#f4f006"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="match_parent"
android:id="@+id/iv"
android:background="@mipmap/bulb_off"
android:layout_height="match_parent" />
</LinearLayout>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ToggleButton
android:id="@+id/tb"
android:layout_width="200dp"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_height="60dp" />



<LinearLayout
android:layout_width="match_parent"
android:layout_below="@+id/tb"
android:orientation="horizontal"
android:paddingLeft="40dp"
android:layout_marginTop="50dp"
android:layout_height="match_parent">
<CheckBox
android:text="红灯"
android:id="@+id/cb1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>

<CheckBox
android:text="绿灯"
android:id="@+id/cb2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<CheckBox
android:text="黄灯"
android:id="@+id/cb3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/yk"
android:icon="@mipmap/ic_launcher"
android:title="实时路况"
app:showAsAction="always" />
</menu>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
运行如果:







猜你喜欢

转载自blog.csdn.net/yang__k/article/details/80216376