├── .gitignore
├── LICENSE
├── README.md
├── app
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── hi
│ │ └── dhl
│ │ └── demo
│ │ └── binding
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── hi
│ │ │ └── dhl
│ │ │ └── demo
│ │ │ └── binding
│ │ │ ├── App.kt
│ │ │ ├── MainActivity.kt
│ │ │ ├── MainViewModel.kt
│ │ │ ├── brvah
│ │ │ └── BRVAHActivity.kt
│ │ │ ├── databind
│ │ │ ├── BindViewStubActivity.kt
│ │ │ ├── BindingAdapter.kt
│ │ │ ├── DatBindActivity.kt
│ │ │ ├── DataBindCustomView.kt
│ │ │ ├── DataBindDialog.kt
│ │ │ ├── DataBindIncludeActivity.kt
│ │ │ └── list
│ │ │ │ ├── DataBindRecycleActivity.kt
│ │ │ │ ├── DataBindRecycleFragment.kt
│ │ │ │ ├── ListViewModel.kt
│ │ │ │ └── ProductAdapter.kt
│ │ │ ├── di
│ │ │ └── AppModule.kt
│ │ │ ├── navigation
│ │ │ ├── FragmentNav1.kt
│ │ │ ├── FragmentNav2.kt
│ │ │ ├── FragmentNav3.kt
│ │ │ └── NavigationActivity.kt
│ │ │ ├── viewbind
│ │ │ ├── ViewBindActivity.kt
│ │ │ ├── ViewBindCustomView.kt
│ │ │ ├── ViewBindDialog.kt
│ │ │ ├── ViewBindFragment.kt
│ │ │ ├── ViewBindIncludeActivity.kt
│ │ │ └── nested
│ │ │ │ ├── NestedActivity.kt
│ │ │ │ └── NestedFragment1.kt
│ │ │ └── viewpager2
│ │ │ ├── Fragment1.kt
│ │ │ ├── Fragment2.kt
│ │ │ ├── Fragment3.kt
│ │ │ ├── Fragment4.kt
│ │ │ ├── Fragment5.kt
│ │ │ └── ViewPager2Activity.kt
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ ├── avatar.jpeg
│ │ ├── ic_launcher_background.xml
│ │ └── logo.png
│ │ ├── layout
│ │ ├── activity_brvah.xml
│ │ ├── activity_data_bind.xml
│ │ ├── activity_data_bind_recycle.xml
│ │ ├── activity_include.xml
│ │ ├── activity_include_view_bind.xml
│ │ ├── activity_main.xml
│ │ ├── activity_navigation.xml
│ │ ├── activity_nested.xml
│ │ ├── activity_view_bind.xml
│ │ ├── activity_view_stub.xml
│ │ ├── activity_viewpager.xml
│ │ ├── dialog_app.xml
│ │ ├── dialog_data_binding.xml
│ │ ├── fragment1.xml
│ │ ├── fragment2.xml
│ │ ├── fragment3.xml
│ │ ├── fragment4.xml
│ │ ├── fragment5.xml
│ │ ├── fragment_data_bind_recycle.xml
│ │ ├── fragment_nav_1.xml
│ │ ├── fragment_nav_2.xml
│ │ ├── fragment_nav_3.xml
│ │ ├── fragment_nested1.xml
│ │ ├── fragment_nested2.xml
│ │ ├── fragment_view_bind.xml
│ │ ├── layout_include_data_item.xml
│ │ ├── layout_include_item.xml
│ │ ├── layout_merge_data_item.xml
│ │ ├── layout_merge_item.xml
│ │ ├── layout_view_custom.xml
│ │ ├── layout_view_custom_data.xml
│ │ ├── recycle_item_product.xml
│ │ ├── recycle_item_product_footer.xml
│ │ ├── recycle_item_product_header.xml
│ │ ├── view_stub.xml
│ │ └── view_stub_data.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── navigation
│ │ └── nav_graph.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── themes.xml
│ └── test
│ └── java
│ └── com
│ └── hi
│ └── dhl
│ └── demo
│ └── binding
│ └── ExampleUnitTest.kt
├── binding
├── build.gradle
├── consumer-rules.pro
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ └── java
│ ├── androidx
│ └── lifecycle
│ │ └── BindingLifecycleObserver.kt
│ └── com
│ └── hi
│ └── dhl
│ └── binding
│ ├── base
│ ├── ActivityDelegate.kt
│ ├── DialogDelegate.kt
│ └── FragmentDelegate.kt
│ ├── databind
│ ├── ActivityDataBinding.kt
│ ├── DialogDataBinding.kt
│ ├── FragmentDataBinding.kt
│ ├── ViewGroupDataBinding.kt
│ └── ViewHolderDataBinding.kt
│ ├── ext
│ ├── ComponentExt.kt
│ ├── LifecycleExt.kt
│ └── ReflectExt.kt
│ └── viewbind
│ ├── ActivityViewBinding.kt
│ ├── DialogViewBinding.kt
│ ├── FragmentViewBinding.kt
│ ├── ViewGroupViewBinding.kt
│ └── ViewHolderViewBinding.kt
├── build.gradle
├── buildSrc
├── build.gradle.kts
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── hi
│ │ └── dhl
│ │ └── Deps.kt
│ └── test
│ └── kotlin
│ └── com
│ └── hi
│ └── dhl
│ └── DepsTest.java
├── doc
└── README_CN.md
├── gradle-mvn-push.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── settings.gradle.kts
/.gitignore:
--------------------------------------------------------------------------------
1 | # Windows:
2 | Thumbs.db
3 | ehthumbs.db
4 | Desktop.ini
5 |
6 | # Python:
7 | *.py[cod]
8 | *.so
9 | *.egg
10 | *.egg-info
11 | dist
12 | build
13 | migrations
14 |
15 | #Django
16 | *.egg-info
17 | *.pot
18 | *.py[co]
19 | .tox/
20 | __pycache__
21 | MANIFEST
22 | dist/
23 | docs/_build/
24 | docs/locale/
25 | node_modules/
26 | tests/coverage_html/
27 | tests/.coverage
28 | build/
29 | tests/report/
30 |
31 | #--------------------------------
32 | __pycache__/
33 | *.py[cod]
34 | *$py.class
35 |
36 | # C extensions
37 | *.so
38 |
39 | # Distribution / packaging
40 | .Python
41 | env/
42 | build/
43 | develop-eggs/
44 | dist/
45 | downloads/
46 | eggs/
47 | .eggs/
48 | lib/
49 | lib64/
50 | parts/
51 | sdist/
52 | var/
53 | *.egg-info/
54 | .installed.cfg
55 | *.egg
56 |
57 | # PyInstaller
58 | # Usually these files are written by a python script from a template
59 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
60 | *.manifest
61 | *.spec
62 |
63 | # Installer logs
64 | pip-log.txt
65 | pip-delete-this-directory.txt
66 |
67 | # Unit test / coverage reports
68 | htmlcov/
69 | .tox/
70 | .coverage
71 | .coverage.*
72 | .cache
73 | nosetests.xml
74 | coverage.xml
75 | *,cover
76 | .hypothesis/
77 |
78 | # Translations
79 | *.mo
80 | *.pot
81 |
82 | # Django stuff:
83 | *.log
84 | local_settings.py
85 |
86 | # Flask stuff:
87 | instance/
88 | .webassets-cache
89 |
90 | # Scrapy stuff:
91 | .scrapy
92 |
93 | # Sphinx documentation
94 | docs/_build/
95 |
96 | # PyBuilder
97 | target/
98 |
99 | # IPython Notebook
100 | .ipynb_checkpoints
101 |
102 | # pyenv
103 | .python-version
104 |
105 | # celery beat schedule file
106 | celerybeat-schedule
107 |
108 | # dotenv
109 | .env
110 |
111 | # virtualenv
112 | venv/
113 | ENV/
114 |
115 | # Spyder project settings
116 | .spyderproject
117 |
118 | # Rope project settings
119 | .ropeproject
120 |
121 | database/
122 | media/
123 | whoosh_index/
124 | .idea/
125 | *.sqlite3
126 | fabfile.py
127 |
128 |
129 |
130 | #--------------------------------
131 |
132 |
133 | #java
134 | *.class
135 |
136 | # Package Files #
137 | *.jar
138 | *.war
139 | *.ear
140 |
141 | # Mobile Tools for Java (J2ME)
142 | .mtj.tmp/
143 |
144 |
145 | # My configurations:
146 | db.ini
147 | deploy_key_rsa
148 |
149 | # Package Files #
150 | *.jar
151 | *.war
152 | *.ear
153 |
154 | #phpstorm
155 | *.idea
156 |
157 | #过滤数据库文件、sln解决方案文件、配置文件
158 | *.mdb
159 | *.ldb
160 | *.sln
161 | *.config
162 |
163 | #other
164 | data/error/
165 | .idea*/
166 | .idea/
167 | *.htm
168 |
169 | # 忽略名称中末尾为ignore的文件夹
170 | *ignore/
171 | *ignore*/
172 |
173 |
174 |
175 |
176 | # Logs
177 | logs
178 | *.log
179 | npm-debug.log*
180 | # Runtime data
181 | pids
182 | *.pid
183 | *.seed
184 | *.pid.lock
185 | # Directory for instrumented libs generated by jscoverage/JSCover
186 | lib-cov
187 | # Coverage directory used by tools like istanbul
188 | coverage
189 | # nyc test coverage
190 | .nyc_output
191 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
192 | .grunt
193 | # Bower dependency directory (https://bower.io/)
194 | bower_components
195 | # node-waf configuration
196 | .lock-wscript
197 | # Compiled binary addons (http://nodejs.org/api/addons.html)
198 | build/Release
199 | # Dependency directories
200 | node_modules/
201 | jspm_packages/
202 | # Typescript v1 declaration files
203 | typings/
204 | # Optional npm cache directory
205 | .npm
206 | # Optional eslint cache
207 | .eslintcache
208 | # Optional REPL history
209 | .node_repl_history
210 | # Output of 'npm pack'
211 | *.tgz
212 | # Yarn Integrity file
213 | .yarn-integrity
214 | # dotenv environment variables file
215 | .env
216 | .vscode
217 | # ignore sh
218 | sh/
219 | # ignore test sdk.config.json
220 | sdk.config.json
221 | # ignore local config
222 | server/config.local.js
223 |
224 |
225 | #-------android
226 | # Built application files
227 | #*.apk
228 | *.ap_
229 |
230 | # Files for the ART/Dalvik VM
231 | *.dex
232 |
233 | # Java class files
234 | *.class
235 |
236 | # Generated files
237 | bin/
238 | gen/
239 | out/
240 |
241 | # Gradle files
242 | .gradle/
243 | build/
244 |
245 | # Local configuration file (sdk path, etc)
246 | local.properties
247 |
248 | # Proguard folder generated by Eclipse
249 | proguard/
250 |
251 | # Log Files
252 | *.log
253 |
254 | # Android Studio Navigation editor temp files
255 | .navigation/
256 |
257 | # Android Studio captures folder
258 | captures/
259 |
260 | # Intellij
261 | *.iml
262 | .idea/
263 |
264 | # Keystore files
265 | *.jks
266 |
267 | # External native build folder generated in Android Studio 2.2 and later
268 | .externalNativeBuild
269 |
270 | # DS_Store files
271 | *.DS_Store
272 |
273 | .MWebMetaData/
274 | .sync/
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'kotlin-android'
4 | id 'kotlin-kapt'
5 | id 'kotlin-parcelize'
6 | }
7 |
8 | android {
9 | compileSdkVersion 32
10 |
11 | defaultConfig {
12 | applicationId "com.hi.dhl.demo.binding"
13 | minSdkVersion 24
14 | targetSdkVersion 32
15 | versionCode 1
16 | versionName "1.0"
17 |
18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19 | }
20 |
21 | buildTypes {
22 | release {
23 | minifyEnabled true
24 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
25 | }
26 | }
27 |
28 | compileOptions {
29 | sourceCompatibility JavaVersion.VERSION_11
30 | targetCompatibility JavaVersion.VERSION_11
31 | }
32 |
33 | kotlinOptions {
34 | jvmTarget = JavaVersion.VERSION_11.toString()
35 | }
36 |
37 | buildFeatures {
38 | dataBinding = true
39 | viewBinding = true
40 | }
41 | }
42 |
43 | dependencies {
44 | ext.fragment = "1.2.5"
45 | ext.koin_version = "2.2.1"
46 | ext.nav_version = "2.3.2"
47 | ext.constraintlayout = "2.0.4"
48 | ext.material = "1.2.1"
49 | ext.appcompat = "1.2.0"
50 | ext.ktx = "1.3.2"
51 | ext.remote = true
52 |
53 | implementation "androidx.core:core-ktx:${ktx}"
54 | implementation "androidx.appcompat:appcompat:${appcompat}"
55 | implementation "com.google.android.material:material:${material}"
56 | implementation "androidx.constraintlayout:constraintlayout:${constraintlayout}"
57 |
58 | implementation "org.koin:koin-androidx-viewmodel:${koin_version}"
59 | implementation "io.coil-kt:coil:1.1.0"
60 |
61 | implementation"androidx.navigation:navigation-fragment-ktx:$nav_version"
62 | implementation"androidx.navigation:navigation-ui-ktx:$nav_version"
63 |
64 | implementation 'androidx.viewpager2:viewpager2:1.0.0-beta02'
65 |
66 | if (remote) {
67 | implementation 'com.hi-dhl:binding:1.2.0'
68 | } else {
69 | implementation project(':binding')
70 | }
71 | implementation "com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4"
72 | testImplementation 'junit:junit:4.+'
73 | androidTestImplementation 'androidx.test.ext:junit:1.1.2'
74 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
75 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
23 | -keepclassmembers class ** implements androidx.viewbinding.ViewBinding {
24 | public static ** bind(***);
25 | public static ** inflate(***);
26 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/hi/dhl/demo/binding/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.hi.dhl.demo.binding", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
13 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
27 |
28 |
31 |
32 |
35 |
36 |
39 |
40 |
43 |
44 |
47 |
48 |
51 |
52 |
55 |
56 |
59 |
60 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/App.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding
2 |
3 | import android.app.Application
4 | import com.hi.dhl.demo.binding.di.appModules
5 | import org.koin.android.ext.koin.androidContext
6 | import org.koin.android.logger.AndroidLogger
7 | import org.koin.core.context.loadKoinModules
8 | import org.koin.core.context.startKoin
9 | import org.koin.core.logger.Level
10 |
11 | /**
12 | *
13 | * author: dhl
14 | * date : 2020/12/12
15 | * desc :
16 | *
17 | */
18 | class App : Application() {
19 | override fun onCreate() {
20 | super.onCreate()
21 | startKoin {
22 | AndroidLogger(Level.DEBUG)
23 | androidContext(this@App)
24 | loadKoinModules(appModules)
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding
2 |
3 | import android.os.Bundle
4 | import android.view.View
5 | import androidx.appcompat.app.AppCompatActivity
6 | import com.hi.dhl.binding.viewbind
7 | import com.hi.dhl.demo.binding.databind.DatBindActivity
8 | import com.hi.dhl.demo.binding.databinding.ActivityMainBinding
9 | import com.hi.dhl.demo.binding.viewbind.ViewBindActivity
10 |
11 |
12 | class MainActivity : AppCompatActivity(), View.OnClickListener {
13 |
14 | private val binding: ActivityMainBinding by viewbind()
15 |
16 | override fun onCreate(savedInstanceState: Bundle?) {
17 | super.onCreate(savedInstanceState)
18 |
19 | getViews().forEach {
20 | it.setOnClickListener(this)
21 | }
22 |
23 | }
24 |
25 | override fun onClick(v: View) {
26 | with(binding) {
27 | when (v) {
28 | btnDataBind -> DatBindActivity.startActivity(this@MainActivity)
29 | btnViewBind -> ViewBindActivity.startActivity(this@MainActivity)
30 | }
31 | }
32 | }
33 |
34 | private fun getViews() = with(binding) {
35 | arrayListOf(btnDataBind, btnViewBind)
36 | }
37 |
38 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/MainViewModel.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding
2 |
3 | import android.os.Parcelable
4 | import androidx.databinding.BaseObservable
5 | import androidx.databinding.Bindable
6 | import androidx.databinding.ObservableField
7 | import androidx.lifecycle.LiveData
8 | import androidx.lifecycle.MutableLiveData
9 | import androidx.lifecycle.ViewModel
10 | import java.util.*
11 | import kotlinx.android.parcel.Parcelize
12 |
13 | /**
14 | *
15 | * author: dhl
16 | * date : 2020/12/12
17 | * desc :
18 | *
19 | */
20 |
21 | class MainViewModel : ViewModel() {
22 | val inputnNumber = ObservableField()
23 |
24 | private val _imgLiveData = MutableLiveData()
25 | val imgLiveData: LiveData = _imgLiveData
26 |
27 | private val _userLiveData = MutableLiveData()
28 | val userLiveData: LiveData = _userLiveData
29 |
30 | /**
31 | * 将 inputnNumber 和 AppCompatTextView 通过 android:text="@={mainViewModel.inputnNumber}" 进行双向绑定,
32 | * 当 inputnNumber 变更的时候,AppCompatTextView 也会跟着一起变化
33 | */
34 | fun generateTimber() {
35 | val timer = Timer()
36 | timer.schedule(
37 | object : TimerTask() {
38 | override fun run() {
39 | inputnNumber.set("ObservableField 双向绑定示例 \n Time: " + System.currentTimeMillis())
40 | }
41 | },
42 | 0, 500
43 | )
44 | }
45 |
46 | fun bindImage(url: String) {
47 | _imgLiveData.value = url
48 | }
49 |
50 | fun bindUser() {
51 | val user = User(name = "dhl", account = "公众号:ByteCode")
52 | _userLiveData.value = user
53 | }
54 |
55 | fun bindAccount(): Account {
56 | val account = Account()
57 | account.name = "i am BaseObservable"
58 | return account
59 | }
60 |
61 | }
62 |
63 | @Parcelize
64 | data class User(val name: String, val account: String) : Parcelable
65 |
66 | class Account : BaseObservable() {
67 |
68 | @get:Bindable
69 | var name: String = ""
70 | set(value) {
71 | field = value
72 | notifyPropertyChanged(BR.name)
73 | }
74 |
75 | @get:Bindable
76 | var avator: String = ""
77 | set(avator) {
78 | field = avator
79 | notifyPropertyChanged(BR.avator)
80 | }
81 |
82 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/brvah/BRVAHActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.brvah
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import android.view.LayoutInflater
7 | import android.view.View
8 | import android.view.ViewGroup
9 | import androidx.annotation.LayoutRes
10 | import androidx.appcompat.app.AppCompatActivity
11 | import com.chad.library.adapter.base.BaseQuickAdapter
12 | import com.chad.library.adapter.base.viewholder.BaseViewHolder
13 | import com.hi.dhl.binding.viewbind
14 | import com.hi.dhl.demo.binding.R
15 | import com.hi.dhl.demo.binding.databind.list.ListViewModel
16 | import com.hi.dhl.demo.binding.databind.list.Product
17 | import com.hi.dhl.demo.binding.databinding.ActivityBrvahBinding
18 | import com.hi.dhl.demo.binding.databinding.RecycleItemProductBinding
19 | import org.koin.androidx.viewmodel.ext.android.viewModel
20 |
21 | /**
22 | *
23 | * author: dhl
24 | * date : 2021/5/15
25 | * desc :
26 | *
27 | * 因有小伙伴反馈在开源库 BRVAH 中无法使用,所以单独写了一个如何在 BRVAH 中使用的例子
28 | * issue: https://github.com/hi-dhl/Binding/issues/24
29 | *
30 | *
31 | */
32 | class BRVAHActivity : AppCompatActivity() {
33 |
34 | private val binding: ActivityBrvahBinding by viewbind()
35 | private val viewModel: ListViewModel by viewModel()
36 | private val adapter: BRVAHAdapter by lazy { BRVAHAdapter() }
37 |
38 | override fun onCreate(savedInstanceState: Bundle?) {
39 | super.onCreate(savedInstanceState)
40 | with(binding) {
41 | recyclerView.adapter = adapter
42 | }
43 |
44 | viewModel.liveData.observe(this@BRVAHActivity) {
45 | adapter.addData(it)
46 | adapter.notifyDataSetChanged()
47 | }
48 | }
49 |
50 | companion object {
51 | fun startActivity(activity: Activity) {
52 | activity.startActivity(Intent(activity, BRVAHActivity::class.java))
53 | }
54 | }
55 | }
56 |
57 | class BRVAHAdapter : BaseQuickAdapter(0) {
58 | override fun convert(holder: BRVAHViewHolder, item: Product) {
59 | holder.bindData(item)
60 | }
61 |
62 | override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): BRVAHViewHolder {
63 | val view = inflateView(parent, viewType)
64 | return BRVAHViewHolder(view)
65 | }
66 |
67 | private fun inflateView(viewGroup: ViewGroup, @LayoutRes viewType: Int): View {
68 | val layoutInflater = LayoutInflater.from(viewGroup.context)
69 | return layoutInflater.inflate(viewType, viewGroup, false)
70 | }
71 |
72 | override fun getDefItemViewType(position: Int): Int = R.layout.recycle_item_product
73 | }
74 |
75 |
76 | class BRVAHViewHolder(view: View) : BaseViewHolder(view) {
77 | val binding: RecycleItemProductBinding by viewbind()
78 |
79 | fun bindData(data: Product) {
80 | binding.apply {
81 | product = data
82 | executePendingBindings()
83 | }
84 | }
85 |
86 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/databind/BindViewStubActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.databind
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import androidx.appcompat.app.AppCompatActivity
7 | import androidx.databinding.DataBindingUtil
8 | import androidx.databinding.ViewStubProxy
9 | import com.hi.dhl.binding.databind
10 | import com.hi.dhl.demo.binding.R
11 | import com.hi.dhl.demo.binding.databinding.ActivityViewStubBinding
12 | import com.hi.dhl.demo.binding.databinding.ViewStubBinding
13 | import com.hi.dhl.demo.binding.databinding.ViewStubDataBinding
14 |
15 | /**
16 | *
17 | * author: dhl
18 | * date : 2020/12/31
19 | * desc :
20 | *
21 | */
22 | class BindViewStubActivity : AppCompatActivity() {
23 |
24 | val binding: ActivityViewStubBinding by databind(R.layout.activity_view_stub)
25 |
26 | override fun onCreate(savedInstanceState: Bundle?) {
27 | super.onCreate(savedInstanceState)
28 |
29 | binding.viewStub.setOnInflateListener { stub, inflated ->
30 |
31 | // ViewBinding
32 | val viewStub: ViewStubBinding = ViewStubBinding.bind(inflated)
33 | viewStub.tvTitle.setText("使用 ViewStub 加载 ViewBinding 布局")
34 |
35 | }
36 |
37 | binding.viewStubData.setOnInflateListener { stub, inflated ->
38 |
39 | // DataBinding
40 | val dataViewStub: ViewStubDataBinding = DataBindingUtil.bind(inflated)!!
41 | dataViewStub.tvTitle.setText("使用 ViewStub 加载 DataBinding 布局")
42 | }
43 |
44 | inflateLayout(binding.viewStub)
45 | inflateLayout(binding.viewStubData)
46 | }
47 |
48 | private fun inflateLayout(viewStubProxy: ViewStubProxy) {
49 | if (!viewStubProxy.isInflated) {
50 | viewStubProxy.viewStub!!.inflate()
51 | }
52 | }
53 |
54 | companion object {
55 |
56 | fun startActivtiy(activity: Activity) {
57 | activity.startActivity(Intent(activity, BindViewStubActivity::class.java))
58 | }
59 |
60 | }
61 |
62 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/databind/BindingAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.databind
2 |
3 | import android.widget.ImageView
4 | import android.widget.TextView
5 | import androidx.databinding.BindingAdapter
6 | import androidx.recyclerview.widget.RecyclerView
7 | import coil.load
8 | import com.hi.dhl.demo.binding.R
9 | import com.hi.dhl.demo.binding.User
10 | import com.hi.dhl.demo.binding.databind.list.Product
11 | import com.hi.dhl.demo.binding.databind.list.ProductAdapter
12 |
13 | /**
14 | *
15 | * author: dhl
16 | * date : 2020/12/13
17 | * desc :
18 | *
19 | */
20 |
21 | @BindingAdapter("bindingImage")
22 | fun bindingImage(imageView: ImageView, url: String?) {
23 | imageView.load(url) {
24 | crossfade(true)
25 | placeholder(R.mipmap.ic_launcher_round)
26 | }
27 | }
28 |
29 | @BindingAdapter("bindingLiveData")
30 | fun bindingLiveData(textView: TextView, user: User?) {
31 | user?.apply {
32 | textView.setText("@BindingAdapter + LiveData + parcelize 示例 \n")
33 | textView.append("${name} - ${account}")
34 | }
35 |
36 | }
37 |
38 | @BindingAdapter("bindList")
39 | fun bindAdapterList(recyclerView: RecyclerView, data: List?) {
40 | val adapter = recyclerView.adapter as? ProductAdapter
41 | ?: throw RuntimeException("adapter must bu not null")
42 | data?.let {
43 | adapter.submitList(it)
44 | adapter.notifyDataSetChanged()
45 | }
46 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/databind/DatBindActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.databind
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import android.view.View
7 | import androidx.appcompat.app.AppCompatActivity
8 | import com.hi.dhl.binding.databind
9 | import com.hi.dhl.demo.binding.MainViewModel
10 | import com.hi.dhl.demo.binding.R
11 | import com.hi.dhl.demo.binding.databind.list.DataBindRecycleActivity
12 | import com.hi.dhl.demo.binding.databinding.ActivityDataBindBinding
13 | import com.hi.dhl.demo.binding.navigation.NavigationActivity
14 | import org.koin.androidx.viewmodel.ext.android.viewModel
15 |
16 | /**
17 | *
18 | * author: dhl
19 | * date : 2020/12/13
20 | * desc :
21 | *
22 | */
23 | class DatBindActivity : AppCompatActivity(), View.OnClickListener {
24 |
25 | val viewModel: MainViewModel by viewModel()
26 |
27 | val binding: ActivityDataBindBinding by databind(R.layout.activity_data_bind)
28 |
29 | // 第二种用法 或者通过下列方式绑定数据, 所有使用 DataBinding 绑定的地方都可以使用这种方式
30 | // val binding: ActivityDataBindBinding by databind(R.layout.activity_data_bind) {
31 | // val account = Account()
32 | // account.name = "test"
33 | // this.account = account
34 | // }
35 |
36 |
37 | override fun onCreate(savedInstanceState: Bundle?) {
38 | super.onCreate(savedInstanceState)
39 | with(binding) {
40 | account = viewModel.bindAccount()
41 | mainViewModel = viewModel
42 | }
43 |
44 | getViews().forEach {
45 | it.setOnClickListener(this)
46 | }
47 | }
48 |
49 |
50 | override fun onClick(v: View) {
51 | val lifecycle = this.lifecycle
52 | with(binding) {
53 | when (v) {
54 | btnBindRandom -> viewModel.generateTimber()
55 | btnBindAdapter -> viewModel.bindUser()
56 | btnDialog -> DataBindDialog(this@DatBindActivity, lifecycle).show()
57 | btnRecycle -> DataBindRecycleActivity.startActivity(this@DatBindActivity)
58 | btnNavigation -> NavigationActivity.startActivity(this@DatBindActivity)
59 | btnStub -> BindViewStubActivity.startActivtiy(this@DatBindActivity)
60 | btnInclude -> DataBindIncludeActivity.startActivtiy(this@DatBindActivity)
61 | }
62 | }
63 | }
64 |
65 | private fun getViews() = with(binding) {
66 | arrayListOf(
67 | btnBindRandom,
68 | btnBindAdapter,
69 | btnDialog,
70 | btnRecycle,
71 | btnNavigation,
72 | btnStub,
73 | btnInclude
74 | )
75 | }
76 |
77 | companion object {
78 | fun startActivity(activity: Activity) {
79 | activity.startActivity(Intent(activity, DatBindActivity::class.java))
80 | }
81 | }
82 |
83 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/databind/DataBindCustomView.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.databind
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 | import android.widget.LinearLayout
6 | import com.hi.dhl.binding.databind
7 | import com.hi.dhl.demo.binding.R
8 | import com.hi.dhl.demo.binding.databinding.LayoutViewCustomDataBinding
9 |
10 | /**
11 | *
12 | * author: dhl
13 | * date : 2021/1/14
14 | * desc :
15 | *
16 | */
17 |
18 | class DataBindCustomView @JvmOverloads constructor(
19 | context: Context,
20 | attr: AttributeSet? = null,
21 | defStyleAttr: Int = 0,
22 | ) : LinearLayout(context, attr, defStyleAttr) {
23 |
24 | val binding: LayoutViewCustomDataBinding by databind(R.layout.layout_view_custom_data)
25 |
26 | init {
27 | with(binding) {
28 | result.setText("这是 ViewGroup 通过 DataBinding 绑定")
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/databind/DataBindDialog.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.databind
2 |
3 | import android.app.Dialog
4 | import android.content.Context
5 | import android.os.Bundle
6 | import android.view.Window
7 | import androidx.lifecycle.Lifecycle
8 | import com.hi.dhl.binding.databind
9 | import com.hi.dhl.demo.binding.R
10 | import com.hi.dhl.demo.binding.databinding.DialogDataBindingBinding
11 |
12 | /**
13 | *
14 | * author: dhl
15 | * date : 2020/12/12
16 | * desc :
17 | *
18 | */
19 | class DataBindDialog(context: Context, lifecycle: Lifecycle) :
20 | Dialog(context, R.style.AppDialog) {
21 |
22 | val binding: DialogDataBindingBinding by databind(R.layout.dialog_data_binding, lifecycle)
23 |
24 | override fun onCreate(savedInstanceState: Bundle?) {
25 | super.onCreate(savedInstanceState)
26 | requestWindowFeature(Window.FEATURE_NO_TITLE)
27 | binding.apply { result.setText("DataBindDialog") }
28 | }
29 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/databind/DataBindIncludeActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.databind
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import androidx.appcompat.app.AppCompatActivity
7 | import com.hi.dhl.binding.databind
8 | import com.hi.dhl.demo.binding.MainViewModel
9 | import com.hi.dhl.demo.binding.R
10 | import com.hi.dhl.demo.binding.User
11 | import com.hi.dhl.demo.binding.databinding.ActivityIncludeBinding
12 | import com.hi.dhl.demo.binding.databinding.LayoutMergeItemBinding
13 | import org.koin.androidx.viewmodel.ext.android.viewModel
14 |
15 | /**
16 | *
17 | * author: dhl
18 | * date : 2020/12/31
19 | * desc :
20 | *
21 | */
22 | class DataBindIncludeActivity : AppCompatActivity() {
23 |
24 | val viewModel: MainViewModel by viewModel()
25 | private val binding: ActivityIncludeBinding by databind(R.layout.activity_include)
26 |
27 | override fun onCreate(savedInstanceState: Bundle?) {
28 | super.onCreate(savedInstanceState)
29 | with(binding) {
30 | user = User(name = "数据绑定:dhl", account = "数据绑定:公众号:ByteCode")
31 |
32 | // 布局是 ViewBdining
33 | include.includeTvTitle.setText("使用 include 布局中的控件, 不包含 merge")
34 |
35 | // 布局是 ViewBdining
36 | LayoutMergeItemBinding.bind(binding.root)
37 | .mergeTvTitle.setText("使用 include 布局中的控件, 包含 merge")
38 |
39 | // 布局是 DataBidning 使用 include 布局中的控件, 不包含 merge
40 | includeData.includeTvTitle.setText("通过代码设置 include layout 的控件")
41 | // 布局是 DataBidning,使用 include 布局中的控件, 包含 merge
42 | includeDataMerge.mergeTvTitle.setText("通过代码设置 merge layout 的控件")
43 | }
44 | }
45 |
46 | companion object {
47 |
48 | fun startActivtiy(activity: Activity) {
49 | activity.startActivity(Intent(activity, DataBindIncludeActivity::class.java))
50 | }
51 |
52 | }
53 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/databind/list/DataBindRecycleActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.databind.list
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import androidx.appcompat.app.AppCompatActivity
7 | import com.hi.dhl.binding.databind
8 | import com.hi.dhl.binding.viewbind
9 | import com.hi.dhl.demo.binding.R
10 | import com.hi.dhl.demo.binding.databinding.ActivityDataBindRecycleBinding
11 |
12 | /**
13 | *
14 | * author: dhl
15 | * date : 2020/12/17
16 | * desc :
17 | *
18 | */
19 | class DataBindRecycleActivity : AppCompatActivity() {
20 |
21 | private val binding: ActivityDataBindRecycleBinding by viewbind()
22 |
23 | override fun onCreate(savedInstanceState: Bundle?) {
24 | super.onCreate(savedInstanceState)
25 | binding.apply { }
26 | // databinding 在 Fragment 中使用
27 | addFragment()
28 | }
29 |
30 | private fun addFragment() {
31 | supportFragmentManager.beginTransaction()
32 | .replace(R.id.container, DataBindRecycleFragment())
33 | .commit()
34 | }
35 |
36 | companion object {
37 | fun startActivity(activity: Activity) {
38 | activity.startActivity(Intent(activity, DataBindRecycleActivity::class.java))
39 | }
40 | }
41 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/databind/list/DataBindRecycleFragment.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.databind.list
2 |
3 | import android.os.Bundle
4 | import android.view.View
5 | import androidx.fragment.app.Fragment
6 | import com.hi.dhl.binding.databind
7 | import com.hi.dhl.demo.binding.R
8 | import com.hi.dhl.demo.binding.databinding.FragmentDataBindRecycleBinding
9 | import org.koin.androidx.viewmodel.ext.android.viewModel
10 |
11 | /**
12 | *
13 | * author: dhl
14 | * date : 2020/12/15
15 | * desc :
16 | *
17 | */
18 | class DataBindRecycleFragment : Fragment(R.layout.fragment_data_bind_recycle) {
19 |
20 | private val listViewModel: ListViewModel by viewModel()
21 |
22 | private val binding: FragmentDataBindRecycleBinding by databind()
23 |
24 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
25 | super.onViewCreated(view, savedInstanceState)
26 | binding.apply {
27 | viewModel = listViewModel
28 | adapter = ProductAdapter()
29 | }
30 | }
31 |
32 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/databind/list/ListViewModel.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.databind.list
2 |
3 | import androidx.lifecycle.LiveData
4 | import androidx.lifecycle.MutableLiveData
5 | import androidx.lifecycle.ViewModel
6 |
7 | /**
8 | *
9 | * author: dhl
10 | * date : 2020/12/17
11 | * desc :
12 | *
13 | */
14 | class ListViewModel : ViewModel() {
15 |
16 | private val _liveData = MutableLiveData>()
17 | val liveData: LiveData> = _liveData
18 |
19 | init {
20 | val data = mutableListOf()
21 | (1..15).forEach {
22 | val id = it
23 | val name = "公众号:ByteCode,这是通过 DataBinding 绑定的 item"
24 | data.add(Product(id, name))
25 | }
26 | _liveData.value = data
27 | }
28 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/databind/list/ProductAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.databind.list
2 |
3 | import android.view.LayoutInflater
4 | import android.view.View
5 | import android.view.ViewGroup
6 | import androidx.annotation.LayoutRes
7 | import androidx.recyclerview.widget.DiffUtil
8 | import androidx.recyclerview.widget.ListAdapter
9 | import androidx.recyclerview.widget.RecyclerView
10 | import com.hi.dhl.binding.databind
11 | import com.hi.dhl.binding.viewbind
12 | import com.hi.dhl.demo.binding.R
13 | import com.hi.dhl.demo.binding.databinding.RecycleItemProductBinding
14 | import com.hi.dhl.demo.binding.databinding.RecycleItemProductFooterBinding
15 | import com.hi.dhl.demo.binding.databinding.RecycleItemProductHeaderBinding
16 |
17 | /**
18 | *
19 | * author: dhl
20 | * date : 2020/12/17
21 | * desc :
22 | *
23 | */
24 |
25 | /**
26 | * 这是 DataBinging 和 ViewBinding在 Adapter 中的使用例子,
27 | * 通过扩展 RecyclerView.ViewHolder 来使用 DataBinding 和 ViewBinding,
28 | * 即所有与 RecyclerView.ViewHolder 相关的 Adapter 都可以使用(ListAdapter、PagingDataAdapter、RecyclerView.Adapter 等等)
29 | */
30 | class ProductAdapter : ListAdapter(Product.CALLBACK) {
31 |
32 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
33 | val view = inflateView(parent, viewType)
34 | return when (viewType) {
35 | R.layout.recycle_item_product_header -> ProductViewHolderHeader(view)
36 | R.layout.recycle_item_product_footer -> ProductViewHolderFooter(view)
37 | else -> ProductViewHolder(view)
38 | }
39 | }
40 |
41 | override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
42 | val data = getItem(position)
43 | if (holder is ProductViewHolder) {
44 | holder.bindData(data, position)
45 | }
46 |
47 | when {
48 | holder is ProductViewHolderHeader -> holder.bindData(data, position)
49 | holder is ProductViewHolderFooter -> holder.bindData(null, position)
50 | holder is ProductViewHolder -> {
51 | val data = getItem(position)
52 | holder.bindData(data, position)
53 | }
54 | }
55 |
56 | }
57 |
58 | override fun getItemViewType(position: Int): Int = when (position) {
59 | 0 -> R.layout.recycle_item_product_header
60 | itemCount - 1 -> R.layout.recycle_item_product_footer
61 | else -> R.layout.recycle_item_product
62 | }
63 |
64 | private fun inflateView(viewGroup: ViewGroup, @LayoutRes viewType: Int): View {
65 | val layoutInflater = LayoutInflater.from(viewGroup.context)
66 | return layoutInflater.inflate(viewType, viewGroup, false)
67 | }
68 | }
69 |
70 | class ProductViewHolder(view: View) : RecyclerView.ViewHolder(view) {
71 |
72 | val binding: RecycleItemProductBinding by databind()
73 |
74 | fun bindData(data: Product?, position: Int) {
75 | binding.apply {
76 | product = data
77 | executePendingBindings()
78 | }
79 | }
80 | }
81 |
82 |
83 | class ProductViewHolderHeader(view: View) : RecyclerView.ViewHolder(view) {
84 |
85 | val binding: RecycleItemProductHeaderBinding by viewbind()
86 |
87 | fun bindData(data: Product?, position: Int) {
88 | binding.apply {
89 | name.text = "通过 ViewBinding 绑定的 head"
90 | }
91 | }
92 | }
93 |
94 | class ProductViewHolderFooter(view: View) : RecyclerView.ViewHolder(view) {
95 |
96 | val binding: RecycleItemProductFooterBinding by viewbind()
97 |
98 | fun bindData(data: Product?, position: Int) {
99 | binding.apply {
100 | name.text = "通过 ViewBinding 绑定的 footer"
101 | }
102 | }
103 | }
104 |
105 | data class Product(val id: Int, val name: String) {
106 | companion object {
107 | val CALLBACK: DiffUtil.ItemCallback = object : DiffUtil.ItemCallback() {
108 | // 判断两个Objects 是否代表同一个item对象, 一般使用Bean的id比较
109 | override fun areItemsTheSame(oldItem: Product, newItem: Product): Boolean =
110 | oldItem.id == newItem.id
111 |
112 | // 判断两个Objects 是否有相同的内容。
113 | override fun areContentsTheSame(oldItem: Product, newItem: Product): Boolean = true
114 | }
115 | }
116 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/di/AppModule.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.di
2 |
3 | import com.hi.dhl.demo.binding.MainViewModel
4 | import com.hi.dhl.demo.binding.databind.list.ListViewModel
5 | import org.koin.androidx.viewmodel.dsl.viewModel
6 | import org.koin.dsl.module
7 |
8 | /**
9 | *
10 | * author: dhl
11 | * date : 2020/4/19
12 | * desc :
13 | *
14 | */
15 | val viewModelsModule = module {
16 | viewModel { MainViewModel() }
17 | viewModel { ListViewModel() }
18 | }
19 |
20 | val appModules = listOf(viewModelsModule)
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/navigation/FragmentNav1.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.navigation
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import androidx.navigation.Navigation
9 | import com.hi.dhl.binding.viewbind
10 | import com.hi.dhl.demo.binding.R
11 | import com.hi.dhl.demo.binding.databinding.FragmentNav1Binding
12 |
13 | /**
14 | *
15 | * author: dhl
16 | * date : 2020/12/21
17 | * desc :
18 | *
19 | */
20 | class FragmentNav1 : Fragment() {
21 |
22 | val binding: FragmentNav1Binding by viewbind()
23 |
24 | override fun onCreateView(
25 | inflater: LayoutInflater, container: ViewGroup?,
26 | savedInstanceState: Bundle?
27 | ): View {
28 | return binding.root
29 | }
30 |
31 |
32 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
33 | super.onViewCreated(view, savedInstanceState)
34 | binding.btnJump.setOnClickListener {
35 | Navigation.findNavController(it).navigate(R.id.action_fragmentNav1_to_fragmentNav2)
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/navigation/FragmentNav2.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.navigation
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import androidx.navigation.Navigation
9 | import com.hi.dhl.binding.databind
10 | import com.hi.dhl.demo.binding.MainViewModel
11 | import com.hi.dhl.demo.binding.R
12 | import com.hi.dhl.demo.binding.databinding.FragmentNav2Binding
13 | import org.koin.androidx.viewmodel.ext.android.viewModel
14 |
15 | /**
16 | *
17 | * author: dhl
18 | * date : 2020/12/21
19 | * desc :
20 | *
21 | */
22 | class FragmentNav2 : Fragment() {
23 |
24 | val binding: FragmentNav2Binding by databind()
25 | val viewModel: MainViewModel by viewModel()
26 |
27 | override fun onCreateView(
28 | inflater: LayoutInflater, container: ViewGroup?,
29 | savedInstanceState: Bundle?
30 | ): View {
31 |
32 | return binding.apply {
33 | lifecycleOwner = this@FragmentNav2
34 | mainViewModel = viewModel
35 | }.root
36 | }
37 |
38 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
39 | super.onViewCreated(view, savedInstanceState)
40 |
41 | with(binding) {
42 | mainViewModel = viewModel
43 | viewModel.generateTimber()
44 | binding.btnJump.setOnClickListener {
45 | Navigation.findNavController(it).navigate(R.id.action_fragmentNav2_to_fragmentNav3)
46 | }
47 | }
48 |
49 | }
50 |
51 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/navigation/FragmentNav3.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.navigation
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import androidx.navigation.Navigation
9 | import com.hi.dhl.binding.viewbind
10 | import com.hi.dhl.demo.binding.R
11 | import com.hi.dhl.demo.binding.databinding.FragmentNav3Binding
12 |
13 | /**
14 | *
15 | * author: dhl
16 | * date : 2020/12/21
17 | * desc :
18 | *
19 | */
20 | class FragmentNav3 : Fragment(R.layout.fragment_nav_3) {
21 |
22 | val binding: FragmentNav3Binding by viewbind()
23 |
24 | // override fun onCreateView(
25 | // inflater: LayoutInflater, container: ViewGroup?,
26 | // savedInstanceState: Bundle?
27 | // ): View {
28 | // return binding.root
29 | // }
30 |
31 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
32 | super.onViewCreated(view, savedInstanceState)
33 | binding.btnJump.setOnClickListener {
34 | Navigation.findNavController(it).navigate(R.id.action_fragmentNav3_to_fragmentNav1)
35 | }
36 | }
37 |
38 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/navigation/NavigationActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.navigation
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import androidx.appcompat.app.AppCompatActivity
7 | import com.hi.dhl.demo.binding.R
8 |
9 | /**
10 | *
11 | * author: dhl
12 | * date : 2020/12/21
13 | * desc :
14 | *
15 | */
16 | class NavigationActivity : AppCompatActivity() {
17 | override fun onCreate(savedInstanceState: Bundle?) {
18 | super.onCreate(savedInstanceState)
19 | setContentView(R.layout.activity_navigation)
20 | }
21 |
22 | companion object {
23 | fun startActivity(activity: Activity) {
24 | activity.startActivity(Intent(activity, NavigationActivity::class.java))
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewbind/ViewBindActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewbind
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import androidx.appcompat.app.AppCompatActivity
7 | import com.hi.dhl.binding.viewbind
8 | import com.hi.dhl.demo.binding.R
9 | import com.hi.dhl.demo.binding.databinding.ActivityViewBindBinding
10 |
11 | /**
12 | *
13 | * author: dhl
14 | * date : 2020/12/13
15 | * desc :
16 | *
17 | */
18 | class ViewBindActivity : AppCompatActivity() {
19 | val binding: ActivityViewBindBinding by viewbind()
20 |
21 | override fun onCreate(savedInstanceState: Bundle?) {
22 | super.onCreate(savedInstanceState)
23 | binding.apply {
24 | addFragment()
25 | }
26 | }
27 |
28 | private fun addFragment() {
29 | supportFragmentManager.beginTransaction()
30 | .replace(R.id.container, ViewBindFragment())
31 | .commit()
32 | }
33 |
34 | companion object {
35 | fun startActivity(activity: Activity) {
36 | activity.startActivity(Intent(activity, ViewBindActivity::class.java))
37 | }
38 | }
39 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewbind/ViewBindCustomView.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewbind
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 | import android.widget.LinearLayout
6 | import com.hi.dhl.binding.viewbind
7 | import com.hi.dhl.demo.binding.databinding.LayoutMergeItemBinding
8 | import com.hi.dhl.demo.binding.databinding.LayoutViewCustomBinding
9 |
10 | /**
11 | *
12 | * author: dhl
13 | * date : 2021/1/14
14 | * desc :
15 | *
16 | */
17 |
18 | class ViewBindCustomView @JvmOverloads constructor(
19 | context: Context,
20 | attr: AttributeSet? = null,
21 | defStyleAttr: Int = 0,
22 | ) : LinearLayout(context, attr, defStyleAttr) {
23 |
24 | lateinit var onDialogClickListener: OnDialogClickListener
25 |
26 | // 当根布局为 merge 标签,使用此方法进行初始化
27 | val binding: LayoutViewCustomBinding by viewbind(this)
28 |
29 | // 当根布局是非 merge 标签,使用此方法进行初始化
30 | // val binding: LayoutViewCustomBinding by viewbind()
31 |
32 | init {
33 | with(binding) {
34 | result.setText("这是 ViewGroup 通过 ViewBinding 绑定")
35 | result.setOnClickListener {
36 | if (::onDialogClickListener.isInitialized) {
37 | onDialogClickListener.onClick()
38 | }
39 | }
40 |
41 | LayoutMergeItemBinding.bind(root)
42 | .mergeTvTitle.setText("在 ViewGroup 中使用 merge 标签")
43 | }
44 | }
45 |
46 | fun setDialogClickListener(onDialogClickListener: OnDialogClickListener) {
47 | this.onDialogClickListener = onDialogClickListener
48 | }
49 |
50 | interface OnDialogClickListener {
51 | fun onClick()
52 | }
53 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewbind/ViewBindDialog.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewbind
2 |
3 | import android.app.Dialog
4 | import android.content.Context
5 | import android.os.Bundle
6 | import android.util.Log
7 | import android.view.Window
8 | import androidx.lifecycle.Lifecycle
9 | import com.hi.dhl.binding.viewbind
10 | import com.hi.dhl.demo.binding.R
11 | import com.hi.dhl.demo.binding.databinding.DialogAppBinding
12 |
13 | /**
14 | *
15 | * author: dhl
16 | * date : 2020/12/12
17 | * desc :
18 | *
19 | */
20 | class ViewBindDialog(context: Context, lifecycle: Lifecycle) : Dialog(context, R.style.AppDialog) {
21 |
22 | val binding: DialogAppBinding by viewbind(lifecycle)
23 |
24 | override fun onCreate(savedInstanceState: Bundle?) {
25 | super.onCreate(savedInstanceState)
26 | requestWindowFeature(Window.FEATURE_NO_TITLE)
27 | binding.apply {
28 | result.setText("ViewBindDialog")
29 | result.setOnClickListener {
30 | Log.e(TAG, "绑定 OnClickListener")
31 | }
32 |
33 | customView.onDialogClickListener = object : ViewBindCustomView.OnDialogClickListener {
34 | override fun onClick() {
35 | Log.e(TAG, "绑定自定义的 OnDialogClickListener")
36 | }
37 |
38 | }
39 | }
40 | }
41 |
42 | companion object {
43 | private val TAG: String = "ViewBindDialog"
44 | }
45 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewbind/ViewBindFragment.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewbind
2 |
3 | import android.app.Activity
4 | import android.content.Context
5 | import android.os.Bundle
6 | import android.view.View
7 | import androidx.fragment.app.Fragment
8 | import com.hi.dhl.binding.viewbind
9 | import com.hi.dhl.demo.binding.R
10 | import com.hi.dhl.demo.binding.brvah.BRVAHActivity
11 | import com.hi.dhl.demo.binding.databind.BindViewStubActivity
12 | import com.hi.dhl.demo.binding.databind.list.DataBindRecycleActivity
13 | import com.hi.dhl.demo.binding.databinding.FragmentViewBindBinding
14 | import com.hi.dhl.demo.binding.navigation.NavigationActivity
15 | import com.hi.dhl.demo.binding.viewbind.nested.NestedActivity
16 | import com.hi.dhl.demo.binding.viewpager2.ViewPager2Activity
17 |
18 | /**
19 | *
20 | * author: dhl
21 | * date : 2020/12/12
22 | * desc :
23 | *
24 | */
25 | class ViewBindFragment : Fragment(R.layout.fragment_view_bind), View.OnClickListener {
26 |
27 |
28 | val binding: FragmentViewBindBinding by viewbind()
29 | var activity: Activity? = null
30 |
31 | override fun onAttach(context: Context) {
32 | super.onAttach(context)
33 | if (context is Activity) {
34 | activity = context
35 | }
36 | }
37 |
38 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
39 | super.onViewCreated(view, savedInstanceState)
40 |
41 | getViews().forEach {
42 | it.setOnClickListener(this)
43 | }
44 |
45 | }
46 |
47 | private fun getViews() = with(binding) {
48 | arrayListOf(
49 | btnDialog,
50 | btnRecycle,
51 | btnNavigation,
52 | btnDialog,
53 | btnStub,
54 | btnInclude,
55 | btnViewPager,
56 | btnBRVAH,
57 | btnFragment
58 | )
59 | }
60 |
61 | override fun onClick(v: View) {
62 |
63 | val lifecycle = this.lifecycle
64 | with(binding) {
65 | when (v) {
66 | btnDialog -> {
67 | this@ViewBindFragment.context?.let { ctx ->
68 | ViewBindDialog(ctx, lifecycle).show()
69 | }
70 | }
71 | btnRecycle -> DataBindRecycleActivity.startActivity(requireActivity())
72 | btnBRVAH -> BRVAHActivity.startActivity(requireActivity())
73 | btnNavigation -> NavigationActivity.startActivity(requireActivity())
74 | btnStub -> BindViewStubActivity.startActivtiy(requireActivity())
75 | btnInclude -> ViewBindIncludeActivity.startActivtiy(requireActivity())
76 | btnViewPager -> ViewPager2Activity.startActivity(requireActivity())
77 | btnFragment -> NestedActivity.startActivity(requireActivity())
78 | else -> {
79 | }
80 | }
81 | }
82 | }
83 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewbind/ViewBindIncludeActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewbind
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import androidx.appcompat.app.AppCompatActivity
7 | import com.hi.dhl.binding.viewbind
8 | import com.hi.dhl.demo.binding.databinding.ActivityIncludeViewBindBinding
9 | import com.hi.dhl.demo.binding.databinding.LayoutMergeItemBinding
10 |
11 | /**
12 | *
13 | * author: dhl
14 | * date : 2020/12/31
15 | * desc :
16 | *
17 | */
18 | class ViewBindIncludeActivity : AppCompatActivity() {
19 |
20 | val binding: ActivityIncludeViewBindBinding by viewbind()
21 |
22 | override fun onCreate(savedInstanceState: Bundle?) {
23 | super.onCreate(savedInstanceState)
24 |
25 | with(binding) {
26 | tvTitle.setText("直接使用布局中的控件")
27 |
28 | // include without merge
29 | include.includeTvTitle.setText("使用 include 布局中的控件, 不包含 merge")
30 |
31 | // include
32 | LayoutMergeItemBinding.bind(root)
33 | .mergeTvTitle.setText("使用 include 布局中的控件, 包含 merge")
34 | }
35 | }
36 |
37 | companion object {
38 |
39 | fun startActivtiy(activity: Activity) {
40 | activity.startActivity(Intent(activity, ViewBindIncludeActivity::class.java))
41 | }
42 |
43 | }
44 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewbind/nested/NestedActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewbind.nested
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import androidx.appcompat.app.AppCompatActivity
7 | import com.hi.dhl.binding.viewbind
8 | import com.hi.dhl.demo.binding.R
9 | import com.hi.dhl.demo.binding.databinding.ActivityNestedBinding
10 |
11 | /**
12 | *
13 | * author: dhl
14 | * date : 2020/12/13
15 | * desc :
16 | *
17 | */
18 | class NestedActivity : AppCompatActivity() {
19 | val binding: ActivityNestedBinding by viewbind()
20 |
21 | override fun onCreate(savedInstanceState: Bundle?) {
22 | super.onCreate(savedInstanceState)
23 | binding.apply {
24 | addFragment()
25 | }
26 | }
27 |
28 | private fun addFragment() {
29 | val fragment1 = NestedFragment1()
30 | fragment1.resId = R.drawable.avatar
31 |
32 | val fragment2 = NestedFragment1()
33 | fragment2.resId = R.drawable.logo
34 |
35 | supportFragmentManager.beginTransaction()
36 | .add(R.id.fragment1, fragment1)
37 | .add(R.id.fragment2, fragment2)
38 | .commit()
39 | }
40 |
41 | companion object {
42 | fun startActivity(activity: Activity) {
43 | activity.startActivity(Intent(activity, NestedActivity::class.java))
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewbind/nested/NestedFragment1.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewbind.nested
2 |
3 | import android.app.Activity
4 | import android.content.Context
5 | import android.os.Bundle
6 | import android.view.View
7 | import androidx.fragment.app.Fragment
8 | import com.hi.dhl.binding.viewbind
9 | import com.hi.dhl.demo.binding.R
10 | import com.hi.dhl.demo.binding.databinding.FragmentNested1Binding
11 |
12 | /**
13 | *
14 | * author: dhl
15 | * date : 2020/12/12
16 | * desc :
17 | *
18 | */
19 | class NestedFragment1 : Fragment(R.layout.fragment_nested1) {
20 |
21 |
22 | val binding: FragmentNested1Binding by viewbind()
23 | var activity: NestedActivity? = null
24 | var resId = R.drawable.avatar
25 |
26 | override fun onAttach(context: Context) {
27 | super.onAttach(context)
28 | if (context is NestedActivity) {
29 | activity = context
30 | }
31 | }
32 |
33 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
34 | super.onViewCreated(view, savedInstanceState)
35 |
36 | with(binding){
37 | image.setImageResource(resId)
38 | }
39 | }
40 |
41 |
42 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment1.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewpager2
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import androidx.viewpager2.adapter.FragmentStateAdapter
9 | import com.hi.dhl.binding.viewbind
10 | import com.hi.dhl.demo.binding.databinding.Fragment1Binding
11 |
12 | /**
13 | *
14 | * author: dhl
15 | * date : 2020/12/21
16 | * desc :
17 | *
18 | */
19 | class Fragment1 : Fragment() {
20 |
21 | val binding: Fragment1Binding by viewbind()
22 |
23 | override fun onCreateView(
24 | inflater: LayoutInflater, container: ViewGroup?,
25 | savedInstanceState: Bundle?
26 | ): View {
27 | return binding.root
28 | }
29 |
30 |
31 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
32 | super.onViewCreated(view, savedInstanceState)
33 | with(binding) {
34 | viewpager2.adapter = object : FragmentStateAdapter(this@Fragment1.requireActivity()) {
35 | override fun getItemCount() = 2
36 |
37 | override fun createFragment(position: Int): Fragment {
38 | return when (position) {
39 | 0 -> Fragment4()
40 | else -> Fragment5()
41 | }
42 | }
43 | }
44 |
45 | }
46 | }
47 |
48 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment2.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewpager2
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import androidx.navigation.Navigation
9 | import com.hi.dhl.binding.databind
10 | import com.hi.dhl.demo.binding.MainViewModel
11 | import com.hi.dhl.demo.binding.R
12 | import com.hi.dhl.demo.binding.databinding.Fragment1Binding
13 | import com.hi.dhl.demo.binding.databinding.Fragment2Binding
14 | import com.hi.dhl.demo.binding.databinding.FragmentNav2Binding
15 | import org.koin.androidx.viewmodel.ext.android.viewModel
16 |
17 | /**
18 | *
19 | * author: dhl
20 | * date : 2020/12/21
21 | * desc :
22 | *
23 | */
24 | class Fragment2 : Fragment() {
25 |
26 | val binding: Fragment2Binding by databind()
27 | val viewModel: MainViewModel by viewModel()
28 |
29 | override fun onCreateView(
30 | inflater: LayoutInflater, container: ViewGroup?,
31 | savedInstanceState: Bundle?
32 | ): View {
33 |
34 | return binding.apply {
35 | lifecycleOwner = this@Fragment2
36 | mainViewModel = viewModel
37 | }.root
38 | }
39 |
40 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
41 | super.onViewCreated(view, savedInstanceState)
42 |
43 | with(binding) {
44 | mainViewModel = viewModel
45 | viewModel.generateTimber()
46 | }
47 |
48 | }
49 |
50 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment3.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewpager2
2 |
3 | import android.os.Bundle
4 | import android.view.View
5 | import androidx.fragment.app.Fragment
6 | import com.hi.dhl.binding.viewbind
7 | import com.hi.dhl.demo.binding.R
8 | import com.hi.dhl.demo.binding.databinding.Fragment3Binding
9 |
10 | /**
11 | *
12 | * author: dhl
13 | * date : 2020/12/21
14 | * desc :
15 | *
16 | */
17 | class Fragment3 : Fragment(R.layout.fragment3) {
18 |
19 | val binding: Fragment3Binding by viewbind()
20 |
21 | // override fun onCreateView(
22 | // inflater: LayoutInflater, container: ViewGroup?,
23 | // savedInstanceState: Bundle?
24 | // ): View {
25 | // return binding.root
26 | // }
27 |
28 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
29 | super.onViewCreated(view, savedInstanceState)
30 | }
31 |
32 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment4.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewpager2
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import com.hi.dhl.binding.viewbind
9 | import com.hi.dhl.demo.binding.databinding.Fragment4Binding
10 |
11 | /**
12 | *
13 | * author: dhl
14 | * date : 2020/12/21
15 | * desc :
16 | *
17 | */
18 | class Fragment4 : Fragment() {
19 |
20 | val binding: Fragment4Binding by viewbind()
21 |
22 | override fun onCreateView(
23 | inflater: LayoutInflater, container: ViewGroup?,
24 | savedInstanceState: Bundle?
25 | ): View {
26 | return binding.root
27 | }
28 |
29 |
30 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
31 | super.onViewCreated(view, savedInstanceState)
32 | }
33 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/Fragment5.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewpager2
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import com.hi.dhl.binding.viewbind
9 | import com.hi.dhl.demo.binding.databinding.Fragment5Binding
10 |
11 | /**
12 | *
13 | * author: dhl
14 | * date : 2020/12/21
15 | * desc :
16 | *
17 | */
18 | class Fragment5 : Fragment() {
19 |
20 | val binding: Fragment5Binding by viewbind()
21 |
22 | override fun onCreateView(
23 | inflater: LayoutInflater, container: ViewGroup?,
24 | savedInstanceState: Bundle?
25 | ): View {
26 | return binding.root
27 | }
28 |
29 |
30 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
31 | super.onViewCreated(view, savedInstanceState)
32 | }
33 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hi/dhl/demo/binding/viewpager2/ViewPager2Activity.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding.viewpager2
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import android.view.View
7 | import androidx.appcompat.app.AppCompatActivity
8 | import androidx.fragment.app.FragmentTransaction
9 | import com.hi.dhl.binding.viewbind
10 | import com.hi.dhl.demo.binding.R
11 | import com.hi.dhl.demo.binding.databinding.ActivityViewpagerBinding
12 |
13 |
14 | /**
15 | *
16 | * author: dhl
17 | * date : 2021/4/10
18 | * desc :
19 | *
20 | */
21 | class ViewPager2Activity : AppCompatActivity(), View.OnClickListener {
22 | val binding: ActivityViewpagerBinding by viewbind()
23 | var fragment1: Fragment1 = Fragment1()
24 | var fragment2: Fragment2 = Fragment2()
25 | var fragment3: Fragment3 = Fragment3()
26 | override fun onCreate(savedInstanceState: Bundle?) {
27 | super.onCreate(savedInstanceState)
28 |
29 | with(binding) {
30 |
31 | val fragmentTransaction = supportFragmentManager.beginTransaction()
32 | fragmentTransaction.add(R.id.container, fragment3)
33 | fragmentTransaction.add(R.id.container, fragment2)
34 | fragmentTransaction.add(R.id.container, fragment1)
35 | hideFragment(fragmentTransaction)
36 | fragmentTransaction.show(fragment1)
37 | fragmentTransaction.commit()
38 |
39 | getViews().forEach {
40 | it.setOnClickListener(this@ViewPager2Activity)
41 | }
42 | }
43 |
44 | }
45 |
46 | private fun getViews() = with(binding) {
47 | arrayListOf(
48 | tab1,
49 | tab2,
50 | tab3
51 | )
52 | }
53 |
54 | companion object {
55 | fun startActivity(activity: Activity) {
56 | activity.startActivity(Intent(activity, ViewPager2Activity::class.java))
57 | }
58 | }
59 |
60 | override fun onClick(v: View) {
61 | val fragmentTransaction = supportFragmentManager.beginTransaction()
62 | hideFragment(fragmentTransaction)
63 | with(binding) {
64 | when (v) {
65 | tab1 -> {
66 | fragmentTransaction.show(fragment1)
67 | }
68 | tab2 -> {
69 | fragmentTransaction.show(fragment2)
70 | }
71 | tab3 -> fragmentTransaction.show(fragment3)
72 | else -> {
73 | }
74 | }
75 | }
76 | fragmentTransaction.commitNow()
77 | }
78 |
79 | private fun hideFragment(fragmentTransaction: FragmentTransaction) {
80 | getViews().forEach {
81 | fragmentTransaction.hide(fragment1)
82 | fragmentTransaction.hide(fragment2)
83 | fragmentTransaction.hide(fragment3)
84 |
85 | }
86 | }
87 |
88 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/avatar.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/drawable/avatar.jpeg
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/drawable/logo.png
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_brvah.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_data_bind.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
11 |
12 |
15 |
16 |
17 |
18 |
23 |
24 |
32 |
33 |
39 |
40 |
49 |
50 |
56 |
57 |
58 |
67 |
68 |
77 |
78 |
79 |
88 |
89 |
95 |
96 |
102 |
103 |
110 |
111 |
118 |
119 |
126 |
127 |
130 |
131 |
132 |
133 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_data_bind_recycle.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_include.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
10 |
11 |
12 |
17 |
18 |
21 |
22 |
23 |
24 |
28 |
29 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_include_view_bind.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
19 |
20 |
26 |
27 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_navigation.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_nested.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
15 |
16 |
20 |
21 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_view_bind.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_view_stub.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
15 |
16 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_viewpager.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
22 |
23 |
29 |
30 |
39 |
40 |
49 |
50 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_app.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
21 |
22 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_data_binding.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
12 |
13 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment1.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
9 |
10 |
11 |
17 |
18 |
22 |
23 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment3.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment4.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment5.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_data_bind_recycle.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
10 |
11 |
14 |
15 |
16 |
19 |
20 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_nav_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
13 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_nav_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
9 |
10 |
11 |
16 |
17 |
21 |
22 |
31 |
32 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_nav_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
13 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_nested1.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_nested2.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_view_bind.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
16 |
17 |
23 |
24 |
31 |
32 |
39 |
40 |
47 |
48 |
55 |
56 |
63 |
64 |
67 |
68 |
75 |
76 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_include_data_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
9 |
10 |
11 |
16 |
17 |
24 |
25 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_include_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_merge_data_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
9 |
10 |
11 |
15 |
16 |
17 |
22 |
23 |
30 |
31 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_merge_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_view_custom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_view_custom_data.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
13 |
14 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/recycle_item_product.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
9 |
10 |
11 |
15 |
16 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/recycle_item_product_footer.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/recycle_item_product_header.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/view_stub.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/view_stub_data.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/navigation/nav_graph.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
15 |
16 |
17 |
21 |
22 |
25 |
26 |
27 |
31 |
32 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | 12sp
7 | 10sp
8 | 13sp
9 | 14sp
10 | 16sp
11 | 18sp
12 | 20sp
13 | 22sp
14 | 25sp
15 | 28sp
16 | 30sp
17 | 32sp
18 | 40sp
19 | 50sp
20 |
21 |
22 | 1dp
23 | 3dp
24 | 2dp
25 | 5dp
26 | 6dp
27 | 8dp
28 | 10dp
29 | 12dp
30 | 14dp
31 | 16dp
32 | 18dp
33 | 20dp
34 | 30dp
35 | 25dp
36 | 40dp
37 |
38 |
39 | 1dp
40 | 3dp
41 | 2dp
42 | 5dp
43 | 6dp
44 | 7dp
45 | 8dp
46 | 10dp
47 | 12dp
48 | 13dp
49 | 15dp
50 | 16dp
51 | 22dp
52 | 30dp
53 | 35dp
54 | 45dp
55 | 40dp
56 | 50dp
57 | 60dp
58 | 20dp
59 | 25dp
60 | 80dp
61 | 100dp
62 |
63 | 8dp
64 | 16dp
65 | 24dp
66 | 32dp
67 | 40dp
68 | 50dp
69 | 48dp
70 | 56dp
71 | 64dp
72 | 72dp
73 | 80dp
74 | 160dp
75 |
76 | 8dp
77 |
78 | 100dp
79 | 10dp
80 | 200dp
81 | 180dp
82 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Binding
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
18 |
19 |
24 |
--------------------------------------------------------------------------------
/app/src/test/java/com/hi/dhl/demo/binding/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.demo.binding
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/binding/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'kotlin-android'
4 | }
5 |
6 | android {
7 | compileSdkVersion 32
8 |
9 | defaultConfig {
10 | minSdkVersion 14
11 | targetSdkVersion 32
12 | versionCode 10109
13 | versionName "1.1.9"
14 |
15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16 | consumerProguardFiles "consumer-rules.pro"
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 | compileOptions {
26 | sourceCompatibility JavaVersion.VERSION_1_8
27 | targetCompatibility JavaVersion.VERSION_1_8
28 | }
29 | kotlinOptions {
30 | jvmTarget = '1.8'
31 | }
32 | buildFeatures {
33 | dataBinding = true
34 | viewBinding = true
35 | }
36 | }
37 |
38 | dependencies {
39 | ext.core = "1.3.2"
40 | ext.appcompat = "1.2.0"
41 | ext.recyclerview = "1.1.0"
42 | implementation 'androidx.lifecycle:lifecycle-common-java8:2.3.1'
43 | implementation "androidx.core:core-ktx:$core"
44 | implementation "androidx.appcompat:appcompat:$appcompat"
45 | implementation "androidx.recyclerview:recyclerview:$recyclerview"
46 | }
47 |
48 | apply from: "${rootDir}/gradle-mvn-push.gradle"
--------------------------------------------------------------------------------
/binding/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hi-dhl/Binding/43ca731602ae3b15094b67c1fe904ab1e13410f7/binding/consumer-rules.pro
--------------------------------------------------------------------------------
/binding/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/binding/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/binding/src/main/java/androidx/lifecycle/BindingLifecycleObserver.kt:
--------------------------------------------------------------------------------
1 | package androidx.lifecycle
2 |
3 | /**
4 | *
5 | * author: dhl
6 | * date : 2021/1/25
7 | * desc :
8 | *
9 | */
10 |
11 | open class BindingLifecycleObserver : DefaultLifecycleObserver {
12 |
13 | override fun onCreate(owner: LifecycleOwner) {
14 | }
15 |
16 | override fun onStart(owner: LifecycleOwner) {
17 | }
18 |
19 | override fun onResume(owner: LifecycleOwner) {
20 | }
21 |
22 | override fun onPause(owner: LifecycleOwner) {
23 | }
24 |
25 | override fun onStop(owner: LifecycleOwner) {
26 | }
27 |
28 | override fun onDestroy(owner: LifecycleOwner) {
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/binding/src/main/java/com/hi/dhl/binding/base/ActivityDelegate.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.binding.base
2 |
3 | import android.app.Activity
4 | import android.os.Build
5 | import androidx.activity.ComponentActivity
6 | import androidx.viewbinding.ViewBinding
7 | import com.hi.dhl.binding.observerWhenDestroyed
8 | import com.hi.dhl.binding.registerLifecycleBelowQ
9 | import kotlin.properties.ReadOnlyProperty
10 |
11 | /**
12 | *
13 | * author: dhl
14 | * date : 2020/12/15
15 | * desc :
16 | *
17 | */
18 |
19 | abstract class ActivityDelegate(
20 | activity: Activity
21 | ) : ReadOnlyProperty {
22 |
23 | protected var viewBinding: T? = null
24 | private val LIFECYCLE_FRAGMENT_TAG = "com.hi.dhl.binding.lifecycle_fragment"
25 |
26 | init {
27 | when (activity) {
28 | is ComponentActivity -> activity.lifecycle.observerWhenDestroyed { destroyed() }
29 | else -> {
30 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
31 | activity.observerWhenDestroyed { destroyed() }
32 | }
33 | }
34 | }
35 |
36 | }
37 |
38 | fun addLifecycleFragment(activity: Activity) {
39 | activity.registerLifecycleBelowQ {
40 | destroyed()
41 | }
42 | }
43 |
44 | private fun destroyed() {
45 | viewBinding = null
46 | }
47 | }
--------------------------------------------------------------------------------
/binding/src/main/java/com/hi/dhl/binding/base/DialogDelegate.kt:
--------------------------------------------------------------------------------
1 | package com.hi.dhl.binding.base
2 |
3 | import android.app.Dialog
4 | import androidx.lifecycle.Lifecycle
5 | import androidx.viewbinding.ViewBinding
6 | import com.hi.dhl.binding.observerWhenDestroyed
7 | import kotlin.properties.ReadOnlyProperty
8 |
9 | /**
10 | *
11 | * author: dhl
12 | * date : 2020/12/15
13 | * desc :
14 | *
15 | */
16 | abstract class DialogDelegate(
17 | lifecycle: Lifecycle? = null
18 | ) : ReadOnlyProperty