{"id":395,"date":"2023-02-25T08:49:25","date_gmt":"2023-02-25T00:49:25","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=395"},"modified":"2023-04-30T14:37:02","modified_gmt":"2023-04-30T06:37:02","slug":"rxjava2-learning-7-keyword-search","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/rxjava2-learning-7-keyword-search\/","title":{"rendered":"RxJava2\u5b66\u4e60\u4e4b\u4e03\uff1a\u5173\u952e\u8bcd\u641c\u7d22"},"content":{"rendered":"<h2>TextWatcher\u76d1\u542c\u7684\u5f0a\u7aef<\/h2>\n<p>\u4e00\u822c\u60c5\u51b5\u6211\u4eec\u76d1\u542cEditText\u63a7\u4ef6\uff0c\u5f53\u503c\u53d1\u751f\u6539\u53d8\u53bb\u8bf7\u6c42\u641c\u7d22\u63a5\u53e3\uff0c\u5982\u4e0b\uff1a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">EditText searchText = (EditText) this.findViewById(R.id.search_text);\nsearchText.addTextChangedListener(new TextWatcher() {\n    @Override\n    public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {\n\n    }\n\n    @Override\n    public void onTextChanged(CharSequence charSequence, int start, int before, int count) {\n\n    }\n\n    @Override\n    public void afterTextChanged(Editable editable) {\n        \/\/search(editable.toString().trim());  \/\/\u8bf7\u6c42\u641c\u7d22\u63a5\u53e3\n    }\n});<\/code><\/pre>\n<p>\u8fd9\u91cc\u5b58\u5728\u4e24\u4e2a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\uff081\uff09\u53ef\u80fd\u5bfc\u81f4\u5f88\u591a\u6ca1\u6709\u610f\u4e49\u7684\u8bf7\u6c42\uff0c\u8017\u8d39\u7528\u6237\u6d41\u91cf<\/li>\n<\/ul>\n<p>\u56e0\u4e3a\u63a7\u4ef6\u7684\u503c\u6bcf\u66f4\u6539\u4e00\u6b21\u7acb\u5373\u5c31\u4f1a\u53bb\u8bf7\u6c42\u7f51\u7edc\uff0c\u800c\u4e14\u53ea\u662f\u6700\u540e\u8f93\u5165\u7684\u5173\u952e\u5b57\u662f\u6709\u7528\u7684\u3002<\/p>\n<ul>\n<li>\uff082\uff09\u53ef\u80fd\u5bfc\u81f4\u6700\u7ec8\u641c\u7d22\u7684\u7ed3\u679c\u4e0d\u662f\u7528\u6237\u60f3\u8981\u7684<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u7528\u6237\u4e00\u5f00\u59cb\u8f93\u5165\u5173\u952e\u5b57 \u2019AB\u2019 \u8fd9\u4e2a\u65f6\u5019\u51fa\u73b0\u4e24\u4e2a\u8bf7\u6c42\uff0c\u4e00\u4e2a\u8bf7\u6c42\u662fA\u5173\u952e\u5b57\uff0c \u4e00\u4e2a\u8bf7\u6c42\u662fAB\u5173\u952e\u5b57\u3002\u8868\u9762\u4e0a\u662f\u2019A\u2019\u8bf7\u6c42\u5148\u53d1\u51fa\u53bb\uff0c\u2018AB\u2019\u8bf7\u6c42\u540e\u53d1\u51fa\u53bb\u3002\u5982\u679c\u540e\u53d1\u51fa\u53bb\u7684\u2019AB\u2019\u8bf7\u6c42\u5148\u8fd4\u56de\uff0c\u2018A\u2019\u8bf7\u6c42\u540e\u8fd4\u56de\uff0c\u90a3\u4e48\u2019A\u2019\u8bf7\u6c42\u540e\u7684\u7ed3\u679c\u5c06\u4f1a\u8986\u76d6\u2019AB\u2019\u8bf7\u6c42\u7684\u7ed3\u679c\uff0c\u4ece\u800c\u5bfc\u81f4\u641c\u7d22\u7ed3\u679c\u4e0d\u6b63\u786e\u3002<\/p>\n<h2>RxJava\u5b9e\u73b0<\/h2>\n<h3>RxBinding<\/h3>\n<p>RxJava\u5b9e\u73b0\u6570\u636e\u4e0e\u89c6\u56fe\u7684\u7ed1\u5b9a\u4f7f\u7528\u5230\u4e86RxBinding\u3002<\/p>\n<p>RxBinding\u662f\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u5c42\u4e0eView\u5c42\u7684\u7ed1\u5b9a\uff0c\u5f53\u6570\u636e\u53d1\u751f\u53d8\u5316\uff0cView\u4f1a\u81ea\u52a8\u66f4\u65b0UI\u3002<\/p>\n<p>RxBinding\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/JakeWharton\/RxBinding\">https:\/\/github.com\/JakeWharton\/RxBinding<\/a><\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1aRxBinding\u76ee\u524d\u53ea\u652f\u6301RxJava1<\/p>\n<\/blockquote>\n<h3>RxJava\u64cd\u4f5c\u7b26<\/h3>\n<p>\u672c\u4f8b\u6d89\u53ca\u7684RxJava\u64cd\u4f5c\u7b26\uff1a<\/p>\n<ul>\n<li><strong>Debounce<\/strong> \u2014 <em>\u53ea\u6709\u5728\u7a7a\u95f2\u4e00\u6bb5\u65f6\u95f4\u540e\u624d\u53d1\u5c04\u6570\u636e\uff0c\u901a\u4fd7\u7684\u8bf4\uff0c\u5c31\u662f\u5982\u679c\u4e00\u6bb5\u65f6\u95f4\u6ca1\u6709\u64cd\u4f5c\uff0c\u5c31\u6267\u884c\u4e00\u6b21\u64cd\u4f5c<\/em><\/li>\n<li><strong>Filter<\/strong> \u2014 <em>\u8fc7\u6ee4\uff0c\u8fc7\u6ee4\u6389\u6ca1\u6709\u901a\u8fc7\u8c13\u8bcd\u6d4b\u8bd5\u7684\u6570\u636e\u9879\uff0c\u53ea\u53d1\u5c04\u901a\u8fc7\u6d4b\u8bd5\u7684<\/em><\/li>\n<li><strong>SwitchMap<\/strong> \u2014 <em>\u5c06\u4e00\u4e2a\u53d1\u5c04Observable\u5e8f\u5217\u7684Observable\u8f6c\u6362\u4e3a\u8fd9\u6837\u4e00\u4e2aObservable\uff1a\u5b83\u9010\u4e2a\u53d1\u5c04\u90a3\u4e9bObservable\u6700\u8fd1\u53d1\u5c04\u7684\u6570\u636e<\/em><\/li>\n<\/ul>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ol>\n<li><strong>Debounce<\/strong> \u89e3\u51b3TextWatcher\u76d1\u542c\u7684\u7b2c\u4e00\u4e2a\u95ee\u9898\uff1b<\/li>\n<li><strong>SwitchMap<\/strong> \u662f\u6709\u5e8f\u7684FlatMap\uff0c\u89e3\u51b3TextWatcher\u76d1\u542c\u7684\u7b2c\u4e8c\u4e2a\u95ee\u9898\u3002<\/li>\n<\/ol>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<h4>\u6dfb\u52a0\u4f9d\u8d56<\/h4>\n<pre><code>compile &#039;com.squareup.retrofit2:retrofit:2.1.0&#039;\ncompile &#039;com.squareup.retrofit2:converter-gson:2.1.0&#039;\ncompile &#039;com.jakewharton.rxbinding:rxbinding:1.0.0&#039;<\/code><\/pre>\n<h4>RxJava\u5b9e\u73b0<\/h4>\n<pre><code class=\"language-java\">EditText searchText = (EditText) this.findViewById(R.id.search_text);\n\/\/\u6ce8\u610f\uff1aRxBinding\u76ee\u524d\u53ea\u652f\u6301RxJava1\nRxTextView.textChanges(searchText)\n        .debounce(500, TimeUnit.MILLISECONDS)\n        .filter(new Func1&lt;CharSequence, Boolean&gt;() {\n            @Override\n            public Boolean call(CharSequence charSequence) {\n                \/\/\u8fc7\u6ee4\u6570\u636e\n                return charSequence.toString().trim().length() &gt; 0;\n            }\n        })\n        .subscribeOn(rx.android.schedulers.AndroidSchedulers.mainThread())\n        .switchMap(new Func1&lt;CharSequence, rx.Observable&lt;List&lt;String&gt;&gt;&gt;() {\n            @Override\n            public rx.Observable&lt;List&lt;String&gt;&gt; call(CharSequence charSequence) {\n                Log.i(TAG, &quot;flatMap: &quot; + charSequence);\n                \/\/\u6a21\u62df\u7f51\u7edc\u8bf7\u6c42\u54cd\u5e94\n                List&lt;String&gt; responses = new ArrayList&lt;String&gt;();\n                responses.add(&quot;ok&quot;);\n                responses.add(&quot;success&quot;);\n                return rx.Observable.just(responses);\n            }\n        })\n        \/*\n        .flatMap(new Func1&lt;CharSequence, rx.Observable&lt;List&lt;String&gt;&gt;&gt;() {\n            @Override\n            public rx.Observable&lt;List&lt;String&gt;&gt; call(CharSequence charSequence) {\n                Log.i(TAG, &quot;flatMap: &quot; + charSequence);\n                List&lt;String&gt; responses = new ArrayList&lt;String&gt;();\n                responses.add(&quot;ok&quot;);\n                responses.add(&quot;success&quot;);\n                return rx.Observable.just(responses);\n            }\n        })\n        *\/\n        .subscribeOn(rx.schedulers.Schedulers.io())\n        .observeOn(rx.android.schedulers.AndroidSchedulers.mainThread())\n        .subscribe(new Action1&lt;List&lt;String&gt;&gt;() {\n            @Override\n            public void call(List&lt;String&gt; strings) {\n                Log.i(TAG, &quot;subscribe: &quot; + strings);\n            }\n        }, new Action1&lt;Throwable&gt;() {\n            @Override\n            public void call(Throwable throwable) {\n                throwable.printStackTrace();\n            }\n        });<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>TextWatcher\u76d1\u542c\u7684\u5f0a\u7aef \u4e00\u822c\u60c5\u51b5\u6211\u4eec\u76d1\u542cEditText\u63a7\u4ef6\uff0c\u5f53\u503c\u53d1\u751f\u6539\u53d8\u53bb\u8bf7\u6c42\u641c\u7d22\u63a5\u53e3\uff0c\u5982\u4e0b\uff1a E [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[131],"tags":[132],"class_list":["post-395","post","type-post","status-publish","format-standard","hentry","category-rxjava","tag-rxjava"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=395"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/395\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}