{"id":20086842,"url":"https://github.com/worker8/autoadapter","last_synced_at":"2026-03-07T06:02:58.093Z","repository":{"id":47530048,"uuid":"290935958","full_name":"worker8/AutoAdapter","owner":"worker8","description":null,"archived":false,"fork":false,"pushed_at":"2021-08-25T11:01:49.000Z","size":203,"stargazers_count":10,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-15T02:50:58.958Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/worker8.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-28T02:54:50.000Z","updated_at":"2021-09-05T11:39:36.000Z","dependencies_parsed_at":"2022-09-23T12:21:52.849Z","dependency_job_id":null,"html_url":"https://github.com/worker8/AutoAdapter","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/worker8/AutoAdapter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker8%2FAutoAdapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker8%2FAutoAdapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker8%2FAutoAdapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker8%2FAutoAdapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/worker8","download_url":"https://codeload.github.com/worker8/AutoAdapter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker8%2FAutoAdapter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30208802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-07T05:23:27.321Z","status":"ssl_error","status_checked_at":"2026-03-07T05:00:17.256Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T16:02:44.601Z","updated_at":"2026-03-07T06:02:58.057Z","avatar_url":"https://github.com/worker8.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AutoAdapter\n\n[A small Android library](https://github.com/worker8/AutoAdapter/tree/master/library/src/main/java/com/worker8/auto/adapter/library) for making `RecyclerView` easier to use with different view types. \n\n## Install\n\nbuild.gradle:\n\n```\n    allprojects {\n\trepositories {\n            maven { url 'https://jitpack.io' }\n        }\n    }\n    \n    // if using .kts\n    allprojects {\n        repositories {\n            maven(\"https://jitpack.io\")\n    }\n}\n```\n\napp/build.gradle:\n\n```\ndependencies {\n    implementation 'com.github.worker8:AutoAdapter:-SNAPSHOT'\n    // if using .kts\n    implementation(\"com.github.worker8:AutoAdapter:-SNAPSHOT\")\n}\n```\nReplace `-SNAPSHOT` with the latest version found here to use the latest version: https://github.com/worker8/AutoAdapter/releases\n\n\n## Usage by examples\n\n| Example | After |\n| - | - |\n| [Same View Type Example](https://github.com/worker8/AutoAdapter/blob/master/app/src/main/java/com/worker8/autoadapter/SimpleActivity.kt)  | \u003cimg src=\"https://user-images.githubusercontent.com/1988156/92556121-1808a700-f2a4-11ea-8844-e00cdfbc73f7.png\" width=\"200px\" /\u003e  |\n| [Multiple View Type Example](https://github.com/worker8/AutoAdapter/blob/master/app/src/main/java/com/worker8/autoadapter/MultipleViewTypeActivity.kt) | \u003cimg src=\"https://user-images.githubusercontent.com/1988156/92556136-1f2fb500-f2a4-11ea-957c-236df89631cb.png\" width=\"200px\" /\u003e  |\n| [Parallax Header Detail Page Example](https://github.com/worker8/AutoAdapter/blob/master/app/src/main/java/com/worker8/autoadapter/ParallaxActivity.kt) | \u003cimg src=\"https://user-images.githubusercontent.com/1988156/92556132-1dfe8800-f2a4-11ea-9681-5c46d0474b4f.png\" width=\"200px\" /\u003e |\n\n## How to use\nLet's begin with something simple, let's say we want to build a row like this:\n \n\u003cimg src=\"https://user-images.githubusercontent.com/1988156/92567086-252f9100-f2b8-11ea-939b-ca476660c217.png\" width=\"300px\" /\u003e\n\nWe need 3 steps for this:\n\n### 1. `ListItem`\nFirst, we need to make  `ListItem`.\n \n`ListItem` is an interface describing 3 things: \n\n```\nabstract class ListItem\u003cout T : AutoData\u003e {\n    abstract val data: T\n\n    @get:LayoutRes\n    abstract val layoutResId: Int\n    abstract fun bind(itemView: View)\n}\n```\n\n- the `data`\n- the `layout resource id`\n- the `bind()` method\n\nHere's how to inherit from `ListItem`:\n\n```\nclass NormalRow(override val data: NormalAutoData) : ListItem\u003cNormalAutoData\u003e() { // Explanation i.\n\n    override val layoutResId = R.layout.normal_row // Explanation ii.\n\n    override fun bind(itemView: View, position: Int) { // Explanation iii.\n        itemView.titleText.text = data.name\n        itemView.descText.text = data.desc\n    }\n}\n```\n\n**Explanation:**\n\ni. `NormalAutoData` is a class inherited from `AutoData` (will be described more in the next section). This holds the data itself, in this case, it is the `title #n` and the `desc #n` strings.\n\nii. we pass in the xml resource id to be used for this row.\n\niii. we override the `bind()` method, this method will be called by `RecyclerView` when this row appears in the screen.\n\n### 2. `AutoData`\n\n`AutoData` is an interface for describing an object that has an `id` where it's content can be comparable: \n\n```\ninterface AutoData {\n    val id: Long\n    fun isContentSame(other: AutoData): Boolean\n}\n```\nThis 2 information will be used by the `DiffUtil` of `RecyclerView` to calculate the the changes that have happened to properly animating and reusing the views.\n\nIn this case, our `NormalAutoData` will hold 2 things, the `title #n` and the `desc #n` strings:\n\n```\ndata class NormalAutoData(override val id: Long, val name: String, val desc: String) : AutoData { // i, ii\n    override fun isContentSame(other: AutoData): Boolean { // iii\n        return this == other\n    }\n}\n```\n\n**Explanation:**\n\ni. the `id` will be used by [RecyclerView.Adapter#getItemId()](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#getItemId(int)) to identify an item.\n\nii. if there is no `stable id`, [`NO_ID`](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView#NO_ID) can be used\n\niii. we override the `isContentSame()` method for comparing the content, this will be used by the [`DiffUtil.ItemCallback`](https://developer.android.com/reference/androidx/recyclerview/widget/DiffUtil.ItemCallback) to calculate the difference.\n- _refer to [this doc](https://developer.android.com/reference/androidx/recyclerview/widget/ListAdapter) if you are not familiar with `RecyclerView.ListAdapter`._\n\n### 3. `AutoAdapter`\nAfter making the `ListItem` and `AutoData`, we can finally make the list!\n\n`AutoAdapter` inherits from [ListAdapter](https://developer.android.com/reference/androidx/recyclerview/widget/ListAdapter). When using `AutoAdapter`, it will automatically calculate the view types and position offset, so there is no need to write your own Adapter at all even for use cases with multiple different view types.\n\nTo setup `AutoAdapter`, simply make an instance in the activity:\n\n```kotlin\nfun onCreate() {\n    val adapter = AutoAdapter()\n    recyclerView.adapter = adapter\n}\n```\n\nSince `AutoAdapter` inherits from `RecyclerView.ListAdapter`, we use the same API `adapter.submitList(list)` to populate the list:\n\n```diff\nfun onCreate() {\n    val adapter = AutoAdapter()\n    recyclerView.adapter = adapter\n+   val list = mutableListOf\u003cListItem\u003cAutoData\u003e\u003e()\n+   list.add(NormalRow(NormalAutoData(NO_ID, \"title #0\", \"desc #0\")))\n+   list.add(NormalRow(NormalAutoData(NO_ID, \"title #1\", \"desc #1\")))\n+   list.add(NormalRow(NormalAutoData(NO_ID, \"title #2\", \"desc #2\")))\n+   adapter.submitList(list)\n}\n```\n\nTada :tada: , now you will get this:\n\n\u003cimg src=\"https://user-images.githubusercontent.com/1988156/92567086-252f9100-f2b8-11ea-939b-ca476660c217.png\" width=\"300px\" /\u003e\n\nTo see this in action, check out the example: [SimpleActivity.kt](https://github.com/worker8/AutoAdapter/blob/master/app/src/main/java/com/worker8/autoadapter/SimpleActivity.kt)\n\n### Multiple View Types\nThis section describes how to use `AutoAdapter` with multiple view types. \n\nLet's say we want to build a `Header` like below:\n\n\u003cimg src=\"https://user-images.githubusercontent.com/1988156/92580845-da1e7980-f2c9-11ea-8b61-138f555b4d9f.png\" width=\"300px\" /\u003e\n\nThe changes we need to make from the previous basic example above :point_up: is actually quite minimal.\nWe need to follow the same steps as above, first we need to create `HeaderRow`:\n\n```\nclass HeaderRow(private val title: String) :\n    ListItem\u003cNoAutoData\u003e() {\n\n    override val layoutResId = R.layout.header_row\n\n    override val data = NoAutoData()\n\n    override fun bind(itemView: View, position: Int) {\n        itemView.headerText.text = title\n    }\n}\n```\n\nIn this case I'm using `NoAutoData`, because the `HeaderRow` is static and won't need to change once it's initialized:\n\n```\nclass NoAutoData(override val id: Long = -1) : AutoData {\n    override fun isContentSame(other: AutoData): Boolean {\n        return true\n    }\n}\n```\n\nAfter making the `HeaderRow`, we can simply add it to the `list`:\n\n```diff\nfun onCreate() {\n    val adapter = AutoAdapter()\n    recyclerView.adapter = adapter\n    val list = mutableListOf\u003cListItem\u003cAutoData\u003e\u003e()\n+   list.add(HeaderRow(\"F A K E   N E W S\")\n    list.add(NormalRow(NormalAutoData(NO_ID, \"title #0\", \"desc #0\")))\n    list.add(NormalRow(NormalAutoData(NO_ID, \"title #1\", \"desc #1\")))\n    list.add(NormalRow(NormalAutoData(NO_ID, \"title #2\", \"desc #2\")))\n    adapter.submitList(list)\n}\n```\n\nFor a more thorough example, refer to [Multiple View Type Example](https://github.com/worker8/AutoAdapter/blob/master/app/src/main/java/com/worker8/autoadapter/MultipleViewTypeActivity.kt).\n\n### HasStableId\n\n`AutoAdapter` supports [RecyclerView.Adapter#setHasStableIds(boolean)\n](https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#setHasStableIds(boolean))\n\nWhen making an instance of `AutoAdapter`, you can pass in a boolean to indicate this flag:\n\n```\nval adapter = AutoAdapter(hasStableIds = true) // default is false\n```\n\nThen, pass in a unique id to the data:\n\n```\nlist.add(NormalRow(\n    NormalAutoData(id = uniqueId, \"title\", \"desc\")\n))\n```\n\nAfter enablding `hasStablIds = true`, `RecyclerView` can optimize the animation when changes happen and can prevent blinking issue.\n\n## License\n\n```\nThe MIT License (MIT)\n\nCopyright (c) 2020 Tan Jun Rong\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworker8%2Fautoadapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fworker8%2Fautoadapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworker8%2Fautoadapter/lists"}