Repository: Werb/MoreType Branch: master Commit: 371878a4003a Files: 137 Total size: 227.3 KB Directory structure: gitextract_uxrnujua/ ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── README_EN.md ├── README_ZH.md ├── app/ │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── com/ │ │ └── werb/ │ │ └── moretype/ │ │ └── ExampleInstrumentedTest.kt │ ├── main/ │ │ ├── AndroidManifest.xml │ │ ├── java/ │ │ │ └── com/ │ │ │ └── werb/ │ │ │ └── moretype/ │ │ │ ├── MyApp.kt │ │ │ ├── TitleViewHolder.kt │ │ │ ├── Utils.kt │ │ │ ├── anim/ │ │ │ │ ├── AnimActivity.kt │ │ │ │ ├── AnimType.kt │ │ │ │ └── AnimViewHolder.kt │ │ │ ├── click/ │ │ │ │ ├── ItemClick.kt │ │ │ │ ├── ItemClickActivity.kt │ │ │ │ ├── ItemClickOneViewHolder.kt │ │ │ │ └── ItemClickTwoViewHolder.kt │ │ │ ├── complete/ │ │ │ │ ├── Complete.kt │ │ │ │ ├── CompleteActivity.kt │ │ │ │ ├── CompleteViewHolder.kt │ │ │ │ ├── FootViewHolder.kt │ │ │ │ ├── Footer.kt │ │ │ │ ├── HorizontalData.kt │ │ │ │ ├── HorizontalItemViewHolder.kt │ │ │ │ ├── HorizontalViewHolder.kt │ │ │ │ └── OnLoadMoreListener.kt │ │ │ ├── data/ │ │ │ │ └── DataServer.kt │ │ │ ├── diff/ │ │ │ │ ├── DiffDataActivity.kt │ │ │ │ └── DiffDataCallback.kt │ │ │ ├── main/ │ │ │ │ ├── MainActivity.kt │ │ │ │ ├── MainCard.kt │ │ │ │ ├── MainCardViewHolder.kt │ │ │ │ └── PayLoadOne.kt │ │ │ ├── me/ │ │ │ │ ├── AboutMeActivity.kt │ │ │ │ ├── MeInfo.kt │ │ │ │ ├── MeInfoViewHolder.kt │ │ │ │ ├── MeMessage.kt │ │ │ │ ├── MeMessageViewHolder.kt │ │ │ │ ├── MeThanks.kt │ │ │ │ ├── MeThanksViewHolder.kt │ │ │ │ └── MeTitleViewType.kt │ │ │ ├── multi/ │ │ │ │ ├── ImageSize.kt │ │ │ │ ├── Message.kt │ │ │ │ ├── MessageInViewHolder.kt │ │ │ │ ├── MessageOutViewHolder.kt │ │ │ │ └── MultiRegisterActivity.kt │ │ │ └── single/ │ │ │ ├── SingleImage.kt │ │ │ ├── SingleItemDecoration.kt │ │ │ ├── SingleRegisterActivity.kt │ │ │ ├── SingleText.kt │ │ │ ├── SingleTypeOneViewHolder.kt │ │ │ └── SingleTypeTwoViewHolder.kt │ │ └── res/ │ │ ├── drawable/ │ │ │ ├── widget_button_bg.xml │ │ │ ├── widget_input_bg.xml │ │ │ ├── widget_input_edit_bg.xml │ │ │ ├── widget_message_in_text_bg.xml │ │ │ ├── widget_message_out_text_bg.xml │ │ │ ├── widget_round_bg.xml │ │ │ └── widget_spinner_bg.xml │ │ ├── layout/ │ │ │ ├── activity_anim.xml │ │ │ ├── activity_complete.xml │ │ │ ├── activity_diff.xml │ │ │ ├── activity_item_click.xml │ │ │ ├── activity_main.xml │ │ │ ├── activity_me.xml │ │ │ ├── activity_multi_register.xml │ │ │ ├── activity_single_register.xml │ │ │ ├── item_view_about_me_info.xml │ │ │ ├── item_view_about_me_message.xml │ │ │ ├── item_view_about_me_thx.xml │ │ │ ├── item_view_about_me_title.xml │ │ │ ├── item_view_anim.xml │ │ │ ├── item_view_click_one.xml │ │ │ ├── item_view_click_two.xml │ │ │ ├── item_view_complete.xml │ │ │ ├── item_view_footer.xml │ │ │ ├── item_view_horizontal_item.xml │ │ │ ├── item_view_horizontal_list.xml │ │ │ ├── item_view_main.xml │ │ │ ├── item_view_multi_message_in.xml │ │ │ ├── item_view_multi_message_out.xml │ │ │ ├── item_view_single_type_one.xml │ │ │ ├── item_view_single_type_two.xml │ │ │ ├── item_view_title.xml │ │ │ ├── widget_view_complete_text.xml │ │ │ ├── widget_view_message_in_image.xml │ │ │ ├── widget_view_message_in_text.xml │ │ │ ├── widget_view_message_input.xml │ │ │ ├── widget_view_message_out_image.xml │ │ │ └── widget_view_message_out_text.xml │ │ ├── values/ │ │ │ ├── arrays.xml │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── xml/ │ │ └── shortcuts.xml │ └── test/ │ └── java/ │ └── com/ │ └── werb/ │ └── moretype/ │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle/ │ └── wrapper/ │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew ├── gradlew.bat ├── library/ │ ├── .gitignore │ ├── bintray.gradle │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ ├── main/ │ │ ├── AndroidManifest.xml │ │ ├── kotlin/ │ │ │ └── com/ │ │ │ └── werb/ │ │ │ └── library/ │ │ │ ├── MoreAdapter.kt │ │ │ ├── MoreType.kt │ │ │ ├── MoreViewHolder.kt │ │ │ ├── action/ │ │ │ │ ├── DataAction.kt │ │ │ │ └── MoreClickListener.kt │ │ │ ├── annotation/ │ │ │ │ └── MoreInject.kt │ │ │ ├── exception/ │ │ │ │ ├── ModelNotRegisterException.kt │ │ │ │ ├── MultiModelNotRegisterException.kt │ │ │ │ └── ViewHolderInitErrorException.kt │ │ │ ├── extension/ │ │ │ │ ├── AlphaAnimation.kt │ │ │ │ ├── AnimExtension.kt │ │ │ │ ├── MoreAnimation.kt │ │ │ │ ├── ScaleInAnimation.kt │ │ │ │ ├── SlideInBottomAnimation.kt │ │ │ │ ├── SlideInLeftAnimation.kt │ │ │ │ └── SlideInRightAnimation.kt │ │ │ └── link/ │ │ │ ├── LayoutID.kt │ │ │ ├── MoreLink.kt │ │ │ ├── MoreLinkManager.kt │ │ │ ├── MultiLink.kt │ │ │ ├── RegisterItem.kt │ │ │ ├── SoleLinkManager.kt │ │ │ └── XDiffCallback.kt │ │ └── res/ │ │ └── values/ │ │ └── ids.xml │ └── test/ │ └── java/ │ └── com/ │ └── werb/ │ └── library/ │ └── ExampleUnitTest.java └── settings.gradle ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Generated files bin/ gen/ release/ # Gradle files .gradle/ build/ # Local configuration file (sdk path, etc) local.properties # IntelliJ project files *.iml .idea/ # Eclipse project files .settings/ # Misc .DS_Store .classpath .project ================================================ FILE: .travis.yml ================================================ language: android jdk: oraclejdk8 sudo: false android: components: - build-tools-28.0.3 - android-28 - extra-android-m2repository - extra-android-support licenses: - 'android-sdk-preview-license-52d11cd2' - 'android-sdk-license-.+' - 'google-gdk-license-.+' before_install: - chmod +x gradlew - mkdir "$ANDROID_HOME/licenses" || true - echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license" - echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license" before_script: - touch local.properties script: - "./gradlew assembleRelease" ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2017 Werb Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: README.md ================================================ # MoreType > new method to build data in RecyclerView with Kotlin! download Click icon download lastest sample [English](https://github.com/Werb/MoreType/blob/master/README_EN.md) | [中文版](https://github.com/Werb/MoreType/blob/master/README_ZH.md) [![Build Status](https://travis-ci.org/werbhelius/MoreType.svg?branch=master)](https://travis-ci.org/werbhelius/MoreType) [![Hex.pm](https://img.shields.io/hexpm/l/plug.svg)](https://github.com/Werb/MoreType/blob/master/LICENSE) [ ![Download](https://api.bintray.com/packages/werbhelius/maven/moretype/images/download.svg) ](https://bintray.com/werbhelius/maven/moretype/_latestVersion) [![API](https://img.shields.io/badge/API-16%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=16) **Keyword: Data driven view 【数据驱动视图】** ## Preview ![more-type-one](./screenshot/type1.png) ![more-type-two](./screenshot/type2.png) ## Dependency ```gradle compile 'com.werb.moretype:moretype:$last_version' ``` or ```gradle implementation 'com.werb.moretype:moretype:$last_version' ``` ## Update log #### [v0.4.0](https://github.com/Werb/MoreType/releases/tag/v0.4.0) * bug fix * update MoreViewHolder Constructor ## Usage Keyword: Data driven view 【数据驱动视图】 #### Step 1. create a data model class, like: ```kotlin data class SingleText(val title: String, val desc: String, val url: String) ``` or ```kotlin class SingleText { var title: String? = null var desc: String? = null var url: String? = null } ``` ### Step 2. create a class (xxxViewHolder) extends abstract class `MoreViewHolder()` , like: ```kotlin // Register layoutID here or Register with adapter in Activity @LayoutID(R.layout.item_view_single_type_one) class SingleTypeOneViewHolder(values: MutableMap, containerView: View) : MoreViewHolder(containerView) { override fun bindData(data: SingleText, payloads: List) { title.text = data.title desc.text = data.desc icon.setImageURI(data.url) } } ``` ### Step 3. `register` and `attach` to `recyclerview` in Any where you build list, like: ```kotlin import kotlinx.android.synthetic.main.activity_single_register.* class SingleRegisterActivity: AppCompatActivity() { private val adapter = MoreAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_single_register) list.layoutManager = LinearLayoutManager(this) /* register ViewHolder and attach to recyclerView */ adapter.apply { // two method register Layout register(RegisterItem(R.layout.item_view_single_type_one, SingleTypeOneViewHolder::class.java)) // or register(SingleTypeOneViewHolder::class.java) attachTo(single_register_list) } /* load any data List or model object */ adapter.loadData(DataServer.getSingleRegisterData()) } } ``` Upon completion of these three steps, a list based on the [Data Driven View] has been completed. ================================================ FILE: README_EN.md ================================================ # MoreType > new method to build data in RecyclerView with Kotlin! download Click icon download lastest sample English | [中文版](https://github.com/Werb/MoreType/blob/master/README_ZH.md) [![Build Status](https://travis-ci.org/werbhelius/MoreType.svg?branch=master)](https://travis-ci.org/werbhelius/MoreType) [![Hex.pm](https://img.shields.io/hexpm/l/plug.svg)](https://github.com/Werb/MoreType/blob/master/LICENSE) [ ![Download](https://api.bintray.com/packages/werbhelius/maven/moretype/images/download.svg) ](https://bintray.com/werbhelius/maven/moretype/_latestVersion) [![API](https://img.shields.io/badge/API-16%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=16) Keyword: Data driven view 【数据驱动视图】 Before writing a project in the company, we need to write a large number of recyclerViews to display data, which means that each recyclerView must write an Adapter, but also for different views according to getItemViewType () write different ViewHolder, in the presence of multiple views Time, an adapter in the code will be very redundant, And the coupling is very high, the follow-up changes are very friendly. **Any of our recyclerViews are based on the data returned by the server to build, so I was wondering, can not simply use the data to drive the view, which is the core of the MoreType 【data driven view】.** I am not The first thought of this concept, I first saw the concept of practice, is [drakeet's MultiType](https://github.com/drakeet/MultiType). After watching his project, i know this is what I hope [Data driven view]. **Kotlin on Android Now official.** Don't have to worry about the NullPointerException, simple code style, in the first time I like to like it, so I decided to use Kotlin to develop a **【data-driven view】** third-party library of the , this was MoreType **【Give you more likely】**. ## Preview ![more-type-one](./screenshot/type1.png) ![more-type-two](./screenshot/type2.png) ## Dependency ```gradle compile 'com.werb.moretype:moretype:$last_version' ``` or ```gradle implementation 'com.werb.moretype:moretype:$last_version' ``` ## Usage Keyword: Data driven view 【数据驱动视图】 #### Step 1. create a data model class, like: ```kotlin data class SingleText(val title: String, val desc: String, val url: String) ``` or ```kotlin class SingleText { var title: String? = null var desc: String? = null var url: String? = null } ``` ### Step 2. create a class (xxxViewHolder) extends abstract class `MoreViewHolder()` , like: ```kotlin // Register layoutID here or Register with adapter in Activity @LayoutID(R.layout.item_view_single_type_one) class SingleTypeOneViewHolder(values: MutableMap, containerView: View) : MoreViewHolder(containerView) { override fun bindData(data: SingleText, payloads: List) { title.text = data.title desc.text = data.desc icon.setImageURI(data.url) } } ``` ### Step 3. `register` and `attach` to `recyclerview` in Any where you build list, like: ```kotlin import kotlinx.android.synthetic.main.activity_single_register.* class SingleRegisterActivity: AppCompatActivity() { private val adapter = MoreAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_single_register) list.layoutManager = LinearLayoutManager(this) /* register ViewHolder and attach to recyclerView */ adapter.apply { // two method register Layout register(RegisterItem(R.layout.item_view_single_type_one, SingleTypeOneViewHolder::class.java)) // or register(SingleTypeOneViewHolder::class.java) attachTo(single_register_list) } /* load any data List or model object */ adapter.loadData(DataServer.getSingleRegisterData()) } } ``` Upon completion of these three steps, a list based on the [Data Driven View] has been completed. ## Feature ### Multi Register: Register one2more ViewHolder Usually data and view are one-to-one relationships, like Feeds list, MoreType alse provide Multi Register like IM list, one data to Many views, MoreType can do it easily. ```kotlin adapter.apply { multiRegister(object : MultiLink() { override fun link(data: Message): RegisterItem { return if (data.me){ RegisterItem(R.layout.item_view_multi_message_out, MessageOutViewHolder::class.java) }else { RegisterItem(R.layout.item_view_multi_message_in, MessageInViewHolder::class.java) } } }) attachTo(multi_register_list) } ``` ### Animation: Provides five types of Animation Provide 5 animations: **Alpha** , **Scale** , **SlideInBottom** , **SlideInLeft** , **SlideInRight** ```Kotlin adapter.register(TitleViewType()) .register(AnimViewType()) /* assign Animation */ .renderWithAnimation(AlphaAnimation()) /* set Animation start position in list */ .startAnimPosition(1) /* set is always show animation or show in first display */ .firstShowAnim(true) .attachTo(anim_list) ``` Also suppost custom Animation, create a class implement `MoreAnimation` and override `getItemAnimators(view: View)` , like: ```kotlin class SlideInLeftAnimation : MoreAnimation { override fun getItemAnimators(view: View): Array{ return arrayOf(ObjectAnimator.ofFloat(view, "translationX", -view.rootView.width.toFloat(), 0f)) } } ``` ### ItemClick: Support onItemClick and onItemLongClick Two ways to achieve ItemClick: **In ViewHolder** and **In Activity** **In ViewType** : Just use `view.setOnClickListener {}` in ViewType **In Activity** : 1. use `addOnClickListener(view: View)` or `addOnClickListener(viewId: Int)` to bind event in ViewType 2. In the Activity through the `MoreClickListener ()` to create a click event object, you can pass in the registration [sample](https://github.com/Werb/MoreType/tree/master/app/src/main/java/com/werb/moretype/click) ### Refresh and loadMore **Refresh**: use `SwipeRefreshLayout` **LoadMore**: build `Footer.class` and `FootViewType` , when list smooth to last position - 1, show `FootViewType` and remove `FootViewType` when new data load successful [sample](https://github.com/Werb/MoreType/blob/master/app/src/main/java/com/werb/moretype/complete/CompleteActivity.kt) ### Sole Global ViewType **Register global viewType in Custom Application** ```kotlin class MyApp: Application() { companion object { @Volatile lateinit var myApp: MyApp private set } override fun onCreate() { super.onCreate() myApp = this // Sole Global Register, like footer , Cutting line MoreType.soleRegister(RegisterItem(R.layout.item_view_footer, FootViewHolder::class.java)) } } ``` and we can use it in any where with `adapter.userSoleRegister()` ### inject Value In ViewHolder ```kotlin adapter.apply { injectValueInAllHolder(mapOf("qqqqqq" to "1233333333")) // inject in All Holder register(MainCardViewHolder::class.java, playLoadClick, mapOf("someValue" to "123")) // inject in single holder attachTo(more_list) } ``` ## Thanks [MultiType](https://github.com/drakeet/MultiType) [recyclerview-animators](https://github.com/wasabeef/recyclerview-animators) ## LICENSE [Apache License](./LICENSE) ================================================ FILE: README_ZH.md ================================================ # MoreType > new method to build data in RecyclerView with Kotlin! download Click icon download lastest sample [English](https://github.com/Werb/MoreType/blob/master/README_EN.md) | [中文版](https://github.com/Werb/MoreType/blob/master/README_ZH.md) [![Build Status](https://travis-ci.org/werbhelius/MoreType.svg?branch=master)](https://travis-ci.org/werbhelius/MoreType) [![Hex.pm](https://img.shields.io/hexpm/l/plug.svg)](https://github.com/Werb/MoreType/blob/master/LICENSE) [ ![Download](https://api.bintray.com/packages/werbhelius/maven/moretype/images/download.svg) ](https://bintray.com/werbhelius/maven/moretype/_latestVersion) [![API](https://img.shields.io/badge/API-16%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=16) 关键词:【数据驱动视图】 之前在写公司的项目的时候,需要写大量的界面,这就意味着每一个界面都要写一个 Adapter,同时还要对不同的视图根据 getItemViewHolder() 写不同的 ViewHolder,在存在多种视图的时候,一个 Adapter 中的代码就会很冗余,而且耦合度很高,对后续的修改很不友好。 **我们任意一个界面都是依靠服务器返回的数据构建的,所以我就在想,能不能简单的使用数据来驱动视图,这就也是 MoreType 的核心所在【数据驱动视图】。** 我并不是第一个想到这个概念的人,我最早看到这个概念的实践,是 drakeet 的 [MultiType](https://github.com/drakeet/MultiType) 。当时看完他的项目之后,有一种醍醐灌顶的畅快,这就是我所希望的【数据驱动视图】。 大概在一个月前,开始接触 Kotlin,Kotlin 是一个让开发者用起来很爽的语言,不必深陷烦人的空指针异常,简洁的代码风格,在初次尝时候我就喜欢上了它,所以我决定用 Kotlin 来开发一个【数据驱动视图】的第三方库,从而就有了 MoreType 的产生【给你更多的可能】。 [与 MultiType 的异同](https://github.com/Werb/MoreType/issues/1) ## Preview ![more-type-one](./screenshot/type1.png) ![more-type-two](./screenshot/type2.png) ## Dependency ```gradle compile 'com.werb.moretype:moretype:$last_version' ``` or ```gradle implementation 'com.werb.moretype:moretype:$last_version' ``` ## Usage Keyword: 【数据驱动视图】 #### Step 1. 创建一个数据模型类,例如: ```kotlin data class SingleText(val title: String, val desc: String, val url: String) ``` or ```kotlin class SingleText { var title: String? = null var desc: String? = null var url: String? = null } ``` ### Step 2. 创建一个类 xxxViewHolder 继承抽象类 `MoreViewHolder()` 例如: ```kotlin // Register layoutID here or Register with adapter in Activity @LayoutID(R.layout.item_view_single_type_one) class SingleTypeOneViewHolder(values: MutableMap, containerView: View) : MoreViewHolder(containerView) { override fun bindData(data: SingleText, payloads: List) { title.text = data.title desc.text = data.desc icon.setImageURI(data.url) } } ``` ### Step 3. 在使用 `RecyclerView` 的地方,声明 `MoreAdapter()`对象,`register` 需要的 `ViewHolder`,同时和 `RecyclerView` 绑定 ```kotlin import kotlinx.android.synthetic.main.activity_single_register.* class SingleRegisterActivity: AppCompatActivity() { private val adapter = MoreAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_single_register) list.layoutManager = LinearLayoutManager(this) /* register ViewHolder and attach to recyclerView */ adapter.apply { // two method register Layout register(RegisterItem(R.layout.item_view_single_type_one, SingleTypeOneViewHolder::class.java)) // or register(SingleTypeOneViewHolder::class.java) attachTo(single_register_list) } /* load any data List or model object */ adapter.loadData(DataServer.getSingleRegisterData()) } } ``` 完成这三步,一个根据【数据驱动视图】的列表就已经构建完成。 ## Feature ### Multi Register: Register one2more ViewHolder 通常我们的数据和视图是一对一的关系,比如瀑布流。MoreType 同时提供一种数据类型对应多种视图的情况,例如私信界面。 ```kotlin adapter.apply { multiRegister(object : MultiLink() { override fun link(data: Message): RegisterItem { return if (data.me){ RegisterItem(R.layout.item_view_multi_message_out, MessageOutViewHolder::class.java) }else { RegisterItem(R.layout.item_view_multi_message_in, MessageInViewHolder::class.java) } } }) attachTo(multi_register_list) } ``` ### Animation: Provides five types of Animation 提供五种动画支持: **Alpha** , **Scale** , **SlideInBottom** , **SlideInLeft** , **SlideInRight** ```Kotlin adapter.register(TitleViewHolder()) .register(AnimViewHolder()) /* assign Animation */ .renderWithAnimation(AlphaAnimation()) /* set Animation start position in list */ .startAnimPosition(1) /* set is always show animation or show in first display */ .firstShowAnim(true) .attachTo(anim_list) ``` 同时支持自定义动画, 创建一个类实现接口 `MoreAnimation` and 重写 `getItemAnimators(view: View)` 例如: ```kotlin class SlideInLeftAnimation : MoreAnimation { override fun getItemAnimators(view: View): Array{ return arrayOf(ObjectAnimator.ofFloat(view, "translationX", -view.rootView.width.toFloat(), 0f)) } } ``` ### ItemClick: Support onItemClick and onItemLongClick 两种方式实现点击事件: **In ViewHolder** and **In Activity** **In ViewHolder 中处理点击事件** : 在ViewHolder中使用 `view.setOnClickListener {}` **In Activity 中处理点击事件** : 1. 在 ViewHolder 中使用 `addOnClickListener(view: View)` or `addOnClickListener(viewId: Int)` 绑定点击事件 2. 在 Activity 中通过 `MoreClickListener()` 创建点击事件对象,在注册的时候传入即可 [sample](https://github.com/Werb/MoreType/tree/master/app/src/main/java/com/werb/moretype/click) ### Refresh and loadMore **Refresh**: 使用 `SwipeRefreshLayout` 就可以实现下拉刷新 **LoadMore**: 通过构建 `Footer.class` 和 `FootViewHolder` 实现数据和视图的绑定, 当 RecyclerView 滚动在底部时显示 `FootViewHolder`,在数据请求成功后移除 `FootViewHolder` [sample](https://github.com/Werb/MoreType/blob/master/app/src/main/java/com/werb/moretype/complete/CompleteActivity.kt) ### 全局 ViewHolder **在自定义 Application中注册全局 ViewHolder** ```kotlin class MyApp: Application() { companion object { @Volatile lateinit var myApp: MyApp private set } override fun onCreate() { super.onCreate() myApp = this // Sole Global Register, like footer , Cutting line MoreType.soleRegister(RegisterItem(R.layout.item_view_footer, FootViewHolder::class.java)) } } ``` 通过 `adapter.userSoleRegister()` 调用使用全局 ViewHolder ### 向 ViewHolder 中传递数据 ```kotlin adapter.apply { injectValueInAllHolder(mapOf("qqqqqq" to "1233333333")) // inject in All Holder register(MainCardViewHolder::class.java, playLoadClick, mapOf("someValue" to "123")) // inject in single holder attachTo(more_list) } ``` ## Thanks [MultiType](https://github.com/drakeet/MultiType) [recyclerview-animators](https://github.com/wasabeef/recyclerview-animators) ## LICENSE [Apache License](./LICENSE) ================================================ FILE: app/.gitignore ================================================ /build ================================================ FILE: app/build.gradle ================================================ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 defaultConfig { applicationId "com.werb.moretype" minSdkVersion 16 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } androidExtensions { experimental = true } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-alpha4', { exclude group: 'com.android.support', module: 'support-annotations' }) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" testImplementation 'junit:junit:4.12' implementation project(':library') implementation 'androidx.appcompat:appcompat:1.1.0-alpha02' implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha02' implementation 'androidx.cardview:cardview:1.0.0' implementation 'com.google.android.material:material:1.1.0-alpha03' implementation 'com.facebook.fresco:fresco:1.7.1' implementation 'com.werb.permissionschecker:permissionschecker:0.0.1-beta2' implementation('com.werb.pickphotoview:pickphotoview:0.3.6-beta1', { exclude group: 'com.android.support' exclude group: 'com.werb.moretype' }) } ================================================ FILE: app/proguard-rules.pro ================================================ # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in /Users/wanbo/Dev/Android/sdk/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} # Uncomment this to preserve the line number information for # debugging stack traces. #-keepattributes SourceFile,LineNumberTable # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile ================================================ FILE: app/src/androidTest/java/com/werb/moretype/ExampleInstrumentedTest.kt ================================================ package com.werb.moretype import androidx.test.InstrumentationRegistry import androidx.test.runner.AndroidJUnit4 import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith /** * Instrumented test, which will execute on an Android device. * * See [testing documentation](http://d.android.com/tools/testing). */ @RunWith(AndroidJUnit4::class) class ExampleInstrumentedTest { @Test fun useAppContext() { // Context of the app under test. val appContext = InstrumentationRegistry.getTargetContext() assertEquals("com.werb.moretype", appContext.packageName) } } ================================================ FILE: app/src/main/AndroidManifest.xml ================================================ ================================================ FILE: app/src/main/java/com/werb/moretype/MyApp.kt ================================================ package com.werb.moretype import android.app.Application import com.facebook.drawee.backends.pipeline.Fresco import com.werb.library.MoreType import com.werb.library.link.RegisterItem import com.werb.moretype.complete.FootViewHolder /** * Created by wanbo on 2017/7/12. */ class MyApp: Application() { companion object { @Volatile lateinit var myApp: MyApp private set } override fun onCreate() { super.onCreate() myApp = this Fresco.initialize(myApp) // Sole Global Register, like footer , Cutting line MoreType.soleRegister(RegisterItem(R.layout.item_view_footer, FootViewHolder::class.java)) } } ================================================ FILE: app/src/main/java/com/werb/moretype/TitleViewHolder.kt ================================================ package com.werb.moretype import android.view.View import com.werb.library.MoreViewHolder import com.werb.library.link.LayoutID import com.werb.moretype.main.MainCard import kotlinx.android.synthetic.main.item_view_title.view.* /** * Created by wanbo on 2017/7/14. */ @LayoutID(R.layout.item_view_title) class TitleViewHolder(values: MutableMap, containerView: View) : MoreViewHolder(values, containerView) { private val title = containerView.title private val desc = containerView.desc override fun bindData(data: MainCard, payloads: List) { title.text = data.cardTitle desc.text = data.cardDesc } } ================================================ FILE: app/src/main/java/com/werb/moretype/Utils.kt ================================================ package com.werb.moretype import android.graphics.BitmapFactory import com.werb.library.MoreViewHolder import com.werb.moretype.multi.ImageSize import java.io.File import java.text.SimpleDateFormat import java.util.* /** * Created by wanbo on 2017/7/14. */ object Utils { fun sendTime(ms: Long): String { val calendar = Calendar.getInstance() calendar.timeInMillis = ms val format = SimpleDateFormat("MM/dd HH:mm") return format.format(Date(ms)) } fun dp2px(dpValue: Float): Int { val scale = MyApp.myApp.resources.displayMetrics.density return (dpValue * scale + 0.5f).toInt() } fun getIMImageSize(width: Double, height: Double): ImageSize { val fixWidth: Int val fixHeight: Int var ratio = width.toFloat() / height.toFloat() if (ratio > 1) { if (ratio > 3) { ratio = 3f } fixWidth = dp2px(200f) fixHeight = (fixWidth / ratio).toInt() } else { if (ratio < 0.3) { ratio = 0.3f } fixHeight = dp2px(200f) fixWidth = (ratio * fixHeight).toInt() } return ImageSize(fixWidth, fixHeight) } fun readImageSize(path: String): ImageSize? { val orFile = File(path) if (!orFile.exists()) { return null } val options = BitmapFactory.Options() options.inJustDecodeBounds = true BitmapFactory.decodeFile(orFile.absolutePath, options) return ImageSize(options.outWidth, options.outHeight) } } ================================================ FILE: app/src/main/java/com/werb/moretype/anim/AnimActivity.kt ================================================ package com.werb.moretype.anim import android.app.Activity import android.content.Intent import android.os.Bundle import android.view.View import android.widget.AdapterView import android.widget.AdapterView.OnItemSelectedListener import android.widget.CompoundButton import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.RecyclerView import com.werb.library.MoreAdapter import com.werb.library.extension.* import com.werb.library.link.RegisterItem import com.werb.moretype.R import com.werb.moretype.TitleViewHolder import com.werb.moretype.data.DataServer import kotlinx.android.synthetic.main.activity_anim.* /** * Created by wanbo on 2017/7/15. */ class AnimActivity : AppCompatActivity() { private val adapter = MoreAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_anim) toolbar.setNavigationIcon(R.mipmap.ic_close_white_24dp) toolbar.setNavigationOnClickListener { finish() } anim_list.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this) adapter.apply { register(RegisterItem(R.layout.item_view_title, TitleViewHolder::class.java)) register(RegisterItem(R.layout.item_view_anim, AnimViewHolder::class.java)) startAnimPosition(1) } switch_view.setOnCheckedChangeListener({ _: CompoundButton, check: Boolean -> adapter.firstShowAnim(check) }) spinner.onItemSelectedListener = object : OnItemSelectedListener { override fun onNothingSelected(p0: AdapterView<*>?) { } override fun onItemSelected(p0: AdapterView<*>?, view: View?, position: Int, p3: Long) { when (position) { 0 -> { adapter.removeAllData() adapter.renderWithAnimation(AlphaAnimation()) adapter.loadData(DataServer.getAnimData()) adapter.attachTo(anim_list) } 1 -> { adapter.removeAllData() adapter.renderWithAnimation(ScaleInAnimation()) adapter.loadData(DataServer.getAnimData()) adapter.attachTo(anim_list) } 2 -> { adapter.removeAllData() adapter.renderWithAnimation(SlideInLeftAnimation()) adapter.loadData(DataServer.getAnimData()) adapter.attachTo(anim_list) } 3 -> { adapter.removeAllData() adapter.renderWithAnimation(SlideInRightAnimation()) adapter.loadData(DataServer.getAnimData()) adapter.attachTo(anim_list) } 4 -> { adapter.removeAllData() adapter.renderWithAnimation(SlideInBottomAnimation()) adapter.loadData(DataServer.getAnimData()) adapter.attachTo(anim_list) } } } } } companion object { fun startActivity(activity: Activity) { activity.startActivity(Intent(activity, AnimActivity::class.java)) } } } ================================================ FILE: app/src/main/java/com/werb/moretype/anim/AnimType.kt ================================================ package com.werb.moretype.anim /** * Created by wanbo on 2017/7/15. */ class AnimType(val title: String, val type: String, val typeIcon: String, val thumb: String) ================================================ FILE: app/src/main/java/com/werb/moretype/anim/AnimViewHolder.kt ================================================ package com.werb.moretype.anim import android.view.View import com.werb.library.MoreViewHolder import kotlinx.android.synthetic.main.item_view_anim.* /** * Created by wanbo on 2017/7/15. */ class AnimViewHolder(values: MutableMap, containerView: View) : MoreViewHolder(values, containerView) { override fun bindData(data: AnimType, payloads: List) { title.text = data.title type.text = data.type icon.setImageURI(data.typeIcon) thumb.setImageURI(data.thumb) } } ================================================ FILE: app/src/main/java/com/werb/moretype/click/ItemClick.kt ================================================ package com.werb.moretype.click /** * Created by wanbo on 2017/7/15. */ data class ItemClick(val title: String, val url: String, val click: Boolean) ================================================ FILE: app/src/main/java/com/werb/moretype/click/ItemClickActivity.kt ================================================ package com.werb.moretype.click import android.app.Activity import android.content.DialogInterface import android.content.Intent import android.os.Bundle import android.view.MotionEvent import android.view.View import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import com.werb.library.MoreAdapter import com.werb.library.action.MoreClickListener import com.werb.library.link.MultiLink import com.werb.library.link.RegisterItem import com.werb.moretype.R import com.werb.moretype.TitleViewHolder import com.werb.moretype.data.DataServer import kotlinx.android.synthetic.main.activity_item_click.* /** * Created by wanbo on 2017/7/15. */ class ItemClickActivity : AppCompatActivity() { private val adapter = MoreAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_item_click) toolbar.setNavigationIcon(R.mipmap.ic_close_white_24dp) toolbar.setNavigationOnClickListener { finish() } item_click_list.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this) item_click_list.adapter = adapter adapter.apply { register(RegisterItem(R.layout.item_view_title, TitleViewHolder::class.java)) multiRegister(object : MultiLink() { override fun link(data: ItemClick): RegisterItem { return if (data.click) { RegisterItem(R.layout.item_view_click_one, ItemClickOneViewHolder::class.java, oneClick) } else { RegisterItem(R.layout.item_view_click_two, ItemClickTwoViewHolder::class.java, twoClick) } } }) } adapter.loadData(DataServer.getItemClickData()) } private val oneClick = object : MoreClickListener() { override fun onItemClick(view: View, position: Int) { when (view.id) { R.id.icon -> { val url = view.tag as String Toast.makeText(view.context, "click icon in Activity icon url is " + url, Toast.LENGTH_SHORT).show() } R.id.button -> { Toast.makeText(view.context, "click button in Activity position is " + position.toString(), Toast.LENGTH_SHORT).show() } } } override fun onItemTouch(view: View, event: MotionEvent, position: Int): Boolean { Toast.makeText(this@ItemClickActivity, "111", Toast.LENGTH_SHORT).show() return true } override fun onItemLongClick(view: View, position: Int): Boolean { return false } } private val twoClick = object : MoreClickListener() { override fun onItemClick(view: View, position: Int) { when (view.id) { R.id.icon -> { val url = view.tag as String Toast.makeText(view.context, "click icon in Activity icon url is " + url, Toast.LENGTH_SHORT).show() } } } override fun onItemLongClick(view: View, position: Int): Boolean { when (view.id) { R.id.button -> { AlertDialog.Builder(view.context) .setTitle("Button click in Activity position is " + position.toString()) .setPositiveButton("sure", { _: DialogInterface, _: Int -> }) .show() } } return true } } companion object { fun startActivity(activity: Activity) { activity.startActivity(Intent(activity, ItemClickActivity::class.java)) } } } ================================================ FILE: app/src/main/java/com/werb/moretype/click/ItemClickOneViewHolder.kt ================================================ package com.werb.moretype.click import android.view.View import android.widget.Toast import com.werb.library.MoreViewHolder import kotlinx.android.synthetic.main.item_view_click_one.* /** * Created by wanbo on 2017/7/15. */ class ItemClickOneViewHolder(values: MutableMap, containerView: View) : MoreViewHolder(values, containerView) { override fun bindData(data: ItemClick, payloads: List) { title.text = data.title icon.setImageURI(data.url) /* click in viewType */ itemView.setOnClickListener { Toast.makeText(containerView.context, " click in ViewType position is " + layoutPosition.toString(), Toast.LENGTH_SHORT).show() } icon.tag = data.url /* click in activity */ addOnClickListener(icon) addOnClickListener(button) addOnTouchListener(containerView) } } ================================================ FILE: app/src/main/java/com/werb/moretype/click/ItemClickTwoViewHolder.kt ================================================ package com.werb.moretype.click import android.view.View import android.widget.Toast import com.werb.library.MoreViewHolder import kotlinx.android.synthetic.main.item_view_click_two.* /** * Created by wanbo on 2017/7/15. */ class ItemClickTwoViewHolder(values: MutableMap, containerView: View) : MoreViewHolder(values, containerView) { override fun bindData(data: ItemClick, payloads: List) { title.text = data.title icon.setImageURI(data.url) itemView.setOnClickListener { Toast.makeText(containerView.context, " click in ViewType position is " + layoutPosition.toString(), Toast.LENGTH_SHORT).show() } icon.tag = data.url addOnClickListener(icon) addOnLongClickListener(button) } } ================================================ FILE: app/src/main/java/com/werb/moretype/complete/Complete.kt ================================================ package com.werb.moretype.complete /** * Created by wanbo on 2017/7/15. */ data class Complete(val name: String, val desc: String, val icon: String, val time: String, val text: String, val image: String, val width: String, val height: String) ================================================ FILE: app/src/main/java/com/werb/moretype/complete/CompleteActivity.kt ================================================ package com.werb.moretype.complete import android.app.Activity import android.content.Intent import android.os.Bundle import android.view.View import androidx.appcompat.app.AppCompatActivity import com.werb.library.MoreAdapter import com.werb.library.link.RegisterItem import com.werb.moretype.R import com.werb.moretype.TitleViewHolder import com.werb.moretype.anim.AnimViewHolder import com.werb.moretype.data.DataServer import com.werb.moretype.main.MainCard import kotlinx.android.synthetic.main.activity_complete.* /** * Created by wanbo on 2017/7/15. */ class CompleteActivity : AppCompatActivity() { private val adapter = MoreAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_complete) toolbar.setNavigationIcon(R.mipmap.ic_close_white_24dp) toolbar.setNavigationOnClickListener { finish() } complete_list.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this) adapter.apply { userSoleRegister() register(RegisterItem(R.layout.item_view_title, TitleViewHolder::class.java)) register(RegisterItem(R.layout.item_view_anim, AnimViewHolder::class.java)) register(RegisterItem(R.layout.item_view_complete, CompleteViewHolder::class.java)) register(RegisterItem(R.layout.item_view_horizontal_list, HorizontalViewHolder::class.java)) renderWithAnimation() .startAnimPosition(1) .attachTo(complete_list) } adapter.loadData(MainCard("Complete Example", "A combination of elegant implementation layouts with refresh and loadMore")) adapter.loadData(DataServer.getCompleteData()) //refresh refresh.setOnRefreshListener { adapter.removeDataFromIndex(1) adapter.loadData(DataServer.getCompleteData()) refresh.isRefreshing = false } //loadMore complete_list.addOnScrollListener(loadMoreListener) } /** [OnLoadMoreListener] this is a sample by use OnScrollListener to achieve loadMore Data*/ private val loadMoreListener = object : OnLoadMoreListener() { override fun isHasMore(): Boolean = true override fun onLoadMore(v: View) { v.postDelayed({ val any = adapter.getData(adapter.itemCount - 1) if (any is Footer) { adapter.removeData(any) isShowFooter = false adapter.loadData(DataServer.getCompleteData()) } }, 500) } override fun showFooter(v: View) { if (!isShowFooter) { isShowFooter = true if (adapter.itemCount > 0 && adapter.getData(adapter.itemCount - 1) !is Footer) { val footer = Footer() adapter.loadData(footer) } } } } companion object { fun startActivity(activity: Activity) { activity.startActivity(Intent(activity, CompleteActivity::class.java)) } } } ================================================ FILE: app/src/main/java/com/werb/moretype/complete/CompleteViewHolder.kt ================================================ package com.werb.moretype.complete import android.text.TextUtils import android.view.LayoutInflater import android.view.View import android.widget.LinearLayout import android.widget.RelativeLayout import android.widget.Toast import androidx.appcompat.widget.AppCompatTextView import com.facebook.drawee.view.SimpleDraweeView import com.werb.library.MoreViewHolder import com.werb.moretype.R import com.werb.moretype.Utils import kotlinx.android.synthetic.main.item_view_complete.* /** * Created by wanbo on 2017/7/15. */ class CompleteViewHolder(values: MutableMap, containerView: View) : MoreViewHolder(values, containerView) { override fun bindData(data: Complete, payloads: List) { icon.setImageURI(data.icon) name.text = data.name desc.text = data.desc time.text = Utils.sendTime(data.time.toLong() * 1000) complete_content_layout.removeAllViews() if(!TextUtils.isEmpty(data.text)){ val currentLayout = LayoutInflater.from(containerView.context).inflate(R.layout.widget_view_complete_text, complete_content_layout, false) as RelativeLayout val text = currentLayout.findViewById(R.id.complete_text) text.text = data.text complete_content_layout.addView(currentLayout) val params = currentLayout.layoutParams as LinearLayout.LayoutParams params.bottomMargin = 15 } if(!TextUtils.isEmpty(data.image)){ val currentLayout = LayoutInflater.from(containerView.context).inflate(R.layout.widget_view_message_in_image, complete_content_layout, false) as RelativeLayout val image = currentLayout.findViewById(R.id.message_in_image) image.setImageURI(data.image) setImgSize(data.width, data.height, image) complete_content_layout.addView(currentLayout) val params = currentLayout.layoutParams as LinearLayout.LayoutParams params.topMargin = 10 } itemView.setOnClickListener { Toast.makeText(containerView.context, " click in ViewType position is " + layoutPosition.toString(), Toast.LENGTH_SHORT).show() } } private fun setImgSize(width: String, height: String, image: SimpleDraweeView) { val size = Utils.getIMImageSize(width.toDouble(), height.toDouble()) val lp = image.layoutParams lp.width = size.width lp.height = size.height image.layoutParams = lp } } ================================================ FILE: app/src/main/java/com/werb/moretype/complete/FootViewHolder.kt ================================================ package com.werb.moretype.complete import android.view.View import com.werb.library.MoreViewHolder import com.werb.moretype.R /** * Created by wanbo on 2017/7/15. */ class FootViewHolder(values: MutableMap, containerView: View) : MoreViewHolder