{"id":17974889,"url":"https://github.com/wingjay/lego","last_synced_at":"2025-03-25T14:32:20.676Z","repository":{"id":88655342,"uuid":"134679060","full_name":"wingjay/Lego","owner":"wingjay","description":"A powerful Android library for building complex RecyclerView. Building a list is as simple as playing Lego game. List\u003cModel\u003e =\u003eLego=\u003e List\u003cView\u003e. 像搭积木一样构建你的RecyclerView列表。","archived":false,"fork":false,"pushed_at":"2019-06-25T08:33:45.000Z","size":455,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-20T12:51:38.898Z","etag":null,"topics":["android","annotation","recyclerview-adapter","recyclerview-multi-type"],"latest_commit_sha":null,"homepage":"https://wingjay.github.io/Lego","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/wingjay.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-24T07:37:05.000Z","updated_at":"2023-07-31T07:00:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"95b901f1-cd71-4cd6-ba2a-219efca19a8a","html_url":"https://github.com/wingjay/Lego","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wingjay%2FLego","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wingjay%2FLego/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wingjay%2FLego/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wingjay%2FLego/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wingjay","download_url":"https://codeload.github.com/wingjay/Lego/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245480565,"owners_count":20622335,"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":["android","annotation","recyclerview-adapter","recyclerview-multi-type"],"created_at":"2024-10-29T17:16:24.732Z","updated_at":"2025-03-25T14:32:20.659Z","avatar_url":"https://github.com/wingjay.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Lego\nA powerful Android library for building complex RecyclerView. Building a list is as simple as playing Lego game. \n\nList\u0026lt;Model\u003e =\u003eLego=\u003e List\u0026lt;View\u003e. 像搭积木一样构建你的RecyclerView列表。\n\n[中文介绍](https://github.com/wingjay/Lego/blob/master/README_CN.md)\n\n# What Lego does\nWhen you need to build a RecyclerView which contains `different types of ViewHolder`, you may always need to maintain a huge Adapter and calculate which type should be displayed on each position.\n\nNow, forget those different types and viewTypeOfPosition() calculation in your terriable Adapter. \n\nWhen using `Lego`, you just need to do:\n```\n1. Build a empty List\u003cObject\u003e;\n2. Put all your data into this List\u003cObject\u003e;\n3. Take this List\u003cObject\u003e to RecyclerView. \n```\n\nIt's done! RecyclerView will auto find the ViewHolder based on List\u003cObject\u003e and display.\n\n## How to use Lego\nFor example, we have three viewHolders to display in a RecyclerView: `AppleViewHolder`, `OrangeViewHolder`, `BananaViewHolder`. Their own data class are `Apple`, `Orange`, `Banana`.\n\nYou have two ways to build with `Lego`.\n\n## 1. Use String Id to identify each ViewHolder\n```\n@LegoViewHolder(id = \"apple_view_holder\")\nclass AppleViewHolder implements ILegoViewHolder {\n    @Override\n    public View initView(ViewGroup parent) { ... }\n\n    @Override\n    public void bindData(Object data, int position, @Nullable Bundle argument) { ... }\n}\n\n@LegoViewHolder(id = \"orange_view_holder\")\nclass OrangeViewHolder implements ILegoViewHolder {\n    @Override\n    public View initView(ViewGroup parent) { ... }\n\n    @Override\n    public void bindData(Object data, int position, @Nullable Bundle argument) { ... }\n}\n\n@LegoViewHolder(id = \"banana_view_holder\")\nclass BananaViewHolder implements ILegoViewHolder {\n    @Override\n    public View initView(ViewGroup parent) { ... }\n\n    @Override\n    public void bindData(Object data, int position, @Nullable Bundle argument) { ... }\n}\n```\n\nThat's it! In your RecyclerView, you can do as below:\n\n```\nRecyclerView recyclerView = findViewById(R.id.recycler_view);\n\n// 1. create a LegoRecyclerAdapter\nLegoRecyclerAdapter adapter = new LegoRecyclerAdapter();\n\n// 2. create a data list of Object\nList\u003cObject\u003e data = new ArrayList\u003c\u003e();\n\n// 3. add LegoItem into data\ndata.add(LegoItem.create(\"apple_view_holder\"));\ndata.add(LegoItem.create(\"orange_view_holder\"));\ndata.add(LegoItem.create(\"banana_view_holder\"));\n\n// 4. It's done\nadapter.swapData(data);\nrecyclerView.setAdapter(adapter)\n```\n\nRun this code, the RecyclerView will show these three viewHolders, try it or [run our sample](https://github.com/wingjay/Lego/tree/master/sample)!\n\n## 2. Simpler Way: use Data object to identify each ViewHolder\n```\n@LegoViewHolder(bean = Apple.class)\nclass AppleViewHolder implements ILegoViewHolder {\n    @Override\n    public View initView(ViewGroup parent) { ... }\n\n    @Override\n    public void bindData(Object data, int position, @Nullable Bundle argument) { \n      Apple apple = (Apple) data;\n      ...\n    }\n}\n\n@LegoViewHolder(bean = Orange.class)\nclass OrangeViewHolder implements ILegoViewHolder {\n    @Override\n    public View initView(ViewGroup parent) { ... }\n\n    @Override\n    public void bindData(Object data, int position, @Nullable Bundle argument) { \n      Orange orange = (Orange) data;\n      ...\n    }\n}\n\n@LegoViewHolder(bean = Banana.class)\nclass BananaViewHolder implements ILegoViewHolder {\n    @Override\n    public View initView(ViewGroup parent) { ... }\n\n    @Override\n    public void bindData(Object data, int position, @Nullable Bundle argument) {   \n      Banana orange = (Banana) data;\n      ...\n    }\n}\n```\n\nIn your RecyclerView, do as below:\n```\nRecyclerView recyclerView = findViewById(R.id.recycler_view);\n\n// 1. create a LegoRecyclerAdapter\nLegoRecyclerAdapter adapter = new LegoRecyclerAdapter();\n\n// 2. create a data list of Object\nList\u003cObject\u003e data = new ArrayList\u003c\u003e();\n\n// 3. add LegoItem into data\ndata.add(new Apple());\ndata.add(new Orange());\ndata.add(new Banana());\n\n// 4. It's done\nadapter.swapData(data);\nrecyclerView.setAdapter(adapter)\n```\n\nBingo! This will display same three ViewHolders as above, but this way is easier to use.\n\n## Get LegoViewHolder instance\n```\nadapter.setOnLegoViewHolderListener(new OnLegoViewHolderListener() {\n    @Override\n    public void onCreate(@NonNull ILegoViewHolder viewHolder) {\n        if (viewHolder instanceof AppleViewHolder) {\n            makeToast(\"Create AppleViewHolder\");\n        } else if (viewHolder instanceof OrangeViewHolder) {\n            makeToast(\"Create OrangeViewHolder\");\n        }\n    }\n});\n```\n\n## ViewHolder cache\nIf you know your page need at least 2 AppleViewHolder and 3 OrangeViewHolder, you can tell Lego to generate ViewHolder instance and inflate. When data(maybe from server) arrives, you can display immediately and no need to do the ViewHolder Inflate.\n```\nLegoCache legoCache =\n    new LegoCache.Builder(recyclerView)\n    .cache(OrangeViewHolder.class, 2)\n    .cache(AppleViewHolder.class, 2)\n    .build();\nadapter.setLegoCache(legoCache);\n```\n\n\n# Sample\nHere is our [sample code](https://github.com/wingjay/Lego/tree/master/sample) you can try.\n\n# Install\n```\nimplementation 'com.wingjay.lego:library:0.9.2'\nannotationProcessor 'com.wingjay.lego:lego-processor:0.9.2'\n```\n\nif you use Kotlin, use as below\n```\nimplementation 'com.wingjay.lego:library:0.9.2'\nkapt 'com.wingjay.lego:lego-processor:0.9.2'\n```\n\n# Proguard\n```\n-keep @interface com.wingjay.lego.LegoBean\n-keep @interface com.wingjay.lego.LegoViewHolder\n\n-keep @com.wingjay.lego.LegoBean class *\n-keepclassmembers class * {\n    @com.wingjay.lego.LegoBean *;\n}\n-keep @com.wingjay.lego.LegoViewHolder class *\n-keepclassmembers class * {\n    @com.wingjay.lego.LegoViewHolder *;\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwingjay%2Flego","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwingjay%2Flego","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwingjay%2Flego/lists"}