{"id":3500,"url":"https://github.com/idanatz/OneAdapter","last_synced_at":"2025-08-03T20:32:28.594Z","repository":{"id":64553534,"uuid":"158571396","full_name":"idanatz/OneAdapter","owner":"idanatz","description":"A Viewholderless Adapter for RecyclerView, who supports builtin diffing, states (paging, empty...), events (clicking, swiping...), and more.","archived":false,"fork":false,"pushed_at":"2024-04-21T12:23:43.000Z","size":5160,"stargazers_count":469,"open_issues_count":7,"forks_count":45,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-12-04T01:36:15.019Z","etag":null,"topics":["adapter","android","android-library","android-ui","animations","click-listeners","diffutil","kotlin-android","kotlin-library","multiselect","recyclerview-adapter","recycleview","swipe","viewholders"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/idanatz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-11-21T15:46:40.000Z","updated_at":"2024-10-14T14:43:10.000Z","dependencies_parsed_at":"2022-12-16T06:01:08.743Z","dependency_job_id":"341c3a84-869f-41a5-8c2c-41c745a08748","html_url":"https://github.com/idanatz/OneAdapter","commit_stats":null,"previous_names":["ironsource/oneadapter"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idanatz%2FOneAdapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idanatz%2FOneAdapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idanatz%2FOneAdapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idanatz%2FOneAdapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/idanatz","download_url":"https://codeload.github.com/idanatz/OneAdapter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228567009,"owners_count":17937983,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["adapter","android","android-library","android-ui","animations","click-listeners","diffutil","kotlin-android","kotlin-library","multiselect","recyclerview-adapter","recycleview","swipe","viewholders"],"created_at":"2024-01-05T20:16:43.287Z","updated_at":"2024-12-07T05:30:35.784Z","avatar_url":"https://github.com/idanatz.png","language":"Kotlin","readme":"![Logo](DEV/logo.png)\n\n\u003cbr\u003e\n\n# Announcement: repository changing ownership\nOn the 21th of May 2024 this repository will change ownership and move to the personal account of one of it's maintainers, [Idan Atsmon.](https://github.com/idanatz)\n\n\u003cbr\u003e\n\n# OneAdapter \n[![](https://jitpack.io/v/idanatz/OneAdapter.svg)](https://jitpack.io/#idanatz/OneAdapter)\n[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-OneAdapter-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/7824)\n[![Android Weekly CN](https://img.shields.io/badge/Android%20Weekly%20CN-242-orange.svg)](https://androidweekly.cn/android-dev-weekly-issue-242/)\n\nOneAdapter is made to simplify and enhance the use of the RecyclerView's Adapter while preventing common mistakes. \nWith multiple modules and hooks, you don't have to think about writing an adapter anymore, and just focus on what matters. \n\nFor better understanding what drove me to write this library and what use cases it solves best, please refer to my Medium post:\nhttps://medium.com/@idanatsmon/adapting-your-recyclerview-the-2019-approach-e47edf2fc4f3\n\n## What's new:\nVersion 2.0.0 is out with a brand new Kotlin API!\u003cbr/\u003e\nKotlin is now the first priority of this library and as such comes a full API change, every Module, Hook and State is now created using dedicated DSLs.\u003cbr/\u003e\nCheck the example below or sample project for reference for Kotlin \u0026 Java use.\n\n## Features:\n- Modular approach for more reusable and testable code\n- Built-in support for DiffUtil (using [Diffable](#2-implement-diffable))\n- Optimized performance - internal processing done on a background thread\n- 100% written in Kotlin with Kotlin friendly API using DSLs\n- [Modules:](#modules)\n  - [Item Module](#basic-usage)\n  - [Paging Module](#paging-module)\n  - [Emptiness Module](#emptiness-module)\n  - [Selection Module](#selection-module)\n- [Event Hooks:](#event-hooks)\n  - [Click Event Hook](#click-event-hook)\n  - [Swipe Event Hook](#swipe-event-hook)\n- [Others:](#others)\n  - [First Bind Animation](#first-bind-animation)\n  - [Data Binding](#data-binding)\n\n\n# Include in your project\nRoot build.gradle:\n```groovy\nallprojects {\n repositories {\n    jcenter()\n    maven { url \"https://jitpack.io\" }\n }\n}\n```\nApp build.gradle:\n```groovy\ndependencies {\n  implementation \"com.github.idanatz:OneAdapter:${LATEST_VERSION}\"\n}\n```\n\nNote that library interfaces and API may change slightly while the library design matures.\u003cbr/\u003e\nPlease see the changes in the CHANGELOG file before upgrading.\n\n\n# Preview\n## Example\nYou can try out the [example project](https://github.com/idanatz/OneAdapter/tree/develop/sample) that includes basic and advanced usage in Kotlin.\n## Screenshots\n\u003cimg src=\"DEV/screenshot_1.jpg\" width=\"210\" height=\"420\"\u003e \u003cimg src=\"DEV/screenshot_2.jpg\" width=\"210\" height=\"420\"\u003e \u003cimg src=\"DEV/screenshot_3.jpg\" width=\"210\" height=\"420\"\u003e \u003cimg src=\"DEV/screenshot_4.jpg\" width=\"210\" height=\"420\"\u003e\n\n\u003cbr/\u003e\u003cbr/\u003e\n# Basic Usage\n### 1. Implement Item Module\nItem Modules are used for the creation and binding of all ViewHolders for you. In the onBind method, you will receive as a parameter the model associated with this view and a ViewBinder class that lets you find (and cache) the views defined in the associated layout file.\n\n```kotlin\nclass MessageModule : ItemModule\u003cMessageModel\u003e() {\n    init {\n        config {\n            layoutResource = R.layout.message_model\n        }\n        onBind { model, viewBinder, metadata -\u003e\n            val title = viewBinder.findViewById\u003cTextView\u003e(R.id.title)\n            title.text = model.title\n        }\n        onUnbind { model, viewBinder, metadata -\u003e\n            // unbind logic like stop animation, release webview resources, etc.\n        }\n    }\n}\n```\n### 2. Implement Diffable\nThe Adapter is calculating the difference between its current data and the modified data on a background thread and posting the result to the main thread. In order for this magic to work without writing tons of DiffUtil.Callback, your models need to implement one simple interface:\n```kotlin\nclass MessageModel : Diffable {\n    private val id: Long = 0L\n    private val title: String? = null\n\n    override val uniqueIdentifier: Long = id\n    override fun areContentTheSame(other: Any): Boolean = other is MessageModel \u0026\u0026 title == other.title\n}\n```\n### 3. Attach To OneAdapter \u0026 Use\n```kotlin\nval oneAdapter = OneAdapter(recyclerView) {\n    itemModule += MessageModule()\n} \noneAdapter.setItems(...) \n```\n\n\u003cbr/\u003e\u003cbr/\u003e\n# Advanced Usage\n## Modules\n### Multiple Types\nHave more than one view type? not a problem, just create another ItemModule and attach it to OneAdapter in the same way.\n#### 1. Implement Multiple Item Modules\n```kotlin\nclass MessageModule : ItemModule\u003cMessageModel\u003e { ... }\nclass StoryModule : ItemModule\u003cStoryModel\u003e { ... }\n```\n#### 2. Attach To OneAdapter\n```kotlin\nval oneAdapter = OneAdapter(recyclerView) {\n    itemModule += MessageModule()\n    itemModule += StoryModule()\n    ...\n}\n```\n\n\u003cbr/\u003e\u003cbr/\u003e\n### Paging Module\nPaging Module is used for creating and binding a specific ViewHolder at the end of the list when the Adapter reaches a load more state. The visible threshold configuration is used to indicate how many items before the end of the list the onLoadMore callback should be invoked.\n\u003cbr/\u003e\u003c/br\u003e\n\u003cimg src=\"https://media1.tenor.com/images/275a6daed242e8f0c09b5020d602a2eb/tenor.gif?itemid=15858072\" width=\"210\" height=\"420\"\u003e\n\u003cbr/\u003e\n#### 1. Implement Paging Modules\n```kotlin\nclass PagingModuleImpl : PagingModule() {\n    init {\n        config {\n            layoutResource = R.layout.load_more // can be some spinner animation\n            visibleThreshold = 3 // invoke onLoadMore 3 items before the end\n        }\n        onLoadMore { currentPage -\u003e\n            // place your load more logic here, like asking the ViewModel to load the next page of data.\n        }\n    }\n}\n```\n#### 2. Attach To OneAdapter\n```kotlin\nval oneAdapter = OneAdapter(recyclerView) {\n    // itemModule += ...\n    pagingModule = PagingModuleImpl()\n}\n```\n\n\u003cbr/\u003e\u003cbr/\u003e\n### Emptiness Module\nEmptiness Module is used for creating and binding a specific ViewHolder when the Adapter has no data to render.\n\u003cbr/\u003e\u003c/br\u003e\n\u003cimg src=\"https://media1.tenor.com/images/c653010777465c1c5d115210b7e349e8/tenor.gif?itemid=15858077\" width=\"210\" height=\"420\"\u003e\n\u003cbr/\u003e\n#### 1. Implement Emptiness Modules\n```kotlin\nclass EmptinessModuleImpl : EmptinessModule() {\n    init {\n    \tconfig {\n            layoutResource = R.layout.empty_state\n        }\n        onBind { viewBinder, metadata -\u003e ... }\n        onUnbind { viewBinder, metadata -\u003e ... }\n    }\n}\n```\n#### 2. Attach To OneAdapter\n```kotlin\nval oneAdapter = OneAdapter(recyclerView) {\n    // itemModule += ...\n    emptinessModule = EmptinessModuleImpl()\n}\n```\n\n\u003cbr/\u003e\u003cbr/\u003e\n### Selection Module\nSelection Module is used for enabling single or multiple selection on Items.\n\u003cbr/\u003e\u003c/br\u003e\n\u003cimg src=\"https://media.tenor.com/images/2d6fdae647c6c774ceadeb79d7640e42/tenor.gif\" width=\"210\" height=\"420\"\u003e\n\u003cbr/\u003e\n#### 1. Implement Selection Modules\n```kotlin\nclass ItemSelectionModuleImpl : ItemSelectionModule() {\n    init {\n    \tconfig {\n            selectionType = SelectionType.Multiple // Or SelectionType.Single\n        }\n        onStartSelection {\n            // place your general selection logic here, like changing the toolbar text to indicate the selected count.\n        } \n        onUpdateSelection { selectedCount -\u003e ... }\n        onEndSelection { ... }\n    }\n}\n```\n#### 2. Implement Selection State\n```kotlin\nclass MessageModule : ItemModule\u003cMessageModel\u003e() {\n    init {\n        // config, onBind, etc...\n        \n        states += SelectionState\u003cMessageModel\u003e().apply {\n            config {\n                enabled = true // decide if the selection should be enabled for this model, true by default\n                selectionTrigger = SelectionTrigger.LongClick // decide what trigger the selection, long or regular click\n            }\n            onSelected { model, selected -\u003e\n                // insert your selected logic here. \n                // right after this call you will receive an onBind call in order to reflect your changes on the relevant Item Module.\n            }\n        }\n    }\n}\n```\n#### 3. Attach To OneAdapter\n```kotlin\nval oneAdapter = OneAdapter(recyclerView) {\n    itemModule += MessageModule()\n    itemSelectionModule = ItemSelectionModuleImpl()\n}\n```\n\n\u003cbr/\u003e\u003cbr/\u003e\u003cbr/\u003e\n## Event Hooks\nItem Modules can easily be enhanced with event hooks to get access to common events like clicking or swiping on an item.\n\n### Click Event Hook\nClick Hook can be attached in order to recieve click events on an item.\n\u003cbr/\u003e\u003c/br\u003e\n\u003cimg src=\"https://media1.tenor.com/images/6c6b218f27b250d1c72d98fc64ba895d/tenor.gif?itemid=15858123\" width=\"210\" height=\"420\"\u003e\n\u003cbr/\u003e\n#### 1. Implement Click Event Hook\n```kotlin\nclass MessageModule : ItemModule\u003cMessageModel\u003e() {\n    init {\n        // config, onBind, etc...\n        \n        eventHooks += ClickEventHook\u003cMessageModel\u003e().apply {\n            onClick { model, viewBinder, metadata -\u003e \n                // place your on click logic here. \n            }\n        }\n    }\n}\n```\n#### 2. Attach To OneAdapter\n```kotlin\nval oneAdapter = OneAdapter(recyclerView) {\n    itemModule += MessageModule()\n}\n```\n\n\u003cbr/\u003e\u003cbr/\u003e\n### Swipe Event Hook\nSwipe Hook can be attached in order to receive swiping (during and when completed) events on an item.\n\u003cbr/\u003e\u003c/br\u003e\n\u003cimg src=\"https://media1.tenor.com/images/6628708db23f76d528f451b1d1d8fd4c/tenor.gif?itemid=15858132\" width=\"210\" height=\"420\"\u003e\n\u003cbr/\u003e\n#### 1. Implement Swipe Event Hook\n```kotlin\nclass MessageModule : ItemModule\u003cMessageModel\u003e() {\n    init {\n        // config, onBind, etc...\n        \n        eventHooks += SwipeEventHook\u003cMessageModel\u003e().apply {\n            config {\n                swipeDirection = listOf(SwipeEventHook.SwipeDirection.Start, SwipeEventHook.SwipeDirection.End)\n            }\n            onSwipe { canvas, xAxisOffset, viewBinder -\u003e\n                // draw your swipe UI here.\n                // like painting the canvas red with a delete icon.\n            }\n            onSwipeComplete { model, viewBinder, metadata -\u003e\n                // place your swipe logic here.\n                // like removing an item after it was swiped right.\n            }\n        }\n    }\n}\n```\n#### 2. Attach To OneAdapter\n```kotlin\nval oneAdapter = OneAdapter(recyclerView) {\n    itemModule += MessageModule()\n}\n```\n\n\u003cbr/\u003e\u003cbr/\u003e\u003cbr/\u003e\n## Others\n\n### First Bind Animation\nThe provided Animator will be animated on the first bind of the corresponding ItemModule's models. \n\u003cbr/\u003e\u003c/br\u003e\n\u003cimg src=\"https://media.tenor.com/images/9e9a9a6ad634157460e779fb59fd59eb/tenor.gif\" width=\"210\" height=\"420\"\u003e\n\u003cbr/\u003e\n```kotlin\nclass MessageModule : ItemModule\u003cMessageModel\u003e() {\n    init {\n        config {\n            layoutResource = R.layout.message_model\n            \n            // can be implemented by inflating Animator Xml\n            firstBindAnimation = AnimatorInflater.loadAnimator(this@FirstBindAnimationActivity, R.animator.item_animation_example)\n\t\t\t\n            // or can be implemented by constructing ObjectAnimator\n            firstBindAnimation = ObjectAnimator().apply {\n                propertyName = \"translationX\"\n                setFloatValues(-1080f, 0f)\n                duration = 750\n            }\n        }\n        onBind { model, viewBinder, metadata -\u003e ... }\n    }\n}\n```\n\n### View Binding\nBuilt in support for Android View Binding (https://developer.android.com/topic/libraries/view-binding)\nFull example is provided in the example project.\n```kotlin\nclass MessageModule : ItemModule\u003cMessageModel\u003e() {\n    init {\n        config {\n            layoutResource = R.layout.message_model\n        }\n        onBind { model, viewBinder, _ -\u003e\n            viewBinder.bindings(MessageModelBinding::bind).run {\n                title.text = model.title\n                body.text = model.body\n                Glide.with(viewBinder.rootView).load(model.avatarImageId).into(avatarImage)\n            }\n        }\n    }\n}\n```\n\n### Data Binding\nBuilt in support for Android Data Binding (https://developer.android.com/topic/libraries/data-binding)\nFull example is provided in the example project.\n```kotlin\nclass MessageModule : ItemModule\u003cObservableMessageModel\u003e() {\n    init {\n        config {\n            layoutResource = R.layout.message_model\n        }\n        onBind { model, viewBinder, metadata -\u003e\n            viewBinder.dataBinding?.run {\n                setVariable(BR.messageModel, model)\n                lifecycleOwner = this@DataBindingActivity\n                executePendingBindings()\n            }\n        }\n    }\n}\n```\n\n\u003cbr/\u003e\u003cbr/\u003e\n# License\nCopyright (c) 2019 Idan Atsmon\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","funding_links":[],"categories":["Libraries"],"sub_categories":["GUI"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidanatz%2FOneAdapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fidanatz%2FOneAdapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidanatz%2FOneAdapter/lists"}