├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── captain_miao │ │ └── agera │ │ └── tutorial │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── ic_launcher-web.png │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── captain_miao │ │ │ └── agera │ │ │ └── tutorial │ │ │ ├── AppAgera.java │ │ │ ├── MainActivity.java │ │ │ ├── SimpleActivityA.java │ │ │ ├── SimpleActivityB.java │ │ │ ├── SimpleActivityC.java │ │ │ ├── SimpleActivityH.java │ │ │ ├── SimpleActivityI.java │ │ │ ├── SimpleFragmentC.java │ │ │ ├── base │ │ │ ├── BaseActivity.java │ │ │ ├── BaseFragment.java │ │ │ └── BasePagerAdapter.java │ │ │ ├── helper │ │ │ ├── ActivityNavigation.java │ │ │ ├── MockRandomData.java │ │ │ ├── PicassoBinding.java │ │ │ ├── PicassoOnScrollListener.java │ │ │ ├── RecyclingBitmapDrawable.java │ │ │ ├── RecyclingImageView.java │ │ │ ├── UiThreadExecutor.java │ │ │ └── ViewVisibleBindingAdapter.java │ │ │ ├── http │ │ │ ├── DemoApiService.java │ │ │ └── RetrofitServiceFactory.java │ │ │ ├── listener │ │ │ ├── OnViewClickListener.java │ │ │ └── SimpleObservable.java │ │ │ ├── model │ │ │ ├── ActInfo.java │ │ │ ├── ApiResult.java │ │ │ ├── BaseModel.java │ │ │ ├── BaseObservableVehicleInfo.java │ │ │ ├── GirlInfo.java │ │ │ ├── ObservableVehicleInfo.java │ │ │ └── VehicleInfo.java │ │ │ ├── observable │ │ │ └── OnClickObservable.java │ │ │ ├── recycleview │ │ │ ├── ComplexRecycleViewActivity.java │ │ │ ├── ComplexRvAdapter.java │ │ │ ├── GirlInfoPresenter.java │ │ │ ├── GirlListAdapter.java │ │ │ ├── RecycleViewActivity.java │ │ │ └── RepositoryAdapterRecycleViewActivity.java │ │ │ ├── supplier │ │ │ ├── GirlsSupplier.java │ │ │ └── ImageSupplier.java │ │ │ └── viewpage │ │ │ ├── GuideViewModel.java │ │ │ ├── ViewPageActivity.java │ │ │ └── ViewPageDotView.java │ └── res │ │ ├── anim │ │ ├── slide_in_left.xml │ │ ├── slide_in_right.xml │ │ ├── slide_out_left.xml │ │ └── slide_out_right.xml │ │ ├── drawable-xhdpi │ │ ├── guide_1.png │ │ ├── guide_2.png │ │ └── guide_3.png │ │ ├── drawable │ │ ├── ic_check_circle.xml │ │ ├── ic_image_load_error.xml │ │ ├── ic_image_load_place_holder.xml │ │ └── ic_uncheck_circle.xml │ │ ├── layout │ │ ├── activity_agera_functions.xml │ │ ├── activity_agera_reservoir.xml │ │ ├── activity_guide.xml │ │ ├── activity_item_view.xml │ │ ├── activity_main.xml │ │ ├── activity_recycle_view.xml │ │ ├── activity_simple.xml │ │ ├── base_observable_recycler_item_view.xml │ │ ├── change_txt_color.xml │ │ ├── fragment_simple.xml │ │ ├── load_image_by_picasso.xml │ │ ├── load_image_by_url.xml │ │ ├── observable_recycler_item_view.xml │ │ ├── recycler_item_view.xml │ │ └── view_page_item_view.xml │ │ ├── menu │ │ └── main_menu.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── github │ └── captain_miao │ └── agera │ └── tutorial │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Android Studio Navigation editor temp files 29 | .navigation/ 30 | 31 | # Android Studio captures folder 32 | captures/ 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidAgeraTutorial 2 | 3 | ## [Agera-Wiki-CN](https://github.com/captain-miao/AndroidAgeraTutorial/wiki) 4 | ## [Agera-Tutorial-CN](https://yanlu.me/android-agera-tutorial-00/) 5 | 6 | Android Agera Example. 7 | 8 | 9 | # Change Text Color 10 | ## Click Button Send Update Event 11 | 12 | ``` 13 | //onClick Observable 14 | mObservable = new OnClickObservable() { 15 | @Override 16 | public void onClick(View view) { 17 | dispatchUpdate(); 18 | } 19 | }; 20 | //when click button, then dispatchUpdate() 21 | mBinding.setObservable(mObservable); 22 | android:onClick="@{observable::onClick}" 23 | ``` 24 | 25 | ## Repository 26 | 27 | ``` 28 | //text color Supplier 29 | Supplier supplier = new Supplier() { 30 | @NonNull 31 | @Override 32 | public Integer get() { 33 | return MockRandomData.getRandomColor(); 34 | } 35 | }; 36 | 37 | mRepository = Repositories.repositoryWithInitialValue(0) 38 | .observe(mObservable) 39 | .onUpdatesPerLoop() 40 | .thenGetFrom(supplier) 41 | .compile(); 42 | 43 | @Override 44 | protected void onResume() { 45 | super.onResume(); 46 | mRepository.addUpdatable(this); 47 | } 48 | 49 | @Override 50 | protected void onPause() { 51 | super.onPause(); 52 | mRepository.removeUpdatable(this); 53 | } 54 | ``` 55 | 56 | ## setTxtColor 57 | 58 | ``` 59 | @Override 60 | public void update() { 61 | mBinding.setTxtColor(mRepository.get()); 62 | } 63 | ``` 64 | # MutableRepository 65 | ``` 66 | private void setUpRepository() { 67 | mObservable = new OnClickObservable() { 68 | @Override 69 | public void onClick(View view) { 70 | mRepository.accept(MockRandomData.getRandomImage()); 71 | } 72 | }; 73 | 74 | mRepository = Repositories.mutableRepository(MockRandomData.getRandomImage()); 75 | 76 | //initialization 77 | mRepository.accept(MockRandomData.getRandomImage()); 78 | } 79 | 80 | @Override 81 | public void update() { 82 | String result = mRepository.get(); 83 | mBinding.setImageUrl(result); 84 | } 85 | ``` 86 | # Load Image By Picasso 87 | ## Repository 88 | ``` 89 | 90 | Supplier imageUriSupplier = new Supplier() { 91 | @NonNull 92 | @Override 93 | public String get() { 94 | return MockRandomData.getRandomImage(); 95 | } 96 | }; 97 | 98 | mRepository = Repositories.repositoryWithInitialValue(Result.absent()) 99 | .observe(mObservable) 100 | .onUpdatesPerLoop() 101 | .getFrom(imageUriSupplier)//image uri 102 | .goTo(networkExecutor) 103 | .thenTransform(new Function>() { 104 | @NonNull 105 | @Override 106 | public Result apply(@NonNull String input) { 107 | return new ImageSupplier(input).get();//image bitmap 108 | } 109 | }) 110 | .compile(); 111 | ``` 112 | 113 | # Load Data By Network 114 | ## Repository 115 | ``` 116 | // Observable and Supplier 117 | mMutableRepository = Repositories.mutableRepository(mPagination); 118 | 119 | mLoadDataRepository = Repositories.repositoryWithInitialValue(Result.>absent()) 120 | .observe(mMutableRepository) 121 | .onUpdatesPerLoop() 122 | .goTo(networkExecutor) 123 | .attemptGetFrom(new GirlsSupplier(mMutableRepository)).orSkip() 124 | .goLazy() 125 | .thenTransform(new Function, Result>>() { 126 | @NonNull 127 | @Override 128 | public Result> apply(@NonNull ApiResult input) { 129 | return absentIfNull(input); 130 | } 131 | }) 132 | .compile(); 133 | ``` 134 | 135 | # UiThreadExecutor 136 | ``` 137 | public class UiThreadExecutor implements Executor { 138 | private final Handler mHandler = new Handler(Looper.getMainLooper()); 139 | @Override 140 | public void execute(Runnable command) { 141 | mHandler.post(command); 142 | } 143 | 144 | // how to release it? 145 | public static Executor newUiThreadExecutor() { 146 | return new UiThreadExecutor(); 147 | } 148 | } 149 | ``` 150 |
151 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | captures/ 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | reports/ 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | .DS_Store 29 | .idea 30 | .iml 31 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion '25.0.0' 6 | 7 | defaultConfig { 8 | applicationId "com.github.captain_miao.agera.tutorial" 9 | minSdkVersion this.minSdkVersion 10 | targetSdkVersion this.targetSdkVersion 11 | versionCode this.versionCode 12 | versionName this.versionName 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | 21 | lintOptions { 22 | abortOnError false 23 | } 24 | 25 | // enable dataBinding 26 | dataBinding { 27 | enabled = true 28 | } 29 | } 30 | 31 | dependencies { 32 | compile fileTree(dir: 'libs', include: ['*.jar']) 33 | testCompile 'junit:junit:4.12' 34 | compile "com.android.support:appcompat-v7:${this.supportLibrariesVersion}" 35 | compile "com.android.support:design:${this.supportLibrariesVersion}" 36 | compile 'com.squareup.picasso:picasso:2.6.0-SNAPSHOT' 37 | compile 'com.github.captain-miao:recyclerviewutils:1.2.3' 38 | 39 | //agera 40 | compile "com.google.android.agera:agera:${this.ageraLibrariesVersion}" 41 | compile "com.google.android.agera:content:${this.ageraLibrariesVersion}" 42 | compile "com.google.android.agera:database:${this.ageraLibrariesVersion}" 43 | compile "com.google.android.agera:net:${this.ageraLibrariesVersion}" 44 | compile "com.google.android.agera:rvadapter:${this.ageraLibrariesVersion}" 45 | 46 | //okhttp and retrofit 47 | compile 'com.squareup.retrofit2:retrofit:2.0.2' 48 | compile 'com.squareup.retrofit2:converter-gson:2.0.2' 49 | } 50 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/captain_miao/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # for retrofit2 20 | -dontwarn retrofit2.** 21 | -keep class retrofit2.** { *; } 22 | -keepattributes Signature 23 | -keepattributes Exceptions -------------------------------------------------------------------------------- /app/src/androidTest/java/com/github/captain_miao/agera/tutorial/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 15 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 29 | 30 | 31 | 35 | 36 | 37 | 38 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 55 | 56 | 57 | 61 | 62 | 63 | 64 | 68 | 69 | 70 | 74 | 75 | 76 | 77 | 81 | 82 | 83 | 84 | 89 | 90 | 91 | 92 | 96 | 97 | 98 | 99 | 103 | 104 | 105 | 106 | 110 | 111 | 112 | 116 | 117 | 118 | 119 | 123 | 124 | 125 | 129 | 130 | 131 | 132 | 136 | 137 | 138 | 142 | 143 | 144 | 145 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | -------------------------------------------------------------------------------- /app/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/captain-miao/AndroidAgeraTutorial/6a4fa1082865bc5d4715dcd78adc9fad00d4f894/app/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/AppAgera.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | /** 7 | * @author YanLu 8 | * @since 16/5/16 9 | */ 10 | public class AppAgera extends Application{ 11 | 12 | private static AppAgera instance; 13 | 14 | public static AppAgera getInstance() { 15 | return instance; 16 | } 17 | 18 | public static Context getAppContext(){ 19 | return instance.getApplicationContext(); 20 | } 21 | 22 | @Override 23 | public void onCreate() { 24 | super.onCreate(); 25 | instance = this; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.Menu; 7 | import android.view.View; 8 | import android.widget.Button; 9 | 10 | import com.github.captain_miao.agera.tutorial.databinding.ActivityMainBinding; 11 | import com.github.captain_miao.agera.tutorial.helper.ActivityNavigation; 12 | import com.github.captain_miao.agera.tutorial.listener.OnViewClickListener; 13 | import com.github.captain_miao.agera.tutorial.model.ActInfo; 14 | 15 | import java.util.HashMap; 16 | import java.util.Map; 17 | 18 | public class MainActivity extends AppCompatActivity implements OnViewClickListener { 19 | private static final String TAG = "MainActivity"; 20 | 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | //ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater()); 25 | //setContentView(binding.getRoot()); 26 | ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); 27 | binding.setClickListener(this); 28 | binding.setMap(mActInfoMap); 29 | 30 | } 31 | 32 | @Override 33 | public boolean onCreateOptionsMenu(Menu menu) { 34 | getMenuInflater().inflate(R.menu.main_menu, menu); 35 | return true; 36 | } 37 | 38 | @Override 39 | public void onClick(View v) { 40 | if(v instanceof Button) { 41 | String name = ((Button) v).getText().toString(); 42 | ActivityNavigation.from(this).toUri(mActInfoMap.get(name).getUrl()); 43 | } 44 | } 45 | 46 | private Map mActInfoMap = new HashMap() {{ 47 | put("change_color", new ActInfo("change_color", "tutorial://agera/activity_a")); 48 | put("change_image", new ActInfo("change_image", "tutorial://agera/activity_b")); 49 | put("mutable_repository", new ActInfo("mutable_repository", "tutorial://agera/activity_c")); 50 | put("with_recycle_view", new ActInfo("with_recycle_view", "tutorial://agera/activity_d")); 51 | put("with_view_page", new ActInfo("with_view_page", "tutorial://agera/activity_e")); 52 | put("with_repository_adapter", new ActInfo("with_repository_adapter", "tutorial://agera/activity_f")); 53 | put("with_complex_recycle_view", new ActInfo("with_complex_recycle_view", "tutorial://agera/activity_g")); 54 | put("with_reservoir", new ActInfo("with_reservoir", "tutorial://agera/activity_h")); 55 | put("with_functions", new ActInfo("with_functions", "tutorial://agera/activity_i")); 56 | }}; 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/SimpleActivityA.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.os.Bundle; 5 | import android.support.annotation.NonNull; 6 | 7 | import com.github.captain_miao.agera.tutorial.base.BaseActivity; 8 | import com.github.captain_miao.agera.tutorial.databinding.ChangeTxtColorBinding; 9 | import com.github.captain_miao.agera.tutorial.helper.MockRandomData; 10 | import com.github.captain_miao.agera.tutorial.observable.OnClickObservable; 11 | import com.google.android.agera.Repositories; 12 | import com.google.android.agera.Repository; 13 | import com.google.android.agera.Result; 14 | import com.google.android.agera.Supplier; 15 | import com.google.android.agera.Updatable; 16 | 17 | public class SimpleActivityA extends BaseActivity implements Updatable { 18 | 19 | private ChangeTxtColorBinding mBinding; 20 | 21 | @Override 22 | public void init(Bundle savedInstanceState) { 23 | 24 | mBinding = DataBindingUtil.setContentView(this, R.layout.change_txt_color); 25 | setUpRepository(); 26 | mBinding.setObservable(mObservable); 27 | } 28 | 29 | 30 | //for agera 31 | private OnClickObservable mObservable; 32 | private Repository> mRepository; 33 | 34 | 35 | @Override 36 | protected void onResume() { 37 | super.onResume(); 38 | mRepository.addUpdatable(this); 39 | } 40 | 41 | @Override 42 | protected void onPause() { 43 | super.onPause(); 44 | mRepository.removeUpdatable(this); 45 | } 46 | 47 | private void setUpRepository() { 48 | mObservable = new OnClickObservable() { 49 | @Override 50 | public void onClick( ) { 51 | dispatchUpdate(); 52 | } 53 | }; 54 | Supplier> supplier = new Supplier>() { 55 | @NonNull 56 | @Override 57 | public Result get() { 58 | return Result.success(MockRandomData.getRandomColor()); 59 | } 60 | }; 61 | 62 | mRepository = Repositories.repositoryWithInitialValue(Result.absent()) 63 | .observe(mObservable) 64 | .onUpdatesPerLoop() 65 | .thenGetFrom(supplier) 66 | .compile(); 67 | } 68 | 69 | @Override 70 | public void update() { 71 | if(mRepository.get().succeeded()) { 72 | mBinding.setTxtColor(mRepository.get().get()); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/SimpleActivityB.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial; 2 | 3 | import android.graphics.Bitmap; 4 | import android.os.Bundle; 5 | import android.support.annotation.NonNull; 6 | import android.widget.Toast; 7 | 8 | import com.github.captain_miao.agera.tutorial.base.BaseActivity; 9 | import com.github.captain_miao.agera.tutorial.databinding.LoadImageByPicassoBinding; 10 | import com.github.captain_miao.agera.tutorial.helper.MockRandomData; 11 | import com.github.captain_miao.agera.tutorial.observable.OnClickObservable; 12 | import com.github.captain_miao.agera.tutorial.supplier.ImageSupplier; 13 | import com.google.android.agera.Function; 14 | import com.google.android.agera.Repositories; 15 | import com.google.android.agera.Repository; 16 | import com.google.android.agera.Result; 17 | import com.google.android.agera.Supplier; 18 | import com.google.android.agera.Updatable; 19 | 20 | import java.util.concurrent.ExecutorService; 21 | import java.util.concurrent.Executors; 22 | 23 | public class SimpleActivityB extends BaseActivity implements Updatable { 24 | private static final String TAG = "SimpleActivityB"; 25 | private LoadImageByPicassoBinding mBinding; 26 | 27 | private ExecutorService networkExecutor; 28 | @Override 29 | public void init(Bundle savedInstanceState) { 30 | mBinding = LoadImageByPicassoBinding.inflate(getLayoutInflater()); 31 | setContentView(mBinding.getRoot()); 32 | setUpRepository(); 33 | mBinding.setObservable(mObservable); 34 | } 35 | 36 | 37 | 38 | //for agera 39 | private OnClickObservable mObservable; 40 | private Repository> mRepository; 41 | 42 | 43 | @Override 44 | protected void onResume() { 45 | super.onResume(); 46 | mRepository.addUpdatable(this); 47 | } 48 | 49 | @Override 50 | protected void onPause() { 51 | super.onPause(); 52 | mRepository.removeUpdatable(this); 53 | } 54 | 55 | @Override 56 | protected void onDestroy() { 57 | super.onDestroy(); 58 | networkExecutor.shutdown(); 59 | } 60 | 61 | private void setUpRepository() { 62 | networkExecutor = Executors.newSingleThreadExecutor(); 63 | mObservable = new OnClickObservable() { 64 | @Override 65 | public void onClick( ) { 66 | dispatchUpdate(); 67 | } 68 | }; 69 | 70 | Supplier imageUriSupplier = new Supplier() { 71 | @NonNull 72 | @Override 73 | public String get() { 74 | return MockRandomData.getRandomImage(); 75 | } 76 | }; 77 | 78 | mRepository = Repositories.repositoryWithInitialValue(Result.absent()) 79 | .observe(mObservable) 80 | .onUpdatesPerLoop() 81 | .getFrom(imageUriSupplier) 82 | .goTo(networkExecutor) 83 | .thenTransform(new Function>() { 84 | @NonNull 85 | @Override 86 | public Result apply(@NonNull String input) { 87 | return new ImageSupplier(input).get(); 88 | } 89 | }) 90 | .compile(); 91 | } 92 | 93 | @Override 94 | public void update() { 95 | Result result = mRepository.get(); 96 | if(result.succeeded()) { 97 | mBinding.setBitmap(result.get()); 98 | } else { 99 | Toast.makeText(this, "load image fail", Toast.LENGTH_LONG).show(); 100 | } 101 | } 102 | 103 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/SimpleActivityC.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial; 2 | 3 | import android.os.Bundle; 4 | 5 | import com.github.captain_miao.agera.tutorial.base.BaseActivity; 6 | 7 | public class SimpleActivityC extends BaseActivity { 8 | 9 | @Override 10 | public void init(Bundle savedInstanceState) { 11 | setContentView(R.layout.fragment_simple); 12 | if (savedInstanceState == null) { 13 | addFragment(R.id.frg_container, SimpleFragmentC.class); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/SimpleActivityH.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.os.Bundle; 5 | import android.support.annotation.NonNull; 6 | 7 | import com.github.captain_miao.agera.tutorial.base.BaseActivity; 8 | import com.github.captain_miao.agera.tutorial.databinding.ActivityAgeraReservoirBinding; 9 | import com.google.android.agera.Repositories; 10 | import com.google.android.agera.Repository; 11 | import com.google.android.agera.Reservoir; 12 | import com.google.android.agera.Reservoirs; 13 | import com.google.android.agera.Result; 14 | import com.google.android.agera.Supplier; 15 | import com.google.android.agera.Updatable; 16 | 17 | import java.util.concurrent.ExecutorService; 18 | import java.util.concurrent.Executors; 19 | 20 | public class SimpleActivityH extends BaseActivity implements Updatable { 21 | 22 | private ActivityAgeraReservoirBinding mBinding; 23 | 24 | @Override 25 | public void init(Bundle savedInstanceState) { 26 | 27 | mBinding = DataBindingUtil.setContentView(this, R.layout.activity_agera_reservoir); 28 | setUpRepository(); 29 | mBinding.setObservable(mReservoir); 30 | } 31 | 32 | 33 | //for agera 34 | private ExecutorService mExecutor; 35 | private Reservoir mReservoir; 36 | private Repository> mRepository; 37 | 38 | private int mCount = 0; 39 | @Override 40 | protected void onResume() { 41 | super.onResume(); 42 | mRepository.addUpdatable(this); 43 | } 44 | 45 | @Override 46 | protected void onPause() { 47 | super.onPause(); 48 | mRepository.removeUpdatable(this); 49 | } 50 | 51 | 52 | @Override 53 | protected void onDestroy() { 54 | super.onDestroy(); 55 | mExecutor.shutdown(); 56 | } 57 | 58 | private void setUpRepository() { 59 | mExecutor = Executors.newSingleThreadExecutor(); 60 | mReservoir = Reservoirs.reservoir(); 61 | Supplier> supplier = new Supplier>() { 62 | @NonNull 63 | @Override 64 | public Result get() { 65 | try { 66 | Thread.sleep(1000); 67 | } catch (InterruptedException e) { 68 | e.printStackTrace(); 69 | } 70 | mReservoir.get();// consume receiver 71 | return Result.success(++mCount); 72 | } 73 | }; 74 | mRepository = Repositories.repositoryWithInitialValue(Result.absent()) 75 | .observe(mReservoir) 76 | .onUpdatesPerLoop() 77 | .goTo(mExecutor) 78 | .thenGetFrom(supplier) 79 | .compile(); 80 | } 81 | 82 | @Override 83 | public void update() { 84 | if(mRepository.get().succeeded()) { 85 | mBinding.setValue(mRepository.get().get()); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/SimpleActivityI.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.graphics.Bitmap; 5 | import android.os.Bundle; 6 | import android.support.annotation.NonNull; 7 | 8 | import com.github.captain_miao.agera.tutorial.base.BaseActivity; 9 | import com.github.captain_miao.agera.tutorial.databinding.ActivityAgeraFunctionsBinding; 10 | import com.github.captain_miao.agera.tutorial.helper.MockRandomData; 11 | import com.github.captain_miao.agera.tutorial.observable.OnClickObservable; 12 | import com.github.captain_miao.agera.tutorial.supplier.ImageSupplier; 13 | import com.google.android.agera.Function; 14 | import com.google.android.agera.Functions; 15 | import com.google.android.agera.Repositories; 16 | import com.google.android.agera.Repository; 17 | import com.google.android.agera.Result; 18 | import com.google.android.agera.Supplier; 19 | import com.google.android.agera.Updatable; 20 | 21 | import java.util.concurrent.ExecutorService; 22 | import java.util.concurrent.Executors; 23 | 24 | import static com.github.captain_miao.agera.tutorial.helper.MockRandomData.sImageSize; 25 | 26 | public class SimpleActivityI extends BaseActivity implements Updatable { 27 | 28 | private ActivityAgeraFunctionsBinding mBinding; 29 | 30 | @Override 31 | public void init(Bundle savedInstanceState) { 32 | 33 | mBinding = DataBindingUtil.setContentView(this, R.layout.activity_agera_functions); 34 | setUpRepository(); 35 | mBinding.setObservable(mObservable); 36 | } 37 | 38 | 39 | //for agera 40 | private ExecutorService mExecutor; 41 | private OnClickObservable mObservable; 42 | private Function> mFunction; 43 | private Repository> mRepository; 44 | 45 | private int mCount = 0; 46 | @Override 47 | protected void onResume() { 48 | super.onResume(); 49 | mRepository.addUpdatable(this); 50 | } 51 | 52 | @Override 53 | protected void onPause() { 54 | super.onPause(); 55 | mRepository.removeUpdatable(this); 56 | } 57 | 58 | 59 | @Override 60 | protected void onDestroy() { 61 | super.onDestroy(); 62 | mExecutor.shutdown(); 63 | } 64 | 65 | private void setUpRepository() { 66 | mExecutor = Executors.newSingleThreadExecutor(); 67 | mObservable = new OnClickObservable() { 68 | @Override 69 | public void onClick() { 70 | mCount++; 71 | dispatchUpdate(); 72 | } 73 | }; 74 | 75 | mFunction = Functions 76 | .functionFrom(String.class) 77 | .apply(new Function() { 78 | @NonNull 79 | @Override 80 | public String apply(@NonNull String input) { 81 | return input.replace(sImageSize[0], sImageSize[mCount % 3]); 82 | } 83 | }) 84 | .apply(new Function>() { 85 | @NonNull 86 | @Override 87 | public Result apply(@NonNull String input) { 88 | return new ImageSupplier(input).get(); 89 | } 90 | }) 91 | .thenApply(new Function, Result>() { 92 | @NonNull 93 | @Override 94 | public Result apply(@NonNull Result input) { 95 | return input; 96 | } 97 | }); 98 | 99 | 100 | mRepository = Repositories.repositoryWithInitialValue(Result.absent()) 101 | .observe(mObservable) 102 | .onUpdatesPerLoop() 103 | .goTo(mExecutor) 104 | .getFrom(new Supplier() { 105 | @NonNull 106 | @Override 107 | public String get() { 108 | return MockRandomData.sImages[5]; 109 | } 110 | }) 111 | .thenTransform(mFunction)// works on work thread(mExecutor) 112 | .compile(); 113 | } 114 | 115 | @Override 116 | public void update() { 117 | if(mRepository.get().succeeded()) { 118 | mBinding.setBitmap(mRepository.get().get()); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/SimpleFragmentC.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | import com.github.captain_miao.agera.tutorial.base.BaseFragment; 10 | import com.github.captain_miao.agera.tutorial.databinding.LoadImageByUrlBinding; 11 | import com.github.captain_miao.agera.tutorial.helper.MockRandomData; 12 | import com.github.captain_miao.agera.tutorial.observable.OnClickObservable; 13 | import com.google.android.agera.MutableRepository; 14 | import com.google.android.agera.Repositories; 15 | import com.google.android.agera.Result; 16 | import com.google.android.agera.Updatable; 17 | 18 | /** 19 | * @author YanLu 20 | * @since 16/4/26 21 | */ 22 | public class SimpleFragmentC extends BaseFragment implements Updatable { 23 | private LoadImageByUrlBinding mBinding; 24 | 25 | @Override 26 | public void onCreate(@Nullable Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setHasOptionsMenu(true); 29 | } 30 | 31 | @Nullable 32 | @Override 33 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 34 | mBinding = LoadImageByUrlBinding.inflate(inflater, container, false); 35 | return mBinding.getRoot(); 36 | } 37 | 38 | @Override 39 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 40 | super.onActivityCreated(savedInstanceState); 41 | setUpRepository(); 42 | mBinding.setObservable(mObservable); 43 | } 44 | 45 | 46 | //for agera 47 | private OnClickObservable mObservable; 48 | private MutableRepository> mRepository; 49 | 50 | 51 | @Override 52 | public void onResume() { 53 | super.onResume(); 54 | mRepository.addUpdatable(this); 55 | } 56 | 57 | @Override 58 | public void onPause() { 59 | super.onPause(); 60 | mRepository.removeUpdatable(this); 61 | } 62 | 63 | @Override 64 | public void onDestroy() { 65 | super.onDestroy(); 66 | } 67 | 68 | private void setUpRepository() { 69 | mObservable = new OnClickObservable() { 70 | @Override 71 | public void onClick( ) { 72 | mRepository.accept(Result.success(MockRandomData.getRandomImage())); 73 | } 74 | }; 75 | 76 | mRepository = Repositories.mutableRepository(Result.success(MockRandomData.getRandomImage())); 77 | 78 | //initialization 79 | //mRepository.accept(Result.success(MockRandomData.getRandomImage())); 80 | } 81 | 82 | @Override 83 | public void update() { 84 | if(mRepository.get().succeeded()) { 85 | String result = mRepository.get().get(); 86 | mBinding.setImageUrl(result); 87 | } 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/base/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.base; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.FragmentTransaction; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.MenuItem; 7 | 8 | 9 | 10 | public abstract class BaseActivity extends AppCompatActivity { 11 | private static String TAG = BaseActivity.class.getSimpleName(); 12 | 13 | public abstract void init(Bundle savedInstanceState); 14 | 15 | @Override 16 | final protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | init(savedInstanceState); 19 | 20 | 21 | if(getSupportActionBar() != null) { 22 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 23 | } 24 | 25 | } 26 | 27 | 28 | 29 | /** 30 | * replace fragment 31 | */ 32 | protected void initFragment(int containerViewId, BaseFragment fragment, String tag) { 33 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 34 | fragmentTransaction.replace(containerViewId, fragment, tag).commitAllowingStateLoss(); 35 | } 36 | 37 | /** 38 | * add fragment 39 | */ 40 | protected void addFragment(int containerViewId, BaseFragment fragment) { 41 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 42 | fragmentTransaction.add(containerViewId, fragment).commitAllowingStateLoss(); 43 | } 44 | 45 | protected void addFragment(int containerViewId, Class fragmentClazz) { 46 | F frg = createFragment(fragmentClazz, getIntent().getExtras()); 47 | FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 48 | fragmentTransaction.add(containerViewId, frg).commitAllowingStateLoss(); 49 | } 50 | 51 | /** 52 | * create fragment instance 53 | * @param fragmentClazz 54 | * @param args 55 | * @param 56 | * @return 57 | */ 58 | public static T createFragment(Class fragmentClazz, Bundle args) { 59 | T fragment = null; 60 | try { 61 | fragment = fragmentClazz.newInstance(); 62 | fragment.setArguments(args); 63 | } catch (java.lang.InstantiationException e) { 64 | e.printStackTrace(); 65 | } catch (IllegalAccessException e) { 66 | e.printStackTrace(); 67 | } 68 | return fragment; 69 | } 70 | 71 | @Override 72 | public boolean onOptionsItemSelected(MenuItem item) { 73 | switch (item.getItemId()) { 74 | case android.R.id.home: 75 | onBackPressed(); 76 | return true; 77 | } 78 | return super.onOptionsItemSelected(item); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/base/BaseFragment.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.base; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.Fragment; 5 | import android.view.Menu; 6 | import android.view.MenuInflater; 7 | 8 | 9 | public abstract class BaseFragment extends Fragment { 10 | public static final String TAG = "BaseFragment"; 11 | 12 | 13 | 14 | /** 15 | * 在这里更改Actionbar 16 | * @param menu 17 | * @param inflater 18 | */ 19 | @Override 20 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 21 | super.onCreateOptionsMenu(menu, inflater); 22 | } 23 | 24 | /** 25 | * create fragment instance 26 | * @param fragmentClazz 27 | * @param args 28 | * @param 29 | * @return 30 | */ 31 | public static T newInstance(Class fragmentClazz, Bundle args) { 32 | T fragment = null; 33 | try { 34 | fragment = fragmentClazz.newInstance(); 35 | fragment.setArguments(args); 36 | } catch (java.lang.InstantiationException e) { 37 | e.printStackTrace(); 38 | } catch (IllegalAccessException e) { 39 | e.printStackTrace(); 40 | } 41 | return fragment; 42 | } 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/base/BasePagerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.base; 2 | 3 | import android.content.Context; 4 | import android.support.v4.view.PagerAdapter; 5 | import android.view.LayoutInflater; 6 | import android.view.ViewGroup; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | /** 12 | * @author YanLu 13 | * @since 16/5/13 14 | */ 15 | public abstract class BasePagerAdapter extends PagerAdapter { 16 | protected ArrayList mData = new ArrayList(); 17 | protected LayoutInflater mInflater; 18 | protected Context mContext; 19 | 20 | public BasePagerAdapter(Context c) { 21 | mContext = c; 22 | mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 23 | } 24 | 25 | @Override 26 | public abstract Object instantiateItem(ViewGroup container, int position); 27 | 28 | @Override 29 | public abstract void destroyItem(ViewGroup container, int position, Object object); 30 | 31 | @Override 32 | public int getCount() { 33 | return mData != null ? mData.size() : 0; 34 | } 35 | 36 | public void addItem(final T item) { 37 | mData.add(item); 38 | notifyDataSetChanged(); 39 | } 40 | public void addAll(final List items) { 41 | if (items != null) { 42 | mData.addAll(items); 43 | } 44 | notifyDataSetChanged(); 45 | } 46 | public void setData(final List items) { 47 | mData.clear(); 48 | if (items != null) { 49 | mData.addAll(items); 50 | } 51 | notifyDataSetChanged(); 52 | } 53 | 54 | public void addItem(int idx, final T item) { 55 | mData.add(idx, item); 56 | notifyDataSetChanged(); 57 | } 58 | 59 | public void clearItems() { 60 | mData.clear(); 61 | notifyDataSetChanged(); 62 | } 63 | 64 | public T getItem(int position) { 65 | return mData.get(position); 66 | } 67 | 68 | public ArrayList getData() { 69 | return mData; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/helper/ActivityNavigation.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.helper; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.net.Uri; 6 | import android.os.Bundle; 7 | import android.text.TextUtils; 8 | import android.util.Log; 9 | 10 | public class ActivityNavigation { 11 | 12 | public static ActivityNavigation from(final Context context) { 13 | return new ActivityNavigation(context); 14 | } 15 | 16 | 17 | private ActivityNavigation(final Context context) { 18 | mContext = context; 19 | mIntent = new Intent(Intent.ACTION_VIEW); 20 | } 21 | 22 | public ActivityNavigation withExtras(final Bundle extras) { 23 | if(extras == null) { 24 | return this; 25 | } 26 | 27 | mIntent.putExtras(extras); 28 | return this; 29 | } 30 | 31 | public ActivityNavigation withFlags(final int flags) { 32 | mIntent.addFlags(flags); 33 | return this; 34 | } 35 | 36 | public boolean toUri(final String uri) { 37 | 38 | if (TextUtils.isEmpty(uri)) 39 | return false; 40 | 41 | return toUri(Uri.parse(uri)); 42 | } 43 | 44 | 45 | public boolean toUri(final Uri uri) { 46 | final Intent intent = mIntent.setData(uri); 47 | Log.d(TAG, uri.toString()); 48 | try { 49 | mContext.startActivity(intent); 50 | return true; 51 | } catch (Exception e) { 52 | e.printStackTrace(); 53 | return false; 54 | } 55 | } 56 | 57 | 58 | 59 | private final Context mContext; 60 | private final Intent mIntent; 61 | 62 | private static final String TAG = "ActivityNavigation"; 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/helper/MockRandomData.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.helper; 2 | 3 | import android.databinding.ObservableBoolean; 4 | import android.graphics.Color; 5 | 6 | import com.github.captain_miao.agera.tutorial.model.VehicleInfo; 7 | 8 | import java.security.SecureRandom; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | /** 13 | * @author YanLu 14 | * @since 16/4/26 15 | */ 16 | public class MockRandomData { 17 | 18 | public static String getRandomImage() { 19 | SecureRandom secureRandom = new SecureRandom(); 20 | return sImages[secureRandom.nextInt(9)]; 21 | } 22 | 23 | 24 | public static String getRandomErrorImage() { 25 | SecureRandom secureRandom = new SecureRandom(); 26 | return sErrorImages[secureRandom.nextInt(9)]; 27 | } 28 | 29 | 30 | public static int getRandomColor() { 31 | SecureRandom secureRandom = new SecureRandom(); 32 | return Color.HSVToColor(150, new float[]{ 33 | secureRandom.nextInt(359), 1, 1 34 | }); 35 | } 36 | 37 | public static List getVehicleInfos(){ 38 | return sVehicleInfos; 39 | } 40 | 41 | 42 | private static List sVehicleInfos = new ArrayList(){{ 43 | add(new VehicleInfo(new ObservableBoolean(true), "http://www.carlogos.org/uploads/car-logos/Bugatti-logo-1.jpg", 44 | "Bugatti", "Italian-born Ettore Bugatti showed great interest in engineering and automotive industry since teenage years, worked for several manufacturers and by 1909 had enough experience and enthusiasm to found his own car-manufacturing company Automobiles E.Bugatti.")); 45 | add(new VehicleInfo(new ObservableBoolean(false), "http://www.carlogos.org/uploads/car-logos/Hennessey-logo-1.jpg", 46 | "Hennessey", "Hennessey Performance Engineering is an American tuning house specializing in modifying sports and super cars from several brands like Ferrari, Porsche, McLaren, Chevrolet, Dodge, Audi, Mercedes-Benz, Toyota, Nissan, Mustang, Cadillac, Lotus, Jeep, Ford, BMW, Bentley, Chrysler, GMC, Lincoln and Lexus.")); 47 | add(new VehicleInfo(new ObservableBoolean(false), "http://www.carlogos.org/uploads/car-logos/Koenigsegg-logo-1.jpg", 48 | "Koenigsegg", "The story goes that Von Koenigsegg got inspired by the Norwegian animated movie ‘Pinchcliffe Grand Prix’ when he was just a child and ever since he dreamt of building his own supercar – just like the main character in the movie, which is a bicycle repairman, one day builds his own racing car.")); 49 | add(new VehicleInfo(new ObservableBoolean(false), "http://www.carlogos.org/uploads/car-logos/SSC-North-America-logo-1.jpg", 50 | "SSC Ultimate Aero", "SSC is an American supercar company founded in 1999 by automotive enthusiast, Jerod Shelby. SSC's headquarters in Tri-Cities, Washington, is also the hometown of Mr. Shelby and home to his dream. Fueled by his passion for racing and automotive culture SSC's success is a true representation of the American Dream.")); 51 | add(new VehicleInfo(new ObservableBoolean(false), "http://www.carlogos.org/uploads/car-logos/McLaren-logo-1.jpg", 52 | "McLaren", "McLaren, also known as McLaren Automotive, it is one of the leading British high-performance automotive manufacturers and has made incredible contributions to the industry. Unlike other British car manufacturers, McLaren has formed several different companies and has changed how we perceive luxury cars in this day and age. Continue reading the McLaren history to see how this brand came into shape and where it stands today.")); 53 | add(new VehicleInfo(new ObservableBoolean(false), "http://www.carlogos.org/uploads/car-logos/Zenvo-logo-1.jpg", 54 | "Zenvo", "The Zenvo ST1 is a high performance supercar manufactured by Danish company Zenvo. It is the company's first model, and is manufactured almost entirely by the hands of a small team of workers, with the exception of a CNC router.")); 55 | add(new VehicleInfo(new ObservableBoolean(false), "http://www.carlogos.org/uploads/car-logos/Gumpert-logo-1.jpg", 56 | "Gumpert Apollo", "Gumpert Sportwagenmanufaktur GmbH was founded by former Audi Director Roland Gumpert in 2004. Its car manufacturing company is based in Altenburg, Germany.")); 57 | add(new VehicleInfo(new ObservableBoolean(false), "http://www.carlogos.org/uploads/car-logos/Aston-Martin-logo-3.jpg", 58 | "Aston Martin", "Founded in 1913 by Robert Bamford and Lionel Martin as 'Bamford & Martin Ltd', the company has developed into an iconic brand synonymous with luxury and elegance. 1914 saw the birth of the name ‘Aston Martin’ following one of Lionel Martin's successful runs at the Aston Hill Climb in Buckinghamshire, England. Within a year the first Aston Martin had been built and registered with the name, and an icon of the automotive world was born.")); 59 | add(new VehicleInfo(new ObservableBoolean(false), "http://www.carlogos.org/uploads/car-logos/Lamborghini-logo-1.jpg", 60 | "Lamborghini Aventador", "The Lamborghini logo symbolizes the founder’s zodiac character – the Taurus or a bull. Ferruccio’s love of bullfights was depicted in the logo and Lamborghini cars get their styles from famous bulls. The golden bull ready for bullfights is depicted on the black shield with the golden title \"Lamborghini\" above. The bull represents Lamborghini sports cars’ power.")); 61 | add(new VehicleInfo(new ObservableBoolean(false), "http://www.carlogos.org/uploads/car-logos/Ferrari-logo-1.jpg", 62 | "LaFerrari", "The horse was initially the sign of the famous Count Francesco Baracca. He was a legendary professional of the air force of Italy at the World War I, who depicted it on the wing of his planes. Francesco Baracca died very yearly on June 19 in 1918, shot down later on 34 victorious fightings and many team triumphs.")); 63 | }}; 64 | 65 | // large bmiddle small 66 | private static String[] sErrorImages = new String[]{ 67 | "http://ww1.sinaimg.cn/large/7a8aed7bjw1f2sm0ns82hj20f00l8tb9.jpg_error", 68 | "http://ww4.sinaimg.cn/large/7a8aed7bjw1f2tpr3im0mj20f00l6q4o.jpg", 69 | "http://ww4.sinaimg.cn/large/610dc034jw1f2uyg3nvq7j20gy0p6myx.jpg_error", 70 | "http://ww2.sinaimg.cn/large/7a8aed7bjw1f2w0qujoecj20f00kzjtt.jpg", 71 | "http://ww3.sinaimg.cn/large/7a8aed7bjw1f2x7vxkj0uj20d10mi42r.jpg_error", 72 | "http://ww1.sinaimg.cn/large/7a8aed7bjw1f2zwrqkmwoj20f00lg0v7.jpg", 73 | "http://ww2.sinaimg.cn/large/7a8aed7bjw1f30sgi3jf0j20iz0sg40a.jpg_error", 74 | "http://ww4.sinaimg.cn/large/7a8aed7bjw1f32d0cumhkj20ey0mitbx.jpg", 75 | "http://ww2.sinaimg.cn/large/610dc034gw1f35cxyferej20dw0i2789.jpg_error", 76 | "http://ww2.sinaimg.cn/large/7a8aed7bjw1f340c8jrk4j20j60srgpf.jpg" 77 | }; 78 | 79 | public static String[] sImageSize = new String[]{ 80 | "large", 81 | "bmiddle", 82 | "small" 83 | }; 84 | 85 | public static String[] sImages = new String[]{ 86 | "http://ww1.sinaimg.cn/large/7a8aed7bjw1f2sm0ns82hj20f00l8tb9.jpg", 87 | "http://ww4.sinaimg.cn/large/7a8aed7bjw1f2tpr3im0mj20f00l6q4o.jpg", 88 | "http://ww4.sinaimg.cn/large/610dc034jw1f2uyg3nvq7j20gy0p6myx.jpg", 89 | "http://ww2.sinaimg.cn/large/7a8aed7bjw1f2w0qujoecj20f00kzjtt.jpg", 90 | "http://ww3.sinaimg.cn/large/7a8aed7bjw1f2x7vxkj0uj20d10mi42r.jpg", 91 | "http://ww1.sinaimg.cn/large/7a8aed7bjw1f2zwrqkmwoj20f00lg0v7.jpg", 92 | "http://ww2.sinaimg.cn/large/7a8aed7bjw1f30sgi3jf0j20iz0sg40a.jpg", 93 | "http://ww4.sinaimg.cn/large/7a8aed7bjw1f32d0cumhkj20ey0mitbx.jpg", 94 | "http://ww2.sinaimg.cn/large/610dc034gw1f35cxyferej20dw0i2789.jpg", 95 | "http://ww2.sinaimg.cn/large/7a8aed7bjw1f340c8jrk4j20j60srgpf.jpg" 96 | }; 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/helper/PicassoBinding.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.helper; 2 | 3 | import android.databinding.BindingAdapter; 4 | import android.graphics.Bitmap; 5 | import android.graphics.drawable.Drawable; 6 | import android.widget.ImageView; 7 | 8 | import com.github.captain_miao.agera.tutorial.R; 9 | import com.squareup.picasso.MemoryPolicy; 10 | import com.squareup.picasso.Picasso; 11 | 12 | /** 13 | * @author YanLu 14 | * @since 16/4/25 15 | */ 16 | public class PicassoBinding { 17 | private static final String TAG = "PicassoBinding"; 18 | 19 | @BindingAdapter({"imageUrl"}) 20 | public static void imageLoader(ImageView imageView, String url) { 21 | // Picasso.Builder builder = new Picasso.Builder(imageView.getContext()); 22 | // builder.listener(new Picasso.Listener() { 23 | // @Override 24 | // public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) { 25 | // exception.printStackTrace(); 26 | // Log.e("Picasso Error", uri.toString()); 27 | // } 28 | // }); 29 | // builder.build().load(url).into(imageView); 30 | 31 | 32 | Picasso.with(imageView.getContext()).load(url).into(imageView); 33 | } 34 | @BindingAdapter({"imageUrl", "error"}) 35 | public static void imageLoader(ImageView imageView, String url, Drawable error) { 36 | // Picasso.Builder builder = new Picasso.Builder(imageView.getContext()); 37 | // builder.listener(new Picasso.Listener() { 38 | // @Override 39 | // public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) { 40 | // exception.printStackTrace(); 41 | // Log.e("Picasso Error", uri.toString()); 42 | // } 43 | // }); 44 | // builder.build() 45 | // .load(url) 46 | // .error(error) 47 | // .into(imageView); 48 | 49 | 50 | Picasso.with(imageView.getContext()).load(url).error(error).into(imageView); 51 | } 52 | 53 | @BindingAdapter({"compressImageUrl"}) 54 | public static void loadImageCompress(ImageView imageView, String url) { 55 | //large -> b middle 56 | // Picasso.Builder builder = new Picasso.Builder(imageView.getContext().getApplicationContext()); 57 | // builder.listener(new Picasso.Listener() { 58 | // @Override 59 | // public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) { 60 | // exception.printStackTrace(); 61 | // Log.e("Picasso Error", uri.toString()); 62 | // } 63 | // }); 64 | //recycle bitmap 65 | // Drawable drawable = imageView.getDrawable(); 66 | // if (drawable instanceof BitmapDrawable) { 67 | // imageView.setImageDrawable(null); 68 | // Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); 69 | // Log.d(TAG, "recycle bitmap, w:" + bitmap.getWidth() + ", h:" + bitmap.getHeight()); 70 | // bitmap.recycle(); 71 | // } 72 | Picasso.with(imageView.getContext().getApplicationContext()) 73 | .load(url) 74 | .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) 75 | .placeholder(R.drawable.ic_image_load_place_holder) 76 | .config(Bitmap.Config.RGB_565) 77 | .tag(PicassoOnScrollListener.TAG) 78 | .into(imageView); 79 | } 80 | 81 | @BindingAdapter({"android:src"}) 82 | public static void setImageViewResource(ImageView imageView, int resource) { 83 | imageView.setImageResource(resource); 84 | } 85 | 86 | @BindingAdapter("{imageBitmap}") 87 | public static void setImageViewBitmap(ImageView iv, Bitmap bitmap) { 88 | iv.setImageBitmap(bitmap); 89 | } 90 | 91 | // @BindingAdapter({"imageUrl", "error", "android:clickable"}) 92 | // public static void imageLoader(ImageView imageView, String url, Drawable error, boolean clickable) { 93 | // Picasso.Builder builder = new Picasso.Builder(imageView.getContext()); 94 | // builder.listener(new Picasso.Listener() { 95 | // @Override 96 | // public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) { 97 | // exception.printStackTrace(); 98 | // Log.e("Picasso Error", uri.toString()); 99 | // } 100 | // }); 101 | // builder.build() 102 | // .load(url) 103 | // .error(error) 104 | // .into(imageView); 105 | // Log.d(TAG, "android:clickable = " + clickable); 106 | // 107 | //// Picasso.with(imageView.getContext()).load(url).error(error).into(imageView); 108 | // } 109 | } 110 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/helper/PicassoOnScrollListener.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.helper; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | 6 | import com.squareup.picasso.Picasso; 7 | 8 | /** 9 | * @author YanLu 10 | * @since 16/5/23 11 | */ 12 | public class PicassoOnScrollListener extends RecyclerView.OnScrollListener { 13 | public static final Object TAG = new Object(); 14 | private static final int SETTLING_DELAY = 500; 15 | private int SCROLL_THRESHOLD = 500; 16 | 17 | private static Picasso sPicasso = null; 18 | private Runnable mSettlingResumeRunnable = null; 19 | 20 | public PicassoOnScrollListener(Context context) { 21 | if(sPicasso == null) { 22 | sPicasso = Picasso.with(context.getApplicationContext()); 23 | SCROLL_THRESHOLD = context.getResources().getDisplayMetrics().heightPixels; 24 | } 25 | } 26 | 27 | @Override 28 | public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) { 29 | if(scrollState == RecyclerView.SCROLL_STATE_IDLE) { 30 | recyclerView.removeCallbacks(mSettlingResumeRunnable); 31 | sPicasso.resumeTag(TAG); 32 | 33 | } else if(scrollState == RecyclerView.SCROLL_STATE_SETTLING) { 34 | mSettlingResumeRunnable = new Runnable() { 35 | @Override 36 | public void run() { 37 | sPicasso.resumeTag(TAG); 38 | } 39 | }; 40 | 41 | recyclerView.postDelayed(mSettlingResumeRunnable, SETTLING_DELAY); 42 | 43 | } 44 | //else { 45 | // sPicasso.pauseTag(TAG); 46 | //} 47 | } 48 | 49 | 50 | 51 | @Override 52 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 53 | super.onScrolled(recyclerView, dx, dy); 54 | if (Math.abs(dy) > SCROLL_THRESHOLD) { 55 | sPicasso.pauseTag(TAG); 56 | } 57 | // else { 58 | // sPicasso.resumeTag(TAG); 59 | // } 60 | } 61 | 62 | public static Picasso getPicasso() { 63 | return sPicasso; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/helper/RecyclingBitmapDrawable.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.helper; 2 | 3 | import android.content.res.Resources; 4 | import android.graphics.Bitmap; 5 | import android.graphics.drawable.BitmapDrawable; 6 | import android.util.Log; 7 | 8 | import com.github.captain_miao.agera.tutorial.BuildConfig; 9 | 10 | /** 11 | * @author YanLu 12 | * @since 16/5/22 13 | */ 14 | public class RecyclingBitmapDrawable extends BitmapDrawable { 15 | 16 | static final String TAG = "CountingBitmapDrawable"; 17 | 18 | private int mCacheRefCount = 0; 19 | private int mDisplayRefCount = 0; 20 | 21 | private boolean mHasBeenDisplayed; 22 | 23 | public RecyclingBitmapDrawable(Resources res, Bitmap bitmap) { 24 | super(res, bitmap); 25 | } 26 | 27 | /** 28 | * Notify the drawable that the displayed state has changed. Internally a 29 | * count is kept so that the drawable knows when it is no longer being 30 | * displayed. 31 | * 32 | * @param isDisplayed - Whether the drawable is being displayed or not 33 | */ 34 | public void setIsDisplayed(boolean isDisplayed) { 35 | synchronized (this) { 36 | if (isDisplayed) { 37 | mDisplayRefCount++; 38 | mHasBeenDisplayed = true; 39 | } else { 40 | mDisplayRefCount--; 41 | } 42 | } 43 | 44 | // Check to see if recycle() can be called 45 | checkState(); 46 | } 47 | 48 | /** 49 | * Notify the drawable that the cache state has changed. Internally a count 50 | * is kept so that the drawable knows when it is no longer being cached. 51 | * 52 | * @param isCached - Whether the drawable is being cached or not 53 | */ 54 | public void setIsCached(boolean isCached) { 55 | synchronized (this) { 56 | if (isCached) { 57 | mCacheRefCount++; 58 | } else { 59 | mCacheRefCount--; 60 | } 61 | } 62 | 63 | // Check to see if recycle() can be called 64 | checkState(); 65 | } 66 | 67 | private synchronized void checkState() { 68 | // If the drawable cache and display ref counts = 0, and this drawable 69 | // has been displayed, then recycle 70 | if (mCacheRefCount <= 0 && mDisplayRefCount <= 0 && mHasBeenDisplayed 71 | && hasValidBitmap()) { 72 | if (BuildConfig.DEBUG) { 73 | Log.d(TAG, "No longer being used or cached so recycling. " 74 | + toString()); 75 | } 76 | 77 | getBitmap().recycle(); 78 | } 79 | } 80 | 81 | private synchronized boolean hasValidBitmap() { 82 | Bitmap bitmap = getBitmap(); 83 | return bitmap != null && !bitmap.isRecycled(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/helper/RecyclingImageView.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.helper; 2 | 3 | import android.content.Context; 4 | import android.graphics.drawable.Drawable; 5 | import android.graphics.drawable.LayerDrawable; 6 | import android.util.AttributeSet; 7 | import android.widget.ImageView; 8 | 9 | /** 10 | * @author YanLu 11 | * @since 16/5/22 12 | */ 13 | public class RecyclingImageView extends ImageView { 14 | 15 | public RecyclingImageView(Context context) { 16 | super(context); 17 | } 18 | 19 | public RecyclingImageView(Context context, AttributeSet attrs) { 20 | super(context, attrs); 21 | } 22 | 23 | /** 24 | * @see android.widget.ImageView#onDetachedFromWindow() 25 | */ 26 | @Override 27 | protected void onDetachedFromWindow() { 28 | // This has been detached from Window, so clear the drawable 29 | setImageDrawable(null); 30 | 31 | super.onDetachedFromWindow(); 32 | } 33 | 34 | /** 35 | * @see android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable) 36 | */ 37 | @Override 38 | public void setImageDrawable(Drawable drawable) { 39 | // Keep hold of previous Drawable 40 | final Drawable previousDrawable = getDrawable(); 41 | 42 | // Call super to set new Drawable 43 | super.setImageDrawable(drawable); 44 | 45 | // Notify new Drawable that it is being displayed 46 | notifyDrawable(drawable, true); 47 | 48 | // Notify old Drawable so it is no longer being displayed 49 | notifyDrawable(previousDrawable, false); 50 | } 51 | 52 | /** 53 | * Notifies the drawable that it's displayed state has changed. 54 | * 55 | * @param drawable 56 | * @param isDisplayed 57 | */ 58 | private static void notifyDrawable(Drawable drawable, final boolean isDisplayed) { 59 | if (drawable instanceof RecyclingBitmapDrawable) { 60 | // The drawable is a CountingBitmapDrawable, so notify it 61 | ((RecyclingBitmapDrawable) drawable).setIsDisplayed(isDisplayed); 62 | } else if (drawable instanceof LayerDrawable) { 63 | // The drawable is a LayerDrawable, so recurse on each layer 64 | LayerDrawable layerDrawable = (LayerDrawable) drawable; 65 | for (int i = 0, z = layerDrawable.getNumberOfLayers(); i < z; i++) { 66 | notifyDrawable(layerDrawable.getDrawable(i), isDisplayed); 67 | } 68 | } 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/helper/UiThreadExecutor.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.helper; 2 | 3 | import android.os.Handler; 4 | import android.os.Looper; 5 | 6 | import java.util.concurrent.Executor; 7 | 8 | /** 9 | * @author YanLu 10 | * @since 16/5/25 11 | */ 12 | public class UiThreadExecutor implements Executor { 13 | private final Handler mHandler = new Handler(Looper.getMainLooper()); 14 | @Override 15 | public void execute(Runnable command) { 16 | mHandler.post(command); 17 | } 18 | 19 | public void shutdown(){ 20 | // TODO: 16/5/25 21 | } 22 | 23 | // how to release it? 24 | public static Executor newUiThreadExecutor() { 25 | return new UiThreadExecutor(); 26 | } 27 | 28 | private UiThreadExecutor() { 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/helper/ViewVisibleBindingAdapter.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.helper; 2 | 3 | import android.databinding.BindingAdapter; 4 | import android.view.View; 5 | 6 | /** 7 | * @author YanLu 8 | * @since 16/4/27 9 | */ 10 | public class ViewVisibleBindingAdapter { 11 | 12 | @BindingAdapter("isGone") 13 | public static void setIsGone(View view, boolean hide){ 14 | view.setVisibility(hide ? View.GONE : View.VISIBLE); 15 | } 16 | 17 | @BindingAdapter("isInvisible") 18 | public static void setIsInvisible(View view, boolean hide){ 19 | view.setVisibility(hide ? View.INVISIBLE : View.VISIBLE); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/http/DemoApiService.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.http; 2 | 3 | import com.github.captain_miao.agera.tutorial.model.ApiResult; 4 | import com.github.captain_miao.agera.tutorial.model.GirlInfo; 5 | 6 | import retrofit2.Call; 7 | import retrofit2.http.GET; 8 | import retrofit2.http.Path; 9 | 10 | /** 11 | * @author YanLu 12 | * @since 16/5/20 13 | */ 14 | public interface DemoApiService { 15 | 16 | @GET("10/{pagination}") 17 | Call> getGirls(@Path("pagination") int pagination); 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/http/RetrofitServiceFactory.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.http; 2 | 3 | 4 | import retrofit2.Retrofit; 5 | import retrofit2.converter.gson.GsonConverterFactory; 6 | 7 | public class RetrofitServiceFactory { 8 | public static String BASE_URL = "https://api.github.com"; 9 | /** 10 | * Creates a retrofit service from an arbitrary class (clazz) 11 | * @param clazz Java interface of the retrofit service 12 | * @param baseUrl REST baseUrl url 13 | * @return retrofit service with defined endpoint 14 | */ 15 | public static T createService(final Class clazz, final String baseUrl) { 16 | 17 | Retrofit retrofit = new Retrofit.Builder() 18 | .baseUrl(baseUrl) 19 | .addConverterFactory(GsonConverterFactory.create()) 20 | .build(); 21 | 22 | return retrofit.create(clazz); 23 | } 24 | 25 | public static T createService(final Class clazz) { 26 | 27 | Retrofit retrofit = new Retrofit.Builder() 28 | .baseUrl(BASE_URL) 29 | .build(); 30 | 31 | return retrofit.create(clazz); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/listener/OnViewClickListener.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.listener; 2 | 3 | import android.view.View; 4 | 5 | /** 6 | * @author YanLu 7 | * @since 16/4/24 8 | */ 9 | public interface OnViewClickListener extends View.OnClickListener { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/listener/SimpleObservable.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.listener; 2 | 3 | import android.util.Log; 4 | 5 | import com.google.android.agera.BaseObservable; 6 | 7 | /** 8 | * @author YanLu 9 | * @since 16/4/24 10 | */ 11 | public class SimpleObservable extends BaseObservable{ 12 | private static final String TAG = "SimpleObservable"; 13 | 14 | public void update() { 15 | dispatchUpdate(); 16 | } 17 | 18 | @Override 19 | protected void observableActivated() { 20 | Log.d(TAG, "observableActivated"); 21 | } 22 | 23 | @Override 24 | protected void observableDeactivated() { 25 | Log.d(TAG, "observableDeactivated"); 26 | } 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/model/ActInfo.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.model; 2 | 3 | /** 4 | * @author YanLu 5 | * @since 16/4/24 6 | */ 7 | public class ActInfo extends BaseModel{ 8 | private static final long serialVersionUID = 1L; 9 | 10 | private String name; 11 | private String url; 12 | 13 | public ActInfo(String name, String url) { 14 | this.name = name; 15 | this.url = url; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public String getUrl() { 27 | return url; 28 | } 29 | 30 | public void setUrl(String url) { 31 | this.url = url; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/model/ApiResult.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.model; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * @author YanLu 7 | * @since 16/5/20 8 | */ 9 | public class ApiResult extends BaseModel { 10 | public boolean error; 11 | public List results; 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/model/BaseModel.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.model; 2 | 3 | 4 | import java.io.Serializable; 5 | 6 | /** 7 | * @author YanLu 8 | * @since 16/4/24 9 | */ 10 | public class BaseModel implements Serializable{ 11 | private static final long serialVersionUID = 2L; 12 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/model/BaseObservableVehicleInfo.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.model; 2 | 3 | import android.databinding.BaseObservable; 4 | import android.databinding.Bindable; 5 | 6 | import com.github.captain_miao.agera.tutorial.BR; 7 | 8 | /** 9 | * @author YanLu 10 | * @since 16/4/27 11 | */ 12 | public class BaseObservableVehicleInfo extends BaseObservable { 13 | 14 | private boolean isSelected; 15 | private String logoUrl; 16 | private String brand; 17 | private String description; 18 | 19 | public BaseObservableVehicleInfo(boolean isSelected, String logoUrl, String brand, String description) { 20 | this.isSelected = isSelected; 21 | this.logoUrl = logoUrl; 22 | this.brand = brand; 23 | this.description = description; 24 | } 25 | 26 | public BaseObservableVehicleInfo(VehicleInfo vehicleInfo) { 27 | this.isSelected = vehicleInfo.isSelected.get(); 28 | this.logoUrl = vehicleInfo.logoUrl; 29 | this.brand = vehicleInfo.brand; 30 | this.description = vehicleInfo.description; 31 | } 32 | 33 | @Bindable 34 | public boolean getIsSelected() { 35 | return isSelected; 36 | } 37 | 38 | public void setIsSelected(boolean isSelected) { 39 | this.isSelected = isSelected; 40 | notifyPropertyChanged(BR.isSelected); 41 | } 42 | 43 | public String getLogoUrl() { 44 | return logoUrl; 45 | } 46 | 47 | public void setLogoUrl(String logoUrl) { 48 | this.logoUrl = logoUrl; 49 | } 50 | 51 | public String getBrand() { 52 | return brand; 53 | } 54 | 55 | public void setBrand(String brand) { 56 | this.brand = brand; 57 | } 58 | 59 | public String getDescription() { 60 | return description; 61 | } 62 | 63 | public void setDescription(String description) { 64 | this.description = description; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/model/GirlInfo.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.model; 2 | 3 | /** 4 | * @author YanLu 5 | * @since 16/5/20 6 | */ 7 | public class GirlInfo extends BaseModel { 8 | public String url; 9 | public String createdAt; 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/model/ObservableVehicleInfo.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.model; 2 | 3 | import android.databinding.Bindable; 4 | import android.databinding.Observable; 5 | import android.databinding.PropertyChangeRegistry; 6 | 7 | import com.github.captain_miao.agera.tutorial.BR; 8 | 9 | /** 10 | * @author YanLu 11 | * @since 16/4/27 12 | */ 13 | public class ObservableVehicleInfo implements Observable { 14 | 15 | private boolean isSelected; 16 | private String logoUrl; 17 | private String brand; 18 | private String description; 19 | 20 | public ObservableVehicleInfo(boolean isSelected, String logoUrl, String brand, String description) { 21 | this.isSelected = isSelected; 22 | this.logoUrl = logoUrl; 23 | this.brand = brand; 24 | this.description = description; 25 | } 26 | 27 | public ObservableVehicleInfo(VehicleInfo vehicleInfo) { 28 | this.isSelected = vehicleInfo.isSelected.get(); 29 | this.logoUrl = vehicleInfo.logoUrl; 30 | this.brand = vehicleInfo.brand; 31 | this.description = vehicleInfo.description; 32 | } 33 | 34 | @Bindable 35 | public boolean getIsSelected() { 36 | return isSelected; 37 | } 38 | 39 | public void setIsSelected(boolean isSelected) { 40 | this.isSelected = isSelected; 41 | notifyPropertyChanged(BR.isSelected); 42 | } 43 | 44 | public String getLogoUrl() { 45 | return logoUrl; 46 | } 47 | 48 | public void setLogoUrl(String logoUrl) { 49 | this.logoUrl = logoUrl; 50 | } 51 | 52 | public String getBrand() { 53 | return brand; 54 | } 55 | 56 | public void setBrand(String brand) { 57 | this.brand = brand; 58 | } 59 | 60 | public String getDescription() { 61 | return description; 62 | } 63 | 64 | public void setDescription(String description) { 65 | this.description = description; 66 | } 67 | 68 | //for data binding Observable 69 | private transient PropertyChangeRegistry mCallbacks; 70 | @Override 71 | public synchronized void addOnPropertyChangedCallback(OnPropertyChangedCallback onPropertyChangedCallback) { 72 | if (mCallbacks == null) { 73 | mCallbacks = new PropertyChangeRegistry(); 74 | } 75 | mCallbacks.add(onPropertyChangedCallback); 76 | } 77 | 78 | @Override 79 | public synchronized void removeOnPropertyChangedCallback(OnPropertyChangedCallback onPropertyChangedCallback) { 80 | if (mCallbacks != null) { 81 | mCallbacks.remove(onPropertyChangedCallback); 82 | } 83 | } 84 | 85 | /** 86 | * Notifies listeners that all properties of this instance have changed. 87 | */ 88 | public synchronized void notifyChange() { 89 | if (mCallbacks != null) { 90 | mCallbacks.notifyCallbacks(this, 0, null); 91 | } 92 | } 93 | 94 | /** 95 | * Notifies listeners that a specific property has changed. The getter for the property 96 | * that changes should be marked with {@link Bindable} to generate a field in 97 | * BR to be used as fieldId. 98 | * 99 | * @param fieldId The generated BR id for the Bindable field. 100 | */ 101 | public void notifyPropertyChanged(int fieldId) { 102 | if (mCallbacks != null) { 103 | mCallbacks.notifyCallbacks(this, fieldId, null); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/model/VehicleInfo.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.model; 2 | 3 | import android.databinding.ObservableBoolean; 4 | 5 | /** 6 | * @author YanLu 7 | * @since 16/4/27 8 | */ 9 | public class VehicleInfo extends BaseModel { 10 | 11 | public ObservableBoolean isSelected; 12 | public String logoUrl; 13 | public String brand; 14 | public String description; 15 | 16 | public VehicleInfo(ObservableBoolean isSelected, String logoUrl, String brand, String description) { 17 | this.isSelected = isSelected; 18 | this.logoUrl = logoUrl; 19 | this.brand = brand; 20 | this.description = description; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/observable/OnClickObservable.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.observable; 2 | 3 | 4 | import com.google.android.agera.BaseObservable; 5 | 6 | /** 7 | * @author YanLu 8 | * @since 16/5/15 9 | */ 10 | public abstract class OnClickObservable extends BaseObservable { 11 | public abstract void onClick( ); 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/recycleview/ComplexRecycleViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.recycleview; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.widget.Toast; 7 | 8 | import com.github.captain_miao.agera.tutorial.R; 9 | import com.github.captain_miao.agera.tutorial.base.BaseActivity; 10 | import com.github.captain_miao.agera.tutorial.helper.PicassoOnScrollListener; 11 | import com.github.captain_miao.agera.tutorial.model.ApiResult; 12 | import com.github.captain_miao.agera.tutorial.model.GirlInfo; 13 | import com.github.captain_miao.agera.tutorial.supplier.GirlsSupplier; 14 | import com.github.captain_miao.recyclerviewutils.WrapperRecyclerView; 15 | import com.github.captain_miao.recyclerviewutils.common.DefaultLoadMoreFooterView; 16 | import com.github.captain_miao.recyclerviewutils.listener.RefreshRecyclerViewListener; 17 | import com.google.android.agera.Function; 18 | import com.google.android.agera.MutableRepository; 19 | import com.google.android.agera.Receiver; 20 | import com.google.android.agera.Repositories; 21 | import com.google.android.agera.Repository; 22 | import com.google.android.agera.RepositoryConfig; 23 | import com.google.android.agera.Result; 24 | import com.google.android.agera.Updatable; 25 | 26 | import java.util.concurrent.ExecutorService; 27 | import java.util.concurrent.Executors; 28 | 29 | import static com.google.android.agera.Result.absentIfNull; 30 | 31 | public class ComplexRecycleViewActivity extends BaseActivity implements RefreshRecyclerViewListener, Updatable, Receiver> { 32 | private static final String TAG = "RecycleViewActivity"; 33 | 34 | private WrapperRecyclerView mRefreshRecyclerView; 35 | private ComplexRvAdapter mAdapter; 36 | @Override 37 | public void init(Bundle savedInstanceState) { 38 | setContentView(R.layout.activity_recycle_view); 39 | mRefreshRecyclerView = (WrapperRecyclerView) findViewById(R.id.refresh_recycler_view); 40 | mAdapter = new ComplexRvAdapter(); 41 | mRefreshRecyclerView.setAdapter(mAdapter); 42 | 43 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 44 | mRefreshRecyclerView.setLayoutManager(linearLayoutManager); 45 | mRefreshRecyclerView.setRecyclerViewListener(this); 46 | mAdapter.setLoadMoreFooterView(new DefaultLoadMoreFooterView(this)); 47 | mRefreshRecyclerView.setPadding(0, 0, 0, 20); 48 | 49 | mRefreshRecyclerView.getRecyclerView().addOnScrollListener(new PicassoOnScrollListener(this)); 50 | 51 | setUpRepository(); 52 | mAdapter.setRepository(Repositories.mutableRepository(new ApiResult())); 53 | //mRefreshRecyclerView.getPtrFrameLayout().autoRefresh(); 54 | } 55 | 56 | 57 | @Override 58 | public void onRefresh() { 59 | mPagination = 1; 60 | mMutableRepository.accept(mPagination); 61 | } 62 | 63 | @Override 64 | public void onLoadMore(int pagination, int pageSize) { 65 | mRefreshRecyclerView.showLoadMoreView(); 66 | mPagination = pagination; 67 | mMutableRepository.accept(mPagination); 68 | } 69 | 70 | 71 | //for agera 72 | private ExecutorService networkExecutor; 73 | private MutableRepository mMutableRepository; 74 | private Repository>> mLoadDataRepository; 75 | 76 | @Override 77 | protected void onResume() { 78 | super.onResume(); 79 | mLoadDataRepository.addUpdatable(this); 80 | } 81 | 82 | @Override 83 | protected void onPause() { 84 | super.onPause(); 85 | mLoadDataRepository.removeUpdatable(this); 86 | } 87 | 88 | @Override 89 | protected void onDestroy() { 90 | super.onDestroy(); 91 | networkExecutor.shutdown(); 92 | } 93 | 94 | private int mPagination = 1; 95 | 96 | private void setUpRepository() { 97 | networkExecutor = Executors.newSingleThreadExecutor(); 98 | 99 | mMutableRepository = Repositories.mutableRepository(mPagination); 100 | 101 | mLoadDataRepository = Repositories.repositoryWithInitialValue(Result.>absent()) 102 | .observe(mMutableRepository) 103 | .onUpdatesPerLoop() 104 | .goTo(networkExecutor) 105 | .attemptGetFrom(new GirlsSupplier(mMutableRepository)).orSkip() 106 | .thenTransform(new Function, Result>>() { 107 | @NonNull 108 | @Override 109 | public Result> apply(@NonNull ApiResult input) { 110 | return absentIfNull(input); 111 | } 112 | }) 113 | .onDeactivation(RepositoryConfig.SEND_INTERRUPT) 114 | .compile(); 115 | 116 | } 117 | 118 | @Override 119 | public void update() { 120 | Result> result = mLoadDataRepository.get(); 121 | 122 | result.ifSucceededSendTo(this) 123 | .ifFailedSendTo(new Receiver() { 124 | @Override 125 | public void accept(@NonNull Throwable value) { 126 | Toast.makeText(ComplexRecycleViewActivity.this, "load data fail", Toast.LENGTH_LONG).show(); 127 | if (mPagination == 1) { 128 | mRefreshRecyclerView.refreshComplete(); 129 | } else { 130 | mRefreshRecyclerView.hideFooterView(); 131 | } 132 | mRefreshRecyclerView.loadMoreComplete(); 133 | } 134 | }); 135 | 136 | } 137 | 138 | 139 | @Override 140 | public void accept(@NonNull ApiResult result) { 141 | if (mPagination == 1) { 142 | mAdapter.clear(); 143 | mAdapter.addAll(result.results); 144 | mRefreshRecyclerView.refreshComplete(); 145 | } else { 146 | if (result.results != null && result.results.size() > 0) { 147 | mAdapter.getRepository().accept(result); 148 | //mAdapter.addAll(result.results, false); 149 | //int size = result.results.size(); 150 | //mAdapter.notifyItemRangeInserted(mAdapter.getItemCount() - size, size); 151 | mRefreshRecyclerView.hideFooterView(); 152 | } else { 153 | Toast.makeText(this, "It's no more data.", Toast.LENGTH_LONG).show(); 154 | if(mAdapter.getItemCount() > 0) { 155 | mRefreshRecyclerView.showNoMoreDataView(); 156 | } else { 157 | mRefreshRecyclerView.hideFooterView(); 158 | } 159 | } 160 | mRefreshRecyclerView.loadMoreComplete(); 161 | } 162 | } 163 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/recycleview/ComplexRvAdapter.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.recycleview; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.databinding.ViewDataBinding; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import com.github.captain_miao.agera.tutorial.BR; 11 | import com.github.captain_miao.agera.tutorial.R; 12 | import com.github.captain_miao.agera.tutorial.model.ApiResult; 13 | import com.github.captain_miao.agera.tutorial.model.GirlInfo; 14 | import com.github.captain_miao.recyclerviewutils.BaseWrapperRecyclerAdapter; 15 | import com.google.android.agera.MutableRepository; 16 | import com.google.android.agera.Updatable; 17 | 18 | import java.util.List; 19 | 20 | 21 | /** 22 | * @author YanLu 23 | * @since 16/4/27 24 | */ 25 | public class ComplexRvAdapter extends BaseWrapperRecyclerAdapter implements Updatable{ 26 | 27 | public ComplexRvAdapter() { 28 | } 29 | 30 | public ComplexRvAdapter(List items) { 31 | addAll(items); 32 | } 33 | 34 | @Override 35 | public RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) { 36 | View view = LayoutInflater.from(parent.getContext()) 37 | .inflate(R.layout.recycler_item_view, parent, false); 38 | 39 | return new ComplexRvAdapter.ViewHolder(view); 40 | } 41 | 42 | @Override 43 | public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { 44 | final GirlInfo info = getItem(position); 45 | if(holder instanceof ViewHolder){ 46 | ViewDataBinding binding = ((ViewHolder) holder).getBinding(); 47 | binding.setVariable(BR.info, info); 48 | 49 | binding.executePendingBindings(); 50 | } 51 | } 52 | 53 | 54 | 55 | public static class ViewHolder extends RecyclerView.ViewHolder { 56 | private ViewDataBinding mBinding; 57 | 58 | public ViewHolder(View itemView) { 59 | super(itemView); 60 | mBinding = DataBindingUtil.bind(itemView); 61 | } 62 | 63 | public ViewDataBinding getBinding() { 64 | return mBinding; 65 | } 66 | } 67 | 68 | private MutableRepository> mRepository; 69 | 70 | @Override 71 | public void update() { 72 | ApiResult result = mRepository.get(); 73 | if (!result.error) { 74 | 75 | if (result.results != null && result.results.size() > 0) { 76 | addAll(result.results, false); 77 | int size = result.results.size(); 78 | notifyItemRangeInserted(getItemCount() - size, size); 79 | } 80 | } 81 | 82 | } 83 | 84 | public MutableRepository> getRepository() { 85 | return mRepository; 86 | } 87 | 88 | public void setRepository(MutableRepository> repository) { 89 | mRepository = repository; 90 | mRepository.addUpdatable(this); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/recycleview/GirlInfoPresenter.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.recycleview; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.widget.RecyclerView; 6 | 7 | import com.github.captain_miao.agera.tutorial.BR; 8 | import com.github.captain_miao.agera.tutorial.R; 9 | import com.github.captain_miao.agera.tutorial.databinding.RecyclerItemViewBinding; 10 | import com.github.captain_miao.agera.tutorial.model.GirlInfo; 11 | import com.google.android.agera.Result; 12 | import com.google.android.agera.rvadapter.RepositoryPresenter; 13 | 14 | import java.util.List; 15 | 16 | /** 17 | * @author YanLu 18 | * @since 16/5/23 19 | */ 20 | public class GirlInfoPresenter extends RepositoryPresenter>> { 21 | 22 | @Override 23 | public int getItemCount(@NonNull Result> data) { 24 | if (data.succeeded()) { 25 | return data.get().size(); 26 | } 27 | return 0; 28 | } 29 | 30 | @Override 31 | public int getLayoutResId(@NonNull Result> data, int index) { 32 | return R.layout.recycler_item_view; 33 | } 34 | 35 | @Override 36 | public void bind(@NonNull Result> data, int index, @NonNull RecyclerView.ViewHolder holder) { 37 | if (data.isAbsent() || data.failed()) { 38 | return; 39 | } 40 | final GirlInfo info = data.get().get(index); 41 | final RecyclerItemViewBinding binding = DataBindingUtil.bind(holder.itemView); 42 | binding.setVariable(BR.info, info); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/recycleview/GirlListAdapter.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.recycleview; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.databinding.ViewDataBinding; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import com.github.captain_miao.agera.tutorial.BR; 11 | import com.github.captain_miao.agera.tutorial.R; 12 | import com.github.captain_miao.agera.tutorial.model.GirlInfo; 13 | import com.github.captain_miao.recyclerviewutils.BaseWrapperRecyclerAdapter; 14 | 15 | import java.util.List; 16 | 17 | 18 | /** 19 | * @author YanLu 20 | * @since 16/4/27 21 | */ 22 | public class GirlListAdapter extends BaseWrapperRecyclerAdapter { 23 | 24 | public GirlListAdapter() { 25 | } 26 | 27 | public GirlListAdapter(List items) { 28 | addAll(items); 29 | } 30 | 31 | @Override 32 | public RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) { 33 | View view = LayoutInflater.from(parent.getContext()) 34 | .inflate(R.layout.recycler_item_view, parent, false); 35 | 36 | return new GirlListAdapter.ViewHolder(view); 37 | } 38 | 39 | @Override 40 | public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) { 41 | final GirlInfo info = getItem(position); 42 | if(holder instanceof ViewHolder){ 43 | ViewDataBinding binding = ((ViewHolder) holder).getBinding(); 44 | binding.setVariable(BR.info, info); 45 | binding.executePendingBindings(); 46 | } 47 | } 48 | 49 | 50 | 51 | public static class ViewHolder extends RecyclerView.ViewHolder { 52 | private ViewDataBinding mBinding; 53 | 54 | public ViewHolder(View itemView) { 55 | super(itemView); 56 | mBinding = DataBindingUtil.bind(itemView); 57 | } 58 | 59 | public ViewDataBinding getBinding() { 60 | return mBinding; 61 | } 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/recycleview/RecycleViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.recycleview; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.widget.Toast; 7 | 8 | import com.github.captain_miao.agera.tutorial.R; 9 | import com.github.captain_miao.agera.tutorial.base.BaseActivity; 10 | import com.github.captain_miao.agera.tutorial.helper.PicassoOnScrollListener; 11 | import com.github.captain_miao.agera.tutorial.helper.UiThreadExecutor; 12 | import com.github.captain_miao.agera.tutorial.listener.SimpleObservable; 13 | import com.github.captain_miao.agera.tutorial.model.ApiResult; 14 | import com.github.captain_miao.agera.tutorial.model.GirlInfo; 15 | import com.github.captain_miao.agera.tutorial.supplier.GirlsSupplier; 16 | import com.github.captain_miao.recyclerviewutils.WrapperRecyclerView; 17 | import com.github.captain_miao.recyclerviewutils.common.DefaultLoadMoreFooterView; 18 | import com.github.captain_miao.recyclerviewutils.listener.RefreshRecyclerViewListener; 19 | import com.google.android.agera.Function; 20 | import com.google.android.agera.Repositories; 21 | import com.google.android.agera.Repository; 22 | import com.google.android.agera.RepositoryConfig; 23 | import com.google.android.agera.Result; 24 | import com.google.android.agera.Supplier; 25 | import com.google.android.agera.Updatable; 26 | 27 | import java.util.concurrent.Executor; 28 | import java.util.concurrent.ExecutorService; 29 | import java.util.concurrent.Executors; 30 | 31 | public class RecycleViewActivity extends BaseActivity implements RefreshRecyclerViewListener, Updatable { 32 | private static final String TAG = "RecycleViewActivity"; 33 | 34 | private WrapperRecyclerView mRefreshRecyclerView; 35 | private GirlListAdapter mAdapter; 36 | @Override 37 | public void init(Bundle savedInstanceState) { 38 | setContentView(R.layout.activity_recycle_view); 39 | mRefreshRecyclerView = (WrapperRecyclerView) findViewById(R.id.refresh_recycler_view); 40 | mAdapter = new GirlListAdapter(); 41 | mRefreshRecyclerView.setAdapter(mAdapter); 42 | 43 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 44 | mRefreshRecyclerView.setLayoutManager(linearLayoutManager); 45 | mRefreshRecyclerView.setRecyclerViewListener(this); 46 | mAdapter.setLoadMoreFooterView(new DefaultLoadMoreFooterView(this)); 47 | mRefreshRecyclerView.setPadding(0, 0, 0, 20); 48 | 49 | mRefreshRecyclerView.getRecyclerView().addOnScrollListener(new PicassoOnScrollListener(this)); 50 | 51 | setUpRepository(); 52 | } 53 | 54 | 55 | @Override 56 | public void onRefresh() { 57 | mPagination = 1; 58 | mObservable.update(); 59 | } 60 | 61 | @Override 62 | public void onLoadMore(int pagination, int pageSize) { 63 | mRefreshRecyclerView.showLoadMoreView(); 64 | mPagination = pagination; 65 | mObservable.update(); 66 | } 67 | 68 | 69 | //for agera 70 | private ExecutorService networkExecutor; 71 | private Executor uiExecutor; 72 | private SimpleObservable mObservable; 73 | private Repository>> mRepository; 74 | 75 | @Override 76 | protected void onResume() { 77 | super.onResume(); 78 | mRepository.addUpdatable(this); 79 | } 80 | 81 | @Override 82 | protected void onPause() { 83 | super.onPause(); 84 | mRepository.removeUpdatable(this); 85 | } 86 | 87 | @Override 88 | protected void onDestroy() { 89 | super.onDestroy(); 90 | networkExecutor.shutdown(); 91 | } 92 | 93 | private int mPagination = 1; 94 | private void setUpRepository() { 95 | networkExecutor = Executors.newSingleThreadExecutor(); 96 | uiExecutor = UiThreadExecutor.newUiThreadExecutor(); 97 | mObservable = new SimpleObservable() { }; 98 | 99 | 100 | mRepository = Repositories.repositoryWithInitialValue(Result.>absent()) 101 | .observe(mObservable) 102 | .onUpdatesPerLoop() 103 | //.goTo(uiExecutor) 104 | .getFrom(new Supplier() { 105 | @NonNull 106 | @Override 107 | public Object get() { 108 | Toast.makeText(RecycleViewActivity.this, "load data begin", Toast.LENGTH_LONG).show(); 109 | return new Object(); 110 | } 111 | }) 112 | .goTo(networkExecutor) 113 | .getFrom(new GirlsSupplier(new Supplier() { 114 | @NonNull 115 | @Override 116 | public Integer get() { 117 | return mPagination; 118 | } 119 | })) 120 | 121 | .goTo(uiExecutor) 122 | .thenTransform(new Function>, Result>>() { 123 | @NonNull 124 | @Override 125 | public Result> apply(@NonNull Result> input) { 126 | Toast.makeText(RecycleViewActivity.this, "load data end", Toast.LENGTH_LONG).show(); 127 | return input; 128 | } 129 | }) 130 | .onDeactivation(RepositoryConfig.SEND_INTERRUPT) 131 | .compile(); 132 | } 133 | 134 | @Override 135 | public void update() { 136 | Result> result = mRepository.get(); 137 | if (result.succeeded() && !result.get().error) { 138 | if (mPagination == 1) { 139 | mAdapter.clear(); 140 | mAdapter.addAll(result.get().results); 141 | mRefreshRecyclerView.refreshComplete(); 142 | } else { 143 | if(result.get().results != null && result.get().results.size() > 0) { 144 | mAdapter.addAll(result.get().results, false); 145 | int size = result.get().results.size(); 146 | mAdapter.notifyItemRangeInserted(mAdapter.getItemCount() - size, size); 147 | } else { 148 | Toast.makeText(this, "It's no more data.", Toast.LENGTH_LONG).show(); 149 | } 150 | mRefreshRecyclerView.loadMoreComplete(); 151 | } 152 | } else { 153 | Toast.makeText(this, "load data fail", Toast.LENGTH_LONG).show(); 154 | } 155 | mRefreshRecyclerView.hideFooterView(); 156 | } 157 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/recycleview/RepositoryAdapterRecycleViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.recycleview; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | 7 | import com.github.captain_miao.agera.tutorial.R; 8 | import com.github.captain_miao.agera.tutorial.base.BaseActivity; 9 | import com.github.captain_miao.agera.tutorial.listener.SimpleObservable; 10 | import com.github.captain_miao.agera.tutorial.model.ApiResult; 11 | import com.github.captain_miao.agera.tutorial.model.GirlInfo; 12 | import com.github.captain_miao.agera.tutorial.supplier.GirlsSupplier; 13 | import com.github.captain_miao.recyclerviewutils.WrapperRecyclerView; 14 | import com.github.captain_miao.recyclerviewutils.listener.RefreshRecyclerViewListener; 15 | import com.google.android.agera.Function; 16 | import com.google.android.agera.Repositories; 17 | import com.google.android.agera.Repository; 18 | import com.google.android.agera.RepositoryConfig; 19 | import com.google.android.agera.Result; 20 | import com.google.android.agera.Supplier; 21 | import com.google.android.agera.rvadapter.RepositoryAdapter; 22 | 23 | import java.util.List; 24 | import java.util.concurrent.ExecutorService; 25 | import java.util.concurrent.Executors; 26 | 27 | public class RepositoryAdapterRecycleViewActivity extends BaseActivity implements RefreshRecyclerViewListener { 28 | private static final String TAG = "RepositoryAdapterRecycleViewActivity"; 29 | 30 | private WrapperRecyclerView mRefreshRecyclerView; 31 | private RepositoryAdapter mRepositoryAdapter; 32 | @Override 33 | public void init(Bundle savedInstanceState) { 34 | setContentView(R.layout.activity_recycle_view); 35 | mRefreshRecyclerView = (WrapperRecyclerView) findViewById(R.id.refresh_recycler_view); 36 | 37 | setUpRepository(); 38 | 39 | //can't add data 40 | mRepositoryAdapter = RepositoryAdapter.repositoryAdapter() 41 | .add(mRepository, new GirlInfoPresenter()) 42 | .build(); 43 | 44 | mRefreshRecyclerView.getRecyclerView().setAdapter(mRepositoryAdapter); 45 | 46 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 47 | mRefreshRecyclerView.setLayoutManager(linearLayoutManager); 48 | mRefreshRecyclerView.setRecyclerViewListener(this); 49 | mRefreshRecyclerView.disableLoadMore(); 50 | mRefreshRecyclerView.setPadding(0, 0, 0, 20); 51 | } 52 | 53 | 54 | @Override 55 | public void onRefresh() { 56 | mPagination = 1; 57 | mObservable.update(); 58 | mRefreshRecyclerView.refreshComplete(); 59 | } 60 | 61 | @Override 62 | public void onLoadMore(int pagination, int pageSize) { 63 | 64 | } 65 | 66 | // Agera 67 | //for agera 68 | private ExecutorService networkExecutor; 69 | private SimpleObservable mObservable; 70 | private Repository>> mRepository; 71 | 72 | @Override 73 | protected void onResume() { 74 | super.onResume(); 75 | mRepositoryAdapter.startObserving(); 76 | } 77 | 78 | @Override 79 | protected void onPause() { 80 | super.onPause(); 81 | mRepositoryAdapter.stopObserving(); 82 | } 83 | 84 | @Override 85 | protected void onDestroy() { 86 | super.onDestroy(); 87 | networkExecutor.shutdown(); 88 | } 89 | private int mPagination = 1; 90 | private void setUpRepository() { 91 | networkExecutor = Executors.newSingleThreadExecutor(); 92 | mObservable = new SimpleObservable() { }; 93 | 94 | 95 | mRepository = Repositories.repositoryWithInitialValue(Result.>absent()) 96 | .observe(mObservable) 97 | .onUpdatesPerLoop() 98 | .goTo(networkExecutor) 99 | .getFrom(new GirlsSupplier(new Supplier() { 100 | @NonNull 101 | @Override 102 | public Integer get() { 103 | return mPagination; 104 | } 105 | })) 106 | .thenTransform(new Function>, Result>>() { 107 | @NonNull 108 | @Override 109 | public Result> apply(@NonNull Result> input) { 110 | if (input.succeeded() && !input.get().error) { 111 | return Result.success(input.get().results); 112 | } else { 113 | return Result.absent(); 114 | } 115 | } 116 | }) 117 | .onDeactivation(RepositoryConfig.SEND_INTERRUPT) 118 | .compile(); 119 | } 120 | 121 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/supplier/GirlsSupplier.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.supplier; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.github.captain_miao.agera.tutorial.http.DemoApiService; 6 | import com.github.captain_miao.agera.tutorial.http.RetrofitServiceFactory; 7 | import com.github.captain_miao.agera.tutorial.model.ApiResult; 8 | import com.github.captain_miao.agera.tutorial.model.GirlInfo; 9 | import com.google.android.agera.Result; 10 | import com.google.android.agera.Supplier; 11 | 12 | import java.io.IOException; 13 | 14 | /** 15 | * @author YanLu 16 | * @since 16/5/16 17 | */ 18 | public class GirlsSupplier implements Supplier>> { 19 | private String baseUrl = "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/"; 20 | private Supplier mSupplierPagination; 21 | 22 | public GirlsSupplier(@NonNull Supplier supplier ) { 23 | mSupplierPagination = supplier; 24 | } 25 | 26 | @NonNull 27 | @Override 28 | public Result> get() { 29 | return loadGirls(); 30 | } 31 | 32 | private Result> loadGirls() { 33 | DemoApiService apiService = RetrofitServiceFactory.createService(DemoApiService.class, baseUrl); 34 | ApiResult girlInfos = null; 35 | try { 36 | girlInfos = (apiService.getGirls(mSupplierPagination.get()).execute().body()); 37 | } catch (IOException e) { 38 | e.printStackTrace(); 39 | } 40 | if(girlInfos != null){ 41 | return !girlInfos.error ? Result.success(girlInfos) : Result.>failure(); 42 | } else { 43 | return Result.failure(); 44 | } 45 | } 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/supplier/ImageSupplier.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.supplier; 2 | 3 | import android.graphics.Bitmap; 4 | import android.support.annotation.NonNull; 5 | 6 | import com.github.captain_miao.agera.tutorial.AppAgera; 7 | import com.google.android.agera.Result; 8 | import com.google.android.agera.Supplier; 9 | import com.squareup.picasso.Picasso; 10 | 11 | import java.io.IOException; 12 | 13 | import static com.google.android.agera.Result.absentIfNull; 14 | 15 | /** 16 | * @author YanLu 17 | * @since 16/5/16 18 | */ 19 | public class ImageSupplier implements Supplier> { 20 | private String mUri; 21 | 22 | public ImageSupplier(String uri) { 23 | mUri = uri; 24 | } 25 | 26 | @NonNull 27 | @Override 28 | public Result get() { 29 | return loadImage(); 30 | } 31 | 32 | private Result loadImage() { 33 | Bitmap bitmap = null; 34 | try { 35 | bitmap = Picasso.with(AppAgera.getAppContext()).load(mUri).get(); 36 | } catch (IOException e) { 37 | e.printStackTrace(); 38 | } 39 | 40 | return absentIfNull(bitmap); 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/viewpage/GuideViewModel.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.viewpage; 2 | 3 | /** 4 | * @author YanLu 5 | * @since 16/5/13 6 | */ 7 | public class GuideViewModel { 8 | public int bgColor; 9 | public String title; 10 | public String description; 11 | public int imageRes; 12 | 13 | public GuideViewModel(int imageRes) { 14 | this.imageRes = imageRes; 15 | } 16 | 17 | public GuideViewModel(int bgColor, String title, String description, int imageRes) { 18 | this.bgColor = bgColor; 19 | this.title = title; 20 | this.description = description; 21 | this.imageRes = imageRes; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/viewpage/ViewPageActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.viewpage; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.support.v4.view.ViewPager; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import com.github.captain_miao.agera.tutorial.R; 11 | import com.github.captain_miao.agera.tutorial.base.BasePagerAdapter; 12 | import com.github.captain_miao.agera.tutorial.databinding.ViewPageItemViewBinding; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | 18 | /** 19 | * new user guide 20 | */ 21 | public class ViewPageActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener { 22 | private ViewPageDotView mWelcomeDotView; 23 | 24 | @Override 25 | public void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_guide); 28 | ViewPager mViewPager = (ViewPager) findViewById(R.id.view_page); 29 | 30 | final GuideAdapter guideAdapter = new GuideAdapter(ViewPageActivity.this); 31 | guideAdapter.addAll(getGuideViewModels()); 32 | mViewPager.setAdapter(guideAdapter); 33 | mViewPager.addOnPageChangeListener(this); 34 | 35 | mWelcomeDotView = (ViewPageDotView) findViewById(R.id.dot_view); 36 | mWelcomeDotView.setNumOfCircles(guideAdapter.getCount(), getResources().getDimensionPixelSize(R.dimen.height_very_small)); 37 | 38 | } 39 | 40 | @Override 41 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 42 | mWelcomeDotView.onPageScrolled(position, positionOffset, positionOffsetPixels); 43 | } 44 | 45 | @Override 46 | public void onPageSelected(int position) { 47 | mWelcomeDotView.onPageSelected(position); 48 | } 49 | 50 | @Override 51 | public void onPageScrollStateChanged(int state) { 52 | mWelcomeDotView.onPageScrollStateChanged(state); 53 | } 54 | 55 | 56 | private class GuideAdapter extends BasePagerAdapter { 57 | 58 | public GuideAdapter(Context c) { 59 | super(c); 60 | } 61 | 62 | @Override 63 | public Object instantiateItem(ViewGroup container, int position) { 64 | GuideViewModel model = getItem(position); 65 | 66 | ViewPageItemViewBinding frgGuideBinding 67 | = ViewPageItemViewBinding.inflate(mInflater, container, false); 68 | frgGuideBinding.setViewModel(model); 69 | container.addView(frgGuideBinding.getRoot()); 70 | return frgGuideBinding.getRoot(); 71 | 72 | } 73 | 74 | @Override 75 | public void destroyItem(ViewGroup container, int position, Object object) { 76 | if(object instanceof View) { 77 | container.removeView((View) object); 78 | } 79 | } 80 | 81 | 82 | @Override 83 | public boolean isViewFromObject(View view, Object object) { 84 | return view == object; 85 | } 86 | } 87 | 88 | 89 | @SuppressWarnings("deprecation") 90 | private List getGuideViewModels() { 91 | return new ArrayList() {{ 92 | add(new GuideViewModel(getResources().getColor(R.color.guide_bg_color_1), getString(R.string.guide_title_1), 93 | getString(R.string.guide_description_1), R.drawable.guide_1)); 94 | 95 | add(new GuideViewModel(getResources().getColor(R.color.guide_bg_color_2), getString(R.string.guide_title_2), 96 | getString(R.string.guide_description_2), R.drawable.guide_2)); 97 | 98 | add(new GuideViewModel(getResources().getColor(R.color.guide_bg_color_3), getString(R.string.guide_title_3), 99 | getString(R.string.guide_description_3), R.drawable.guide_3)); 100 | }}; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/captain_miao/agera/tutorial/viewpage/ViewPageDotView.java: -------------------------------------------------------------------------------- 1 | package com.github.captain_miao.agera.tutorial.viewpage; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.Color; 6 | import android.graphics.Paint; 7 | import android.os.Parcel; 8 | import android.os.Parcelable; 9 | import android.support.v4.view.ViewPager; 10 | import android.util.AttributeSet; 11 | import android.view.View; 12 | 13 | /** 14 | * 15 | */ 16 | public class ViewPageDotView extends View { 17 | public static int DEFAULT_RADIUS = 8; 18 | public static int DEFAULT_STROKE_COLOR = Color.parseColor("#999999"); 19 | public static int DEFAULT_FILL_COLOR = Color.parseColor("#ffffff"); 20 | 21 | private int mNumOfCircles = 0; 22 | private int mScreenWidth; 23 | private float mRadius = DEFAULT_RADIUS; 24 | private float mDistance; 25 | private float[] mXPositions; 26 | private float mYPositions; 27 | 28 | private float mLeftOrRightSpace; 29 | private float mAllDotsDomainLength; 30 | private float mOnceMovedTotalDistance; 31 | 32 | private Paint mPaintStroke; 33 | private int mStrokeColor = DEFAULT_STROKE_COLOR; 34 | private Paint mPaintFill; 35 | private int mFillColor = DEFAULT_FILL_COLOR; 36 | 37 | private float mScrollRatio; // current drag ratio 38 | 39 | private int mCurrentIndex; 40 | private int mState; 41 | 42 | public ViewPageDotView(Context context) { 43 | this(context, (AttributeSet) null); 44 | } 45 | 46 | public ViewPageDotView(Context context, AttributeSet attrs) { 47 | super(context, attrs); 48 | initPaint(); 49 | initLengthRelatedVariables(); 50 | } 51 | 52 | public void setNumOfCircles(int numOfCircles){ 53 | setNumOfCircles(numOfCircles, DEFAULT_RADIUS); 54 | } 55 | 56 | public void setNumOfCircles(int numOfCircles, float radius) { 57 | if (numOfCircles <= 1) { 58 | this.setVisibility(View.INVISIBLE); 59 | return; 60 | } 61 | this.mRadius = radius; 62 | initLengthRelatedVariables(); 63 | mNumOfCircles = numOfCircles; 64 | mAllDotsDomainLength = 2 * mRadius * mNumOfCircles + mDistance * (mNumOfCircles - 1); 65 | mLeftOrRightSpace = (mScreenWidth - mAllDotsDomainLength) / 2.0f; 66 | mXPositions = new float[mNumOfCircles]; 67 | for (int i = 0; i < mNumOfCircles; i++) { 68 | mXPositions[i] = mLeftOrRightSpace + mRadius * (i * 2 + 1) + mDistance * i; 69 | } 70 | } 71 | 72 | 73 | /** 74 | * 设置画笔相关变量 75 | */ 76 | private void initPaint() { 77 | mPaintStroke = new Paint(); 78 | mPaintStroke.setColor(mStrokeColor); 79 | mPaintStroke.setAntiAlias(true); 80 | mPaintFill = new Paint(); 81 | mPaintFill.setColor(mFillColor); 82 | mPaintFill.setAntiAlias(true); 83 | } 84 | 85 | /** 86 | * 设置距离相关变量 87 | */ 88 | private void initLengthRelatedVariables() { 89 | mScreenWidth = getResources().getDisplayMetrics().widthPixels; 90 | mDistance = mRadius * 2; 91 | mYPositions = mRadius; 92 | mOnceMovedTotalDistance = mRadius * 2 + mDistance; 93 | } 94 | 95 | @Override 96 | protected void onDraw(Canvas canvas) { 97 | super.onDraw(canvas); 98 | drawStrokeCircle(canvas); 99 | drawFillCircle(canvas); 100 | } 101 | 102 | /** 103 | * 绘制静态圆点 104 | * 105 | * @param canvas 106 | */ 107 | private void drawStrokeCircle(Canvas canvas) { 108 | if (mNumOfCircles <= 0) { 109 | return; 110 | } 111 | for (int i = 0; i < mNumOfCircles; i++) { 112 | canvas.drawCircle(mXPositions[i], mYPositions, mRadius, mPaintStroke); 113 | } 114 | } 115 | 116 | /** 117 | * 绘制动态圆点 118 | * 119 | * @param canvas 120 | */ 121 | private void drawFillCircle(Canvas canvas) { 122 | if (mNumOfCircles <= 0) { 123 | return; 124 | } 125 | float offset = mLeftOrRightSpace + mRadius + mCurrentIndex * mOnceMovedTotalDistance + mScrollRatio * mOnceMovedTotalDistance; 126 | if (offset < mLeftOrRightSpace + mRadius) { 127 | offset = mLeftOrRightSpace + mRadius; 128 | } 129 | if (offset > (mLeftOrRightSpace + mRadius) + (mNumOfCircles - 1) * mOnceMovedTotalDistance) { 130 | offset = mLeftOrRightSpace + mRadius + (mNumOfCircles - 1) * mOnceMovedTotalDistance; 131 | } 132 | canvas.drawCircle(offset, mYPositions, mRadius, mPaintFill); 133 | } 134 | 135 | public void onPageScrolled(int canSeePageIndex, float v, int i2) { 136 | mScrollRatio = v; 137 | mCurrentIndex = canSeePageIndex; 138 | invalidate(); 139 | } 140 | 141 | public void onPageSelected(int currentIndex) { 142 | if (mState == ViewPager.SCROLL_STATE_IDLE) { 143 | mCurrentIndex = currentIndex; 144 | invalidate(); 145 | } 146 | } 147 | 148 | public void onPageScrollStateChanged(int i) { 149 | mState = i; 150 | } 151 | 152 | @Override 153 | public void onRestoreInstanceState(Parcelable state) { 154 | ViewPageDotView.SavedState savedState = (ViewPageDotView.SavedState) state; 155 | super.onRestoreInstanceState(savedState.getSuperState()); 156 | mCurrentIndex = savedState.currentPage; 157 | this.requestLayout(); 158 | } 159 | 160 | @Override 161 | public Parcelable onSaveInstanceState() { 162 | Parcelable superState = super.onSaveInstanceState(); 163 | ViewPageDotView.SavedState savedState = new ViewPageDotView.SavedState(superState); 164 | savedState.currentPage = this.mCurrentIndex; 165 | return savedState; 166 | } 167 | 168 | static class SavedState extends BaseSavedState { 169 | int currentPage; 170 | public static final Creator CREATOR = new Creator() { 171 | public ViewPageDotView.SavedState createFromParcel(Parcel in) { 172 | return new ViewPageDotView.SavedState(in); 173 | } 174 | 175 | public ViewPageDotView.SavedState[] newArray(int size) { 176 | return new ViewPageDotView.SavedState[size]; 177 | } 178 | }; 179 | 180 | public SavedState(Parcelable superState) { 181 | super(superState); 182 | } 183 | 184 | private SavedState(Parcel in) { 185 | super(in); 186 | this.currentPage = in.readInt(); 187 | } 188 | 189 | public void writeToParcel(Parcel dest, int flags) { 190 | super.writeToParcel(dest, flags); 191 | dest.writeInt(this.currentPage); 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /app/src/main/res/anim/slide_in_left.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 24 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/anim/slide_in_right.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 24 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/anim/slide_out_left.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 24 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/anim/slide_out_right.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 24 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/guide_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/captain-miao/AndroidAgeraTutorial/6a4fa1082865bc5d4715dcd78adc9fad00d4f894/app/src/main/res/drawable-xhdpi/guide_1.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/guide_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/captain-miao/AndroidAgeraTutorial/6a4fa1082865bc5d4715dcd78adc9fad00d4f894/app/src/main/res/drawable-xhdpi/guide_2.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/guide_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/captain-miao/AndroidAgeraTutorial/6a4fa1082865bc5d4715dcd78adc9fad00d4f894/app/src/main/res/drawable-xhdpi/guide_3.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_check_circle.xml: -------------------------------------------------------------------------------- 1 | 3 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_image_load_error.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_image_load_place_holder.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_uncheck_circle.xml: -------------------------------------------------------------------------------- 1 | 3 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_agera_functions.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 17 | 18 | 29 | 30 |