├── .gitignore ├── APACHE-LICENSE-2.0.txt ├── README.md ├── pom.xml └── src ├── main └── java │ └── com │ └── madgag │ └── android │ └── listviews │ ├── ReflectiveHolderFactory.java │ ├── ViewCreator.java │ ├── ViewFactory.java │ ├── ViewHolder.java │ ├── ViewHolderFactory.java │ ├── ViewHoldingListAdapter.java │ └── ViewInflator.java └── test └── java └── com └── madgag └── android └── listviews └── ReflectiveHolderFactoryTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | *~ 3 | **pom.xml.releaseBackup 4 | release.properties 5 | .classpath 6 | .project 7 | .settings 8 | *.iws 9 | *.ipr 10 | *.iml 11 | 12 | -------------------------------------------------------------------------------- /APACHE-LICENSE-2.0.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | The aim is to reduce the boilerplate around writing your own ListAdapter class. With the library, you define two smaller classes, a 2 | ViewHolderFactory and a ViewHolder implementation. 3 | 4 | Here's how you would set the adapter on your listView: 5 | 6 | ```java 7 | listView.setAdapter(new ViewHoldingListAdapter(fooList, viewInflatorFor(context, foo_list_item), new ViewHolderFactory() { 8 | public ViewHolder createViewHolderFor(View view) { 9 | return new FooViewHolder(view); 10 | } 11 | })); 12 | ``` 13 | 14 | Your FooViewHolder just does the job of the ViewHolder - holds the references to the required Views, and populates those views with the relevant 15 | fields from you Foo: 16 | 17 | ```java 18 | public class FooViewHolder implements ViewHolder { 19 | private final TextView fooName,fooDate; 20 | private final ImageView icon; 21 | 22 | public FooViewHolder(View v) { 23 | this.avatarSession = avatarSession; 24 | fooName = (TextView) v.findViewById(R.id.foo_item_name); 25 | fooDate = (TextView) v.findViewById(R.id.foo_item_date); 26 | icon = (ImageView) v.findViewById(R.id.foo_item_icon); 27 | } 28 | 29 | public void updateViewFor(Foo foo) { 30 | fooName.setText(foo.getName()); 31 | fooDate.setText(foo.getTimeString()); 32 | icon.setImageDrawable(iconBitmap); 33 | } 34 | } 35 | ``` -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 4.0.0 20 | 21 | org.sonatype.oss 22 | oss-parent 23 | 7 24 | 25 | com.madgag 26 | android-viewholder-listviews 27 | 0.8-SNAPSHOT 28 | jar 29 | Android ViewHolder ListViews 30 | Small library around using the 'ViewHolder' pattern with ListViews 31 | https://github.com/rtyley/android-viewholder-listviews 32 | 33 | UTF-8 34 | 2.2.1 35 | 36 | 37 | 38 | Apache V2 39 | http://www.apache.org/licenses/LICENSE-2.0 40 | repo 41 | 42 | 43 | 44 | scm:git:git@github.com:rtyley/android-viewholder-listviews.git 45 | scm:git:git@github.com:rtyley/android-viewholder-listviews.git 46 | git@github.com:rtyley/android-viewholder-listviews.git 47 | 48 | 49 | 50 | roberto 51 | Roberto Tyley 52 | 0 53 | https://github.com/rtyley 54 | 55 | 56 | 57 | 58 | 59 | org.apache.maven.plugins 60 | maven-compiler-plugin 61 | 62 | 1.5 63 | 1.5 64 | UTF-8 65 | 66 | 67 | 68 | 69 | 70 | 71 | com.google.android 72 | android 73 | provided 74 | ${android.version} 75 | 76 | 77 | junit 78 | junit 79 | 4.8 80 | test 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /src/main/java/com/madgag/android/listviews/ReflectiveHolderFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Kevin Sawicki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.madgag.android.listviews; 18 | 19 | import android.view.View; 20 | 21 | import java.lang.reflect.Constructor; 22 | import java.lang.reflect.InvocationTargetException; 23 | import java.lang.reflect.Modifier; 24 | 25 | /** 26 | * {@link ViewHolderFactory} that uses reflection to create new 27 | * {@link ViewHolder} instances with the given parameters that are injected into 28 | * each {@link ViewHolder} created. 29 | * 30 | * @param 31 | * model class that views are bound to 32 | */ 33 | public class ReflectiveHolderFactory implements ViewHolderFactory { 34 | 35 | private final Class> holderClass; 36 | 37 | public static ReflectiveHolderFactory reflectiveFactoryFor( 38 | Class> holderClass, 39 | Object... args) { 40 | return new ReflectiveHolderFactory(holderClass, args); 41 | } 42 | 43 | private static Constructor findMatch(final Class clazz, 44 | final Class[] types) { 45 | Constructor[] candidates = clazz.getConstructors(); 46 | for (Constructor candidate : candidates) { 47 | Class[] params = candidate.getParameterTypes(); 48 | if (params.length != types.length) 49 | continue; 50 | boolean match = true; 51 | for (int i = 0; i < types.length; i++) 52 | if (!isMatch(params[i], types[i])) { 53 | match = false; 54 | break; 55 | } 56 | if (match) 57 | return candidate; 58 | } 59 | return null; 60 | } 61 | 62 | private static boolean isMatch(Class constructorParam, Class argument) { 63 | if (constructorParam.isPrimitive()) 64 | if (Integer.TYPE.equals(constructorParam)) 65 | constructorParam = Integer.class; 66 | else if (Double.TYPE.equals(constructorParam)) 67 | constructorParam = Double.class; 68 | else if (Short.TYPE.equals(constructorParam)) 69 | constructorParam = Short.class; 70 | else if (Long.TYPE.equals(constructorParam)) 71 | constructorParam = Long.class; 72 | else if (Float.TYPE.equals(constructorParam)) 73 | constructorParam = Float.class; 74 | else if (Byte.TYPE.equals(constructorParam)) 75 | constructorParam = Byte.class; 76 | else if (Character.TYPE.equals(constructorParam)) 77 | constructorParam = Character.class; 78 | else if (Boolean.TYPE.equals(constructorParam)) 79 | constructorParam = Boolean.class; 80 | return constructorParam.isAssignableFrom(argument); 81 | } 82 | 83 | private final Constructor> constructor; 84 | 85 | private final Object[] args; 86 | 87 | /** 88 | * Create holder factory for constructor and arguments 89 | * 90 | * @param holderClass 91 | * @param args 92 | * @throws RuntimeException 93 | */ 94 | @SuppressWarnings("unchecked") 95 | private ReflectiveHolderFactory(final Class> holderClass, 96 | Object... args) { 97 | this.holderClass = holderClass; 98 | if (args == null) 99 | args = new Object[0]; 100 | 101 | if (holderClass.isMemberClass() && !Modifier.isStatic(holderClass.getModifiers())) 102 | throw new IllegalArgumentException( 103 | "Non-static member classes not supported"); 104 | 105 | Class[] types = new Class[args.length + 1]; 106 | // First argument must be the view itself 107 | types[0] = View.class; 108 | for (int i = 0; i < args.length; i++) 109 | types[i + 1] = args[i].getClass(); 110 | 111 | constructor = (Constructor>) findMatch(holderClass, 112 | types); 113 | if (constructor == null) 114 | throw new IllegalArgumentException( 115 | "No constructor found to bind arguments to"); 116 | this.args = new Object[args.length + 1]; 117 | System.arraycopy(args, 0, this.args, 1, args.length); 118 | } 119 | 120 | public ViewHolder createViewHolderFor(final View view) { 121 | try { 122 | args[0] = view; 123 | return constructor.newInstance(args); 124 | } catch (InstantiationException e) { 125 | throw new IllegalArgumentException(e); 126 | } catch (IllegalAccessException e) { 127 | throw new IllegalArgumentException(e); 128 | } catch (InvocationTargetException e) { 129 | throw new IllegalArgumentException(e); 130 | } 131 | } 132 | 133 | public Class> getHolderClass() { 134 | return holderClass; 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/main/java/com/madgag/android/listviews/ViewCreator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Roberto Tyley 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.madgag.android.listviews; 18 | 19 | import android.view.View; 20 | 21 | public interface ViewCreator { 22 | View createBlankView(); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/madgag/android/listviews/ViewFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Roberto Tyley 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.madgag.android.listviews; 18 | 19 | import android.view.View; 20 | 21 | public class ViewFactory { 22 | 23 | private final ViewCreator creator; 24 | private final ViewHolderFactory viewHolderFactory; 25 | 26 | public ViewFactory(ViewCreator creator, ViewHolderFactory viewHolderFactory) { 27 | this.creator = creator; 28 | this.viewHolderFactory = viewHolderFactory; 29 | } 30 | 31 | 32 | public View getView(View view, T item) { 33 | ViewHolder holder; 34 | 35 | if (view==null) { 36 | view = creator.createBlankView(); 37 | holder = viewHolderFactory.createViewHolderFor(view); 38 | view.setTag(holder); 39 | } else { 40 | holder = (ViewHolder) view.getTag(); 41 | 42 | // validate holder is of correct type - could it have been produced by our factory? 43 | if (!holder.getClass().equals(viewHolderFactory.getHolderClass())) { 44 | view = creator.createBlankView(); 45 | holder = viewHolderFactory.createViewHolderFor(view); 46 | view.setTag(holder); 47 | } 48 | } 49 | 50 | holder.updateViewFor(item); 51 | return view; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/madgag/android/listviews/ViewHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Roberto Tyley 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.madgag.android.listviews; 18 | 19 | 20 | public interface ViewHolder { 21 | 22 | public void updateViewFor(T item); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/madgag/android/listviews/ViewHolderFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Roberto Tyley 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.madgag.android.listviews; 18 | 19 | 20 | import android.view.View; 21 | 22 | public interface ViewHolderFactory { 23 | 24 | ViewHolder createViewHolderFor(View view); 25 | 26 | Class> getHolderClass(); 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/madgag/android/listviews/ViewHoldingListAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Roberto Tyley 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.madgag.android.listviews; 18 | 19 | import android.view.View; 20 | import android.view.ViewGroup; 21 | import android.widget.BaseAdapter; 22 | 23 | import java.util.Arrays; 24 | import java.util.List; 25 | 26 | import static java.util.Arrays.asList; 27 | 28 | public class ViewHoldingListAdapter extends BaseAdapter { 29 | 30 | private List itemList; 31 | private final ViewFactory viewFactory, dropDownViewFactory; 32 | 33 | public ViewHoldingListAdapter(List itemList, ViewFactory viewFactory, ViewFactory dropDownViewFactory) { 34 | this.itemList = itemList; 35 | this.viewFactory = viewFactory; 36 | this.dropDownViewFactory = dropDownViewFactory; 37 | } 38 | 39 | public ViewHoldingListAdapter(List itemList, ViewFactory viewFactory) { 40 | this(itemList, viewFactory, viewFactory); 41 | } 42 | 43 | public ViewHoldingListAdapter(List itemList, ViewCreator c, ViewHolderFactory vhf) { 44 | this(itemList, new ViewFactory(c, vhf)); 45 | } 46 | 47 | public ViewHoldingListAdapter(T[] items, ViewFactory viewFactory) { 48 | this(asList(items), viewFactory); 49 | } 50 | 51 | public ViewHoldingListAdapter(T[] items, ViewCreator c, ViewHolderFactory vhf) { 52 | this(asList(items), c, vhf); 53 | } 54 | 55 | @Override 56 | public boolean hasStableIds() { 57 | return true; 58 | } 59 | 60 | public void setList(List itemList) { 61 | this.itemList=itemList; 62 | notifyDataSetChanged(); 63 | } 64 | 65 | public int getCount() { 66 | return itemList.size(); 67 | } 68 | 69 | public T getItem(int index) { 70 | return itemList.get(index); 71 | } 72 | 73 | public long getItemId(int i) { 74 | return getItem(i).hashCode(); 75 | } 76 | 77 | public View getView(int index, View convertView, ViewGroup parent) { 78 | return viewFactory.getView(convertView, itemList.get(index)); 79 | } 80 | 81 | /** 82 | * Declared by SpinnerAdapter, this method allows your spinner to show 83 | * different views in your drop down vs the 'closed' spinned box. 84 | */ 85 | public View getDropDownView(int index, View convertView, ViewGroup parent) { 86 | return dropDownViewFactory.getView(convertView, itemList.get(index)); 87 | } 88 | } -------------------------------------------------------------------------------- /src/main/java/com/madgag/android/listviews/ViewInflator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Roberto Tyley 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.madgag.android.listviews; 18 | 19 | 20 | import android.content.Context; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | 24 | public class ViewInflator implements ViewCreator { 25 | private final LayoutInflater inflater; 26 | private final int resId; 27 | 28 | public ViewInflator(LayoutInflater inflater, int resId) { 29 | this.inflater = inflater; 30 | this.resId = resId; 31 | } 32 | 33 | public View createBlankView() { 34 | return inflater.inflate(resId, null); 35 | } 36 | 37 | public static ViewInflator viewInflatorFor(Context context, int resId) { 38 | return new ViewInflator(LayoutInflater.from(context), resId); 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/com/madgag/android/listviews/ReflectiveHolderFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Kevin Sawicki 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.madgag.android.listviews; 17 | 18 | import static com.madgag.android.listviews.ReflectiveHolderFactory.reflectiveFactoryFor; 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertNotNull; 21 | import android.view.View; 22 | 23 | import org.junit.Test; 24 | 25 | /** 26 | * Unit tests of {@link ReflectiveHolderFactory} 27 | */ 28 | public class ReflectiveHolderFactoryTest { 29 | 30 | private static class StaticViewHolder implements ViewHolder { 31 | 32 | public StaticViewHolder(View view) { 33 | } 34 | 35 | public void updateViewFor(String item) { 36 | } 37 | } 38 | 39 | private class MemberViewHolder implements ViewHolder { 40 | 41 | public MemberViewHolder(View view) { 42 | } 43 | 44 | public void updateViewFor(String item) { 45 | } 46 | } 47 | 48 | private static class StringViewHolder implements ViewHolder { 49 | 50 | final String argument; 51 | 52 | public StringViewHolder(View view, String argument) { 53 | this.argument = argument; 54 | } 55 | 56 | public void updateViewFor(String item) { 57 | } 58 | } 59 | 60 | private static class PrimitiveIntHolder implements ViewHolder { 61 | 62 | final int argument; 63 | 64 | public PrimitiveIntHolder(View view, int argument) { 65 | this.argument = argument; 66 | } 67 | 68 | public void updateViewFor(String item) { 69 | } 70 | } 71 | 72 | private static class IntegerHolder implements ViewHolder { 73 | 74 | final Integer argument; 75 | 76 | public IntegerHolder(View view, Integer argument) { 77 | this.argument = argument; 78 | } 79 | 80 | public void updateViewFor(String item) { 81 | } 82 | } 83 | 84 | private static class TwoStringViewHolder implements ViewHolder { 85 | 86 | final String argument1; 87 | 88 | final CharSequence argument2; 89 | 90 | public TwoStringViewHolder(View view, String argument1, 91 | CharSequence argument2) { 92 | this.argument1 = argument1; 93 | this.argument2 = argument2; 94 | } 95 | 96 | public void updateViewFor(String item) { 97 | } 98 | } 99 | 100 | /** 101 | * Create holder for class with single default constructor that takes a view 102 | */ 103 | @Test 104 | public void defaultConstructor() { 105 | ReflectiveHolderFactory factory = reflectiveFactoryFor( 106 | StaticViewHolder.class, (Object[]) null); 107 | ViewHolder holder = factory.createViewHolderFor(null); 108 | assertNotNull(holder); 109 | } 110 | 111 | /** 112 | * Create holder for non-static member class 113 | */ 114 | @Test(expected = IllegalArgumentException.class) 115 | public void defaultConstructorNonStaticMemberClass() { 116 | reflectiveFactoryFor(MemberViewHolder.class); 117 | } 118 | 119 | /** 120 | * Create holder for non-static member class 121 | */ 122 | @Test(expected = IllegalArgumentException.class) 123 | public void noConstructorMatchMissingParam() { 124 | ReflectiveHolderFactory factory = reflectiveFactoryFor( 125 | StringViewHolder.class); 126 | factory.createViewHolderFor(null); 127 | } 128 | 129 | /** 130 | * Create holder for non-static member class 131 | */ 132 | @Test(expected = IllegalArgumentException.class) 133 | public void noConstructorMatchInvalidParam() { 134 | ReflectiveHolderFactory factory = reflectiveFactoryFor( 135 | StringViewHolder.class, new Object()); 136 | factory.createViewHolderFor(null); 137 | } 138 | 139 | /** 140 | * Create holder for class that takes a String constructor argument 141 | */ 142 | @Test 143 | public void constructorWithSingleArgument() { 144 | ReflectiveHolderFactory factory = reflectiveFactoryFor( 145 | StringViewHolder.class, "abcd"); 146 | ViewHolder holder = factory.createViewHolderFor(null); 147 | assertNotNull(holder); 148 | assertEquals("abcd", ((StringViewHolder) holder).argument); 149 | } 150 | 151 | /** 152 | * Create holder for class that takes a String and CharSequence constructor 153 | * argument 154 | */ 155 | @Test 156 | public void constructorWithMultipleArgument() { 157 | ReflectiveHolderFactory factory = reflectiveFactoryFor( 158 | TwoStringViewHolder.class, "a1", "b2"); 159 | ViewHolder holder = factory.createViewHolderFor(null); 160 | assertNotNull(holder); 161 | assertEquals("a1", ((TwoStringViewHolder) holder).argument1); 162 | assertEquals("b2", ((TwoStringViewHolder) holder).argument2); 163 | } 164 | 165 | /** 166 | * Create holder for class that takes an int parameter 167 | */ 168 | @Test 169 | public void constructorWithPrimitiveInt() { 170 | ReflectiveHolderFactory factory = reflectiveFactoryFor( 171 | PrimitiveIntHolder.class, 1); 172 | ViewHolder holder = factory.createViewHolderFor(null); 173 | assertNotNull(holder); 174 | assertEquals(1, ((PrimitiveIntHolder) holder).argument); 175 | } 176 | 177 | /** 178 | * Create holder for class that takes an {@link Integer} parameter 179 | */ 180 | @Test 181 | public void constructorWithInteger() { 182 | ReflectiveHolderFactory factory = reflectiveFactoryFor( 183 | IntegerHolder.class, 1); 184 | ViewHolder holder = factory.createViewHolderFor(null); 185 | assertNotNull(holder); 186 | assertEquals(Integer.valueOf(1), ((IntegerHolder) holder).argument); 187 | } 188 | } 189 | --------------------------------------------------------------------------------