(二)RxJava+RxBinding在View上的一些使用技巧

            

    1、View防止连续点击Demo
       不多说,很常用的功能 
         throttleFirst操作符:仅发送指定时间段内的第一个信号
RxView.clicks(btn_click)
.throttleFirst(3, TimeUnit.SECONDS)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
Toast.makeText(getActivity(), R.string.des_demo_not_more_click, Toast.LENGTH_SHORT).show();
}
});

    2、CheckBox状态更新相关Demo
           (1) 设置界面某项功能被打开或关闭,在 SharedPreferences中存储对应的开关标记,方便其他地方读取
            注:需要 RxSharedPreferences库支持: https://github.com/f2prateek/rx-preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
RxSharedPreferences rxPreferences = RxSharedPreferences.create(preferences);
Preference<Boolean> xxFunction = rxPreferences.getBoolean("xxFunction", false);

checkBox1.setChecked(xxFunction.get());

RxCompoundButton.checkedChanges(checkBox1)
.subscribe(xxFunction.asAction());
        (2)在用户登录界面时,如果用户未勾选同意用户协议,不允许登录
RxCompoundButton.checkedChanges(checkBox2)
.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean aBoolean) {
btn_login.setClickable(aBoolean);
btn_login.setBackgroundResource(aBoolean ? R.color.can_login : R.color.not_login);
}
});
效果图如下:

3、搜索关键字提醒
    搜索的关键字提醒功能,RxJava实现方式是如此的小清新。

         debounce操作符:    

RxTextView.textChangeEvents(et_search)
.debounce(300, TimeUnit.MILLISECONDS) //debounce:每次文本更改后有300毫秒的缓冲时间,默认在computation调度器
.observeOn(AndroidSchedulers.mainThread()) //触发后回到Android主线程调度器
.subscribe(new Action1<TextViewTextChangeEvent>() {
@Override
public void call(TextViewTextChangeEvent textViewTextChangeEvent) {
String key = textViewTextChangeEvent.text().toString().trim();
if (TextUtils.isEmpty(key)) {
iv_x.setVisibility(View.GONE);
if (mAdapter != null) {
mAdapter.clear();
mAdapter.notifyDataSetChanged();
}
} else {
iv_x.setVisibility(View.VISIBLE);
getKeyWordFormNet(key)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<String>>() {
@Override
public void call(List<String> strings) {
initPage(strings);
}
});
}
}
});
此处有些小问题:可以看到代码在获取到用户输入的字符后,便通过getKeyWordFromNet()方法拉去服务器匹配到的关键字,但这里明显是在RxJava中嵌套了RxJava代码,违背了Rxjava链式编程的初衷,本人第一时间想到用flatMap操作符进行转换链接,可flatMap中的call方法始终没有执行,诺有大神另有其他解决方案,还望给小弟解惑)

效果图如下:





  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值