{"id":15640632,"url":"https://github.com/sockeqwe/adaptercommands","last_synced_at":"2025-04-30T08:59:33.866Z","repository":{"id":57724807,"uuid":"51451752","full_name":"sockeqwe/AdapterCommands","owner":"sockeqwe","description":"Drop in solution to animate RecyclerView's dataset changes by using command pattern","archived":false,"fork":false,"pushed_at":"2017-05-23T00:14:31.000Z","size":148,"stargazers_count":75,"open_issues_count":0,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-30T08:59:19.561Z","etag":null,"topics":["animation","recyclerview","recyclerview-adapter","recyclerview-item-animation"],"latest_commit_sha":null,"homepage":"http://hannesdorfmann.com/android/adapter-commands","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sockeqwe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-10T15:56:24.000Z","updated_at":"2025-02-23T19:25:15.000Z","dependencies_parsed_at":"2022-09-11T20:10:47.431Z","dependency_job_id":null,"html_url":"https://github.com/sockeqwe/AdapterCommands","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sockeqwe%2FAdapterCommands","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sockeqwe%2FAdapterCommands/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sockeqwe%2FAdapterCommands/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sockeqwe%2FAdapterCommands/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sockeqwe","download_url":"https://codeload.github.com/sockeqwe/AdapterCommands/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251674576,"owners_count":21625644,"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":["animation","recyclerview","recyclerview-adapter","recyclerview-item-animation"],"created_at":"2024-10-03T11:38:53.796Z","updated_at":"2025-04-30T08:59:33.811Z","avatar_url":"https://github.com/sockeqwe.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Deprecated\nThis library is deprecated. Use [DiffUtils](https://developer.android.com/reference/android/support/v7/util/DiffUtil.html) from Android RecyclerView library which does exactly the same as AdapterCommands. AdapterCommands has been developed and released before DiffUtils has been released, however, now that Google has published and is maintaining DiffUtils there is very little reason to prefer this library over DiffUtils.\n\n# AdapterCommands\nDrop in solution to animate RecyclerView's dataset changes by using the `command pattern` for adapters with **not stable ids**.\nRead my [blog post](http://hannesdorfmann.com/android/adapter-commands) for more information.\n\nKeep in mind that the runtime of `DiffCommandsCalculator` is `O(n*m)` (n = number of items in old list, m = number of items in new list).\nSo you better run this on a background thread if your data set contains many items.\n\n##Dependencies\n\n```groovy\ncompile 'com.hannesdorfmann.adaptercommands:adaptercommands:1.0.4'\n```\n\n## How to use\nThere are basically 2 components:\n  - `DiffCommandsCalculator` that calculates the difference from previous data set to the new data set and returns `List\u003cAdapterCommand\u003e`. Please note that `DiffCommandsCalculator` is **not thread safe**. If you need a thread safe instance use `ThreadSafeDiffCommandsCalculator`.\n  - `AdapterCommandProcessor` takes `List\u003cAdapterCommand\u003e` and executes each command to trigger RecyclerView's `ItemAnimator` to run animations.\n\n```java\npublic class MainActivity extends AppCompatActivity {\n\n  @Bind(R.id.recyclerView) RecyclerView recyclerView;\n\n  List\u003cItem\u003e items = new ArrayList\u003cItem\u003e();\n  Random random = new Random();\n  ItemAdapter adapter; // RecyclerView adapter\n  AdapterCommandProcessor commandProcessor;\n  DiffCommandsCalculator\u003cItem\u003e commandsCalculator = new DiffCommandsCalculator\u003cItem\u003e();\n\n  @Override protected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_main);\n    ButterKnife.bind(this);\n\n    adapter = new ItemAdapter(this, items);\n    recyclerView.setAdapter(adapter);\n    recyclerView.setLayoutManager(new GridLayoutManager(this, 4));\n\n    commandProcessor = new AdapterCommandProcessor(adapter);\n  }\n\n  // Called when new items should be displayed in RecyclerView\n  public void setItems(List\u003cItem\u003e newItems){\n    adapter.setItems(newItems);\n    List\u003cAdapterCommand\u003e commands = commandsCalculator.diff(newItems);\n    commandProcessor.execute(commands); // executes commands that triggers animations\n  }\n```\n\n## MVP\nBest practise is to use a `PresentationModel` and `Model-View-Presenter`. See  my [blog post](http://hannesdorfmann.com/android/adapter-commands) for a concrete example.\n\n## Customization\n - comparing items\n `DiffCommandsCalculator` uses standard java's `equals()` method to compare two items (one from old list, one from new list).\n  So you have to override `equals()` and `hashCode()` in your model class (use IDE to generate that):\n  ```java\npublic class Item {\n\n  int id;\n  String text;\n\n  @Override public boolean equals(Object o) {\n    if (this == o) return true;\n    if (o == null || getClass() != o.getClass()) return false;\n\n    Item item = (Item) o;\n\n    return id == item.id;\n  }\n\n  @Override public int hashCode() {\n    return id;\n  }\n ```\n As you might have noticed, we only use `Item.id` for equals. The reason is that if we have this item in old list `item { id = 1, text =\"Foo\"}` and the same item with the same id in the new list `{item { id = 1, text =\"other\"}` we just want to compare this items by the id.\n What here has happened was that the `item.text` has been changed from new list to old list, but we still compare two items just by `item.id` since this is the property we use in `equals()`.\n\n However, when a `item.text` has been changed, we have to detect this too because we have to call `adapter.notifyItemChanged(position)` (`ItemChangedCommand`).\n So we can provide an `ItemChangedDetector` that we can pass as constructor argument to `DiffCommandsCalculator`:\n\n ```java\nclass MyItemChangedDetector implements ItemChangedDetector\u003cItem\u003e() {\n    @Override public boolean hasChanged(Item oldItem, Item newItem) {\n      return !oldItem.text.equals(newItem.text);\n    }\n};\n ```\n and then use it like this:\n ```java\nDiffCommandsCalculator\u003cItem\u003e calculator = new DiffCommandsCalculator\u003c\u003e(new MyItemChangedDetector());\n ```\n\n - We also can specify what exactly should happen on the first time we use `DiffCommandsCalculator` (there is no old list to compare to).\n In this case we either could call `adapter.notifyDatasetChanged()` (`EntireDatasetChangedCommand`) which is the default behaviour or `adapter.notifyItemRangeInserted(0, items.size())` (`ItemRangeInsertedCommand`) which then will run `ItemAnimator` so that items will animate in.\n You can specify the behaviour as constructor parameter `DiffCommandsCalculator(boolean itemRangeInsertedOnFirstDiff)`: `new DiffCommandsCalculator(false)` uses `EntireDatasetChangedCommand` (no animations, equivalent to `new DiffCommandsCalculator()`) whereas `new DiffCommandsCalculator(true)` uses `ItemRangeInsertedCommand` (animations).\n\n\n## License\n```\nCopyright 2016 Hannes Dorfmann\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n   http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsockeqwe%2Fadaptercommands","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsockeqwe%2Fadaptercommands","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsockeqwe%2Fadaptercommands/lists"}