Skip to content

Commit ab4d893

Browse files
添加samples
1 parent 79c6ee8 commit ab4d893

8 files changed

Lines changed: 97 additions & 20 deletions

File tree

‎.idea/gradle.xml‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/misc.xml‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/app.iml‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,26 @@
9696
</content>
9797
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
9898
<orderEntry type="sourceFolder" forTests="false" />
99-
<orderEntry type="library" exported="" name="okhttp-urlconnection-2.0.0" level="project" />
100-
<orderEntry type="library" exported="" name="leakcanary-watcher-1.3" level="project" />
101-
<orderEntry type="library" exported="" name="rxandroid-1.0.1" level="project" />
102-
<orderEntry type="library" exported="" name="support-annotations-23.1.0" level="project" />
103-
<orderEntry type="library" exported="" name="leakcanary-android-1.3" level="project" />
104-
<orderEntry type="library" exported="" name="butterknife-7.0.1" level="project" />
10599
<orderEntry type="library" exported="" name="gson-2.2.4" level="project" />
106-
<orderEntry type="library" exported="" name="design-23.1.0" level="project" />
107-
<orderEntry type="library" exported="" name="okio-1.0.0" level="project" />
108-
<orderEntry type="library" exported="" name="timber-2.4.2" level="project" />
100+
<orderEntry type="library" exported="" name="butterknife-7.0.1" level="project" />
109101
<orderEntry type="library" exported="" name="haha-1.1" level="project" />
110-
<orderEntry type="library" exported="" name="rxbinding-0.2.0" level="project" />
111-
<orderEntry type="library" exported="" name="rxjava-1.0.14" level="project" />
112-
<orderEntry type="library" exported="" name="recyclerview-v7-23.1.0" level="project" />
102+
<orderEntry type="library" exported="" name="timber-2.4.2" level="project" />
113103
<orderEntry type="library" exported="" name="retrofit-1.6.1" level="project" />
114-
<orderEntry type="library" exported="" name="appcompat-v7-23.1.0" level="project" />
104+
<orderEntry type="library" exported="" name="leakcanary-analyzer-1.3" level="project" />
105+
<orderEntry type="library" exported="" name="rxandroid-1.0.1" level="project" />
106+
<orderEntry type="library" exported="" name="design-23.1.0" level="project" />
107+
<orderEntry type="library" exported="" name="recyclerview-v7-23.1.0" level="project" />
115108
<orderEntry type="library" exported="" name="rxjava-math-1.0.0" level="project" />
116-
<orderEntry type="library" exported="" name="support-v13-23.0.1" level="project" />
109+
<orderEntry type="library" exported="" name="leakcanary-watcher-1.3" level="project" />
117110
<orderEntry type="library" exported="" name="okhttp-2.0.0" level="project" />
111+
<orderEntry type="library" exported="" name="rxjava-1.0.14" level="project" />
112+
<orderEntry type="library" exported="" name="rxbinding-0.2.0" level="project" />
113+
<orderEntry type="library" exported="" name="support-v13-23.0.1" level="project" />
114+
<orderEntry type="library" exported="" name="okio-1.0.0" level="project" />
115+
<orderEntry type="library" exported="" name="leakcanary-android-1.3" level="project" />
118116
<orderEntry type="library" exported="" name="support-v4-23.1.0" level="project" />
119-
<orderEntry type="library" exported="" name="leakcanary-analyzer-1.3" level="project" />
117+
<orderEntry type="library" exported="" name="appcompat-v7-23.1.0" level="project" />
118+
<orderEntry type="library" exported="" name="okhttp-urlconnection-2.0.0" level="project" />
119+
<orderEntry type="library" exported="" name="support-annotations-23.1.0" level="project" />
120120
</component>
121121
</module>

‎app/src/main/java/pers/example/xiayong/rxjavasamples/fragments/MainFragment.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ void demoBtnClick() {
9696
clickedOn(new ButtonClicksFragment());
9797
//clickedOn(new RotationPersist1Fragment());
9898
}
99+
@OnClick(R.id.btn_demo_reactive_UI)
100+
void demoUILogic(){
101+
clickedOn(new RxUIFragment());
102+
}
99103

100104
private void clickedOn(@NonNull Fragment fragment) {
101105
final String tag = fragment.getClass().toString();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package pers.example.xiayong.rxjavasamples.fragments;
2+
3+
import android.content.SharedPreferences;
4+
import android.os.Bundle;
5+
import android.preference.PreferenceManager;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.widget.CheckBox;
10+
import android.widget.Toast;
11+
12+
import com.jakewharton.rxbinding.widget.RxCompoundButton;
13+
14+
import butterknife.Bind;
15+
import butterknife.ButterKnife;
16+
import pers.example.xiayong.rxjavasamples.R;
17+
import rx.functions.Action1;
18+
19+
/**
20+
* 使用Rxjava做响应式界面.比如勾选了某个checkbox,自动更新对应的preference.
21+
*/
22+
public class RxUIFragment extends BaseFragment {
23+
24+
@Bind(R.id.checkBox)
25+
CheckBox checkBox;
26+
27+
@Override
28+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
29+
Bundle savedInstanceState) {
30+
// Inflate the layout for this fragment
31+
View view = inflater.inflate(R.layout.fragment_rx_ui, container, false);
32+
ButterKnife.bind(this, view);
33+
initCheckBox();
34+
return view;
35+
}
36+
37+
@Override
38+
public void onDestroyView() {
39+
super.onDestroyView();
40+
ButterKnife.unbind(this);
41+
}
42+
private void initCheckBox(){
43+
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
44+
// RxSharedPreferences rxPreferences = RxSharedPreferences.create(preferences);
45+
RxCompoundButton.checkedChanges(checkBox).subscribe(new Action1<Boolean>() {
46+
@Override
47+
public void call(Boolean aBoolean) {
48+
Toast.makeText(getContext(),"checked:"+aBoolean,Toast.LENGTH_SHORT).show();
49+
}
50+
});
51+
52+
53+
}
54+
}

‎app/src/main/res/layout/fragment_main.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,11 @@
118118
android:layout_width="match_parent"
119119
android:text="@string/btn_demo_click"
120120
/>
121+
<Button
122+
android:id="@+id/btn_demo_reactive_UI"
123+
android:layout_height="wrap_content"
124+
android:layout_width="match_parent"
125+
android:text="@string/btn_demo_reactive_UI"
126+
/>
121127
</LinearLayout>
122128
</ScrollView>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
3+
android:layout_height="match_parent"
4+
tools:context="pers.example.xiayong.rxjavasamples.fragments.RxUIFragment">
5+
6+
<!-- TODO: Update blank fragment layout -->
7+
8+
<CheckBox
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:text="CheckBox"
12+
android:id="@+id/checkBox"
13+
android:layout_gravity="left|center_vertical"
14+
android:checked="false" />
15+
</FrameLayout>

‎app/src/main/res/values/strings.xml‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<string name="btn_demo_exponential_backoff">Exponential backoff</string>
2020
<string name="btn_demo_rotation_persist">Rotation persist</string>
2121
<string name="btn_demo_click">Button Click with throttleFirst</string>
22+
<string name="btn_demo_reactive_UI">Use RxJava to do UI logic</string>
2223

2324
<string name="msg_demo_concurrency_schedulers">This is a demo of how long running operations can be offloaded to a background thread. After the operation is done, we resume back on the main thread. All using RxJava! \n\n To really see this shine. Hit the button multiple times and see how the button click which is a ui operation is never blocked because the long operation only runs in the background</string>
2425
<string name="msg_demo_buffer">This is a demo of how events can be accumulated using the "buffer" operation. Tap the button below repetitively and you will notice in the logs that button taps are collected over a span of 2s and printed below.</string>
@@ -33,8 +34,5 @@
3334
<string name="msg_demo_form_comb_latest">Monitor the state of multiple observables with the combineLatest operator. Only after all the 3 inputs contain valid entries will the submit button light up</string>
3435
<string name="msg_demo_timing">BTN 1: run single task once (after 2s complete)\nBTN 2: run task every 1s (start delay of 1s) toggle \nBTN 3: run task every 1s (start immediately) toggle \nBTN 4: run task 5 times every 3s (then complete)</string>
3536
<string name="msg_demo_rotation_persist">This is an example of starting an Observable and using the result across rotations. There are many ways to do this, we use a retained fragment in this example</string>
36-
37-
<!-- TODO: Remove or change this placeholder text -->
38-
<string name="hello_blank_fragment">Hello blank fragment</string>
3937
<string name="tv_click">展示了如何使用RxJava防止按钮连续点击</string>
4038
</resources>

0 commit comments

Comments
 (0)