EventBus在Android中的简单使用

EventBus是一个方便与Android中各组件通信的开源框架,开源地址;https://github.com/greenrobot/EventBus。EventBus功能很强大 ,今天在做一个功能时,遇到了点击事件的冲突问题及数据传递更新问题。具体点就是在可以上下拉刷新的ListView的Header上有GridView。此时,GridView的OnitemClick事件与ListView的OnItemClick事件冲突。而且我在实现 Griview的点击事件时,还要进行数据传递。

解决过程1:

OnitemClick点击冲突问题,我把在代码中的OnitemClick事件注释了,然后在GridView的item布局根据加了id,然后在Adapter中对整个item做了个OnClick()点击事件,,问题解决了。

解决过程1:数据传递问题,就用到了EventBus:

在Activity中注册EventBus:在Oncreate()方法中;EventBus.getDefault().register(this);

然后在OnStop()或OnDestory()中:EventBus.getDefault().unregister(this);

事件处理方法onEventMainThread:

public void onEventMainThread(CategoryItemEvent event) {Bundle bundle = new Bundle();bundle.putInt(…);…….//传递数据,实现跳转}

在Adapter中做的就是POST事件;

holder.categoryRela.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {EventBus.getDefault().post(new CategoryItemEvent(pos));//在post的时候,传递了一个int类型的参数pos,这个参数在Activity中EventBus执行onEventMainThread(CategoryItemEvent event)时会用到这个pos。}});

版权声明:本文为博主原创文章,未经博主允许不得转载。

爱情不是避难所,想进去避难的话,是会被赶出来的。

EventBus在Android中的简单使用

相关文章:

你感兴趣的文章:

标签云: