Kotlin中RxBus的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/YANGWEIQIAO/article/details/78959968

1.基础的知识这里不计划多说,直接开始使用

1.导入依赖包

 compile 'com.eightbitlab:rxbus:1.0.2'

2.在需要事件监听的地方 注册

      Bus.observe<UpdateTotalPriceEvent>()//UpdateTotalPriceEvent这个是event类 
                .subscribe {
                    updateTotalPrice() //这里是购物车数据的变化 更新购物车价格
                }
                .registerInBus(this)

看下event类

/*
更新购物车事件
 */
class UpdateTotalPriceEvent //在kotlin中的写法 

3.这个地方注册了 然后我们必须在onDestroy()方法中反注册


 Bus.unregister(this) //就这么简单 

4.在需要发送事件的地方

 holder.itemView.mGoodsCountBtn.getEditText() //这个地方是在购物车的适配器中监听数量的变化 
                .addTextChangedListener(object :DefaultTextWatcher(){
                    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                        super.onTextChanged(s, start, before, count)
                        cartGoods.goodsCount = s.toString().toInt()
                        Bus.send(UpdateTotalPriceEvent()) //这里发送更新价格的事件 
                    }
                })

ok,就这样,没了

猜你喜欢

转载自blog.csdn.net/YANGWEIQIAO/article/details/78959968