├── .gitignore ├── AndroidManifest.xml ├── LICENSE.txt ├── README.md ├── libs └── android-support-v4.jar ├── multiitem.png ├── proguard-project.txt ├── project.properties ├── res ├── drawable-hdpi │ └── ic_launcher.png ├── drawable-mdpi │ └── ic_launcher.png ├── drawable-xhdpi │ └── ic_launcher.png ├── drawable-xxhdpi │ └── ic_launcher.png ├── drawable │ ├── above_shadow.xml │ └── list_selector.xml ├── layout │ ├── activity_demo.xml │ └── include_list_item_contact.xml ├── menu │ └── demo.xml ├── values-land │ └── integers.xml ├── values-sw600dp-land │ └── integers.xml ├── values-sw600dp │ └── integers.xml ├── values-v11 │ └── styles.xml ├── values-v14 │ └── styles.xml └── values │ ├── colors.xml │ ├── dimen.xml │ ├── integers.xml │ ├── strings.xml │ └── styles.xml └── src └── com └── sothree └── multiitemrowlistadapter ├── MultiItemRowListAdapter.java └── demo └── DemoActivity.java /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | 18 | # Eclipse project files 19 | .classpath 20 | .project 21 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 12 | 13 | 14 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Android Multi Item Row ListAdapter 2 | ================================== 3 | ### An Easy Way to Make Your ListView's Look Amazing on Tablets 4 | With the launch of the tablet version of [Umano](http://umanoapp.com) [Android app](https://play.google.com/store/apps/details?id=com.sothree.umano) we decided to open-source another component, which allows you to very quickly make your ListViews and ListActivities look amazing on tablets and phablets by placing multiple items of your ListAdapter in each row in a ListView. Umano Team <3 Open Source. 5 | 6 | As seen in Umano ([http://umanoapp.com](http://umanoapp.com)): 7 | 8 | ![MultiItemRowListAdapter](https://raw.github.com/umano/MultiItemRowListAdapter/master/multiitem.png) 9 | 10 | ### Usage 11 | 12 | All you need to do is wrap your original ListAdapter using a `MultiItemRowListAdapter`: 13 | ```java 14 | int spacing = (int)getResources().getDimension(R.dimen.spacing); 15 | int itemsPerRow = getResources().getInteger(R.integer.items_per_row); 16 | MultiItemRowListAdapter wrapperAdapter = new MultiItemRowListAdapter(this, mAdapter, itemsPerRow, spacing); 17 | setListAdapter(wrapperAdapter); 18 | ``` 19 | As you can see the constructor for `MultiItemRowListAdapter` takes two parameters `itemsPerRow` and `spacing`. The first one is just the number of items from the original adapter to place on each row, and the second one is the cell spacing in pixels between the items. It's really convenient to specify the parameters in xml, so that you can easily vary the number of items per row on different screen orientations and sizes. 20 | res/values/integers.xml - phone portrait (1 items per row) 21 | ```xml 22 | 23 | 24 | 1 25 | 26 | ``` 27 | res/values-land/integers.xml - phone landscape (2 items per row) 28 | ```xml 29 | 30 | 31 | 2 32 | 33 | ``` 34 | res/values-sw600/integers.xml - 7' tablet portrait (2 items per row) 35 | ```xml 36 | 37 | 38 | 2 39 | 40 | ``` 41 | res/values-sw600-land/integers.xml - 7' tablet landscape (3 items per row) 42 | ```xml 43 | 44 | 45 | 3 46 | 47 | ``` 48 | 49 | ### Requrements 50 | Tested on Android 2.2+ 51 | 52 | ### Licence 53 | Licensed under the Apache License, Version 2.0 (the "License"); 54 | you may not use this work except in compliance with the License. 55 | You may obtain a copy of the License in the LICENSE file, or at: 56 | 57 | [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) 58 | 59 | Unless required by applicable law or agreed to in writing, software 60 | distributed under the License is distributed on an "AS IS" BASIS, 61 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 62 | See the License for the specific language governing permissions and 63 | limitations under the License. 64 | -------------------------------------------------------------------------------- /libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/umano/MultiItemRowListAdapter/a5e3e4e50e05e2ee95d1c0d261fd56cd85381e58/libs/android-support-v4.jar -------------------------------------------------------------------------------- /multiitem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/umano/MultiItemRowListAdapter/a5e3e4e50e05e2ee95d1c0d261fd56cd85381e58/multiitem.png -------------------------------------------------------------------------------- /proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-17 15 | -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/umano/MultiItemRowListAdapter/a5e3e4e50e05e2ee95d1c0d261fd56cd85381e58/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/umano/MultiItemRowListAdapter/a5e3e4e50e05e2ee95d1c0d261fd56cd85381e58/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/umano/MultiItemRowListAdapter/a5e3e4e50e05e2ee95d1c0d261fd56cd85381e58/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/umano/MultiItemRowListAdapter/a5e3e4e50e05e2ee95d1c0d261fd56cd85381e58/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable/above_shadow.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | -------------------------------------------------------------------------------- /res/drawable/list_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | -------------------------------------------------------------------------------- /res/layout/activity_demo.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 14 | 15 | 22 | 23 | -------------------------------------------------------------------------------- /res/layout/include_list_item_contact.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 23 | 24 | 36 | 37 | -------------------------------------------------------------------------------- /res/menu/demo.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /res/values-land/integers.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 2 4 | -------------------------------------------------------------------------------- /res/values-sw600dp-land/integers.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 3 4 | -------------------------------------------------------------------------------- /res/values-sw600dp/integers.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 2 4 | -------------------------------------------------------------------------------- /res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #e8e8e8 4 | 5 | -------------------------------------------------------------------------------- /res/values/dimen.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10dp 4 | -------------------------------------------------------------------------------- /res/values/integers.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 1 4 | -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MultiItemRowListAdapter Demo 5 | Settings 6 | Hello world! 7 | 8 | 9 | -------------------------------------------------------------------------------- /res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 15 | 16 | 17 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/com/sothree/multiitemrowlistadapter/MultiItemRowListAdapter.java: -------------------------------------------------------------------------------- 1 | package com.sothree.multiitemrowlistadapter; 2 | 3 | import java.lang.ref.WeakReference; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import android.content.Context; 8 | import android.database.DataSetObserver; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.view.ViewGroup.LayoutParams; 12 | import android.widget.AbsListView; 13 | import android.widget.LinearLayout; 14 | import android.widget.ListAdapter; 15 | import android.widget.WrapperListAdapter; 16 | 17 | public class MultiItemRowListAdapter implements WrapperListAdapter { 18 | private final ListAdapter mAdapter; 19 | private final int mItemsPerRow; 20 | private final int mCellSpacing; 21 | private final WeakReference mContextReference; 22 | private final LinearLayout.LayoutParams mItemLayoutParams; 23 | private final AbsListView.LayoutParams mRowLayoutParams; 24 | 25 | public MultiItemRowListAdapter(Context context, ListAdapter adapter, int itemsPerRow, int cellSpacing) { 26 | if (itemsPerRow <= 0) { 27 | throw new IllegalArgumentException("Number of items per row must be positive"); 28 | } 29 | mContextReference = new WeakReference(context); 30 | mAdapter = adapter; 31 | mItemsPerRow = itemsPerRow; 32 | mCellSpacing = cellSpacing; 33 | 34 | mItemLayoutParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT); 35 | mItemLayoutParams.setMargins(cellSpacing, cellSpacing, 0, 0); 36 | mItemLayoutParams.weight = 1; 37 | mRowLayoutParams = new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 38 | } 39 | 40 | @Override 41 | public boolean isEmpty() { 42 | return (mAdapter == null || mAdapter.isEmpty()); 43 | } 44 | 45 | public int getItemsPerRow() { 46 | return mItemsPerRow; 47 | } 48 | 49 | @Override 50 | public int getCount() { 51 | if (mAdapter != null) { 52 | return (int)Math.ceil(1.0f * mAdapter.getCount() / mItemsPerRow); 53 | } 54 | return 0; 55 | } 56 | 57 | @Override 58 | public boolean areAllItemsEnabled() { 59 | if (mAdapter != null) { 60 | return mAdapter.areAllItemsEnabled(); 61 | } else { 62 | return true; 63 | } 64 | } 65 | 66 | @Override 67 | public boolean isEnabled(int position) { 68 | if (mAdapter != null) { 69 | // the cell is enabled if at least one item is enabled 70 | boolean enabled = false; 71 | for (int i = 0; i < mItemsPerRow; ++i) { 72 | int p = position * mItemsPerRow + i; 73 | if (p < mAdapter.getCount()) { 74 | enabled |= mAdapter.isEnabled(p); 75 | } 76 | } 77 | return enabled; 78 | } 79 | return true; 80 | } 81 | 82 | @Override 83 | public Object getItem(int position) { 84 | if (mAdapter != null) { 85 | List items = new ArrayList(mItemsPerRow); 86 | for (int i = 0; i < mItemsPerRow; ++i) { 87 | int p = position * mItemsPerRow + i; 88 | if (p < mAdapter.getCount()) { 89 | items.add(mAdapter.getItem(p)); 90 | } 91 | } 92 | return items; 93 | } 94 | return null; 95 | } 96 | 97 | @Override 98 | public long getItemId(int position) { 99 | if (mAdapter != null) { 100 | return position; 101 | } 102 | return -1; 103 | } 104 | 105 | @Override 106 | public boolean hasStableIds() { 107 | if (mAdapter != null) { 108 | return mAdapter.hasStableIds(); 109 | } 110 | return false; 111 | } 112 | 113 | @Override 114 | public View getView(int position, View convertView, ViewGroup parent) { 115 | Context c = mContextReference.get(); 116 | if (c == null || mAdapter == null) return null; 117 | 118 | LinearLayout view = null; 119 | if (convertView == null 120 | || !(convertView instanceof LinearLayout) 121 | || !((Integer)convertView.getTag()).equals(mItemsPerRow)) { 122 | // create a linear Layout 123 | view = new LinearLayout(c); 124 | view.setPadding(0, 0, mCellSpacing, 0); 125 | view.setLayoutParams(mRowLayoutParams); 126 | view.setOrientation(LinearLayout.HORIZONTAL); 127 | view.setBaselineAligned(false); 128 | view.setTag(Integer.valueOf(mItemsPerRow)); 129 | } else { 130 | view = (LinearLayout) convertView; 131 | } 132 | 133 | for (int i = 0; i < mItemsPerRow; ++i) { 134 | View subView = i < view.getChildCount() ? view.getChildAt(i) : null; 135 | int p = position * mItemsPerRow + i; 136 | 137 | View newView = subView; 138 | if (p < mAdapter.getCount()) { 139 | if (subView instanceof PlaceholderView){ 140 | view.removeView(subView); 141 | subView = null; 142 | } 143 | newView = mAdapter.getView(p, subView, view); 144 | } else if (subView == null || !(subView instanceof PlaceholderView)) { 145 | newView = new PlaceholderView(c); 146 | } 147 | if (newView != subView || i >= view.getChildCount()) { 148 | if (i < view.getChildCount()) { 149 | view.removeView(subView); 150 | } 151 | newView.setLayoutParams(mItemLayoutParams); 152 | view.addView(newView, i); 153 | } 154 | } 155 | 156 | return view; 157 | } 158 | 159 | @Override 160 | public int getItemViewType(int position) { 161 | if (mAdapter != null) { 162 | return mAdapter.getItemViewType(position); 163 | } 164 | 165 | return -1; 166 | } 167 | 168 | @Override 169 | public int getViewTypeCount() { 170 | if (mAdapter != null) { 171 | return mAdapter.getViewTypeCount(); 172 | } 173 | return 1; 174 | } 175 | 176 | @Override 177 | public void registerDataSetObserver(DataSetObserver observer) { 178 | if (mAdapter != null) { 179 | mAdapter.registerDataSetObserver(observer); 180 | } 181 | } 182 | 183 | @Override 184 | public void unregisterDataSetObserver(DataSetObserver observer) { 185 | if (mAdapter != null) { 186 | mAdapter.unregisterDataSetObserver(observer); 187 | } 188 | } 189 | 190 | @Override 191 | public ListAdapter getWrappedAdapter() { 192 | return mAdapter; 193 | } 194 | 195 | public static class PlaceholderView extends View { 196 | 197 | public PlaceholderView(Context context) { 198 | super(context); 199 | } 200 | 201 | } 202 | } -------------------------------------------------------------------------------- /src/com/sothree/multiitemrowlistadapter/demo/DemoActivity.java: -------------------------------------------------------------------------------- 1 | package com.sothree.multiitemrowlistadapter.demo; 2 | 3 | import com.sothree.multiitemrowlistadapter.MultiItemRowListAdapter; 4 | import com.sothree.slidinguppaneldemo.R; 5 | import android.net.Uri; 6 | import android.os.Build; 7 | import android.os.Bundle; 8 | import android.provider.ContactsContract; 9 | import android.provider.ContactsContract.Contacts; 10 | import android.provider.ContactsContract.Contacts.Photo; 11 | import android.app.Activity; 12 | import android.app.ListActivity; 13 | import android.app.LoaderManager; 14 | import android.content.Context; 15 | import android.content.CursorLoader; 16 | import android.content.Loader; 17 | import android.database.Cursor; 18 | import android.view.LayoutInflater; 19 | import android.view.Menu; 20 | import android.view.View; 21 | import android.view.View.OnClickListener; 22 | import android.view.ViewGroup; 23 | import android.widget.AdapterView; 24 | import android.widget.AdapterView.OnItemClickListener; 25 | import android.widget.BaseAdapter; 26 | import android.widget.ImageView; 27 | import android.widget.TextView; 28 | import android.widget.Toast; 29 | 30 | public class DemoActivity extends ListActivity implements 31 | LoaderManager.LoaderCallbacks, 32 | OnItemClickListener { 33 | 34 | private ContactsAdapter mContactsAdapter; 35 | 36 | @Override 37 | protected void onCreate(Bundle savedInstanceState) { 38 | super.onCreate(savedInstanceState); 39 | setContentView(R.layout.activity_demo); 40 | 41 | mContactsAdapter = new ContactsAdapter(this); 42 | // We need to set the item on click listener on the original adapter instead of the ListView 43 | // since each row in MultiItemRowListAdapter has multiple items 44 | mContactsAdapter.setOnItemClickListener(this); 45 | 46 | int spacing = (int)getResources().getDimension(R.dimen.spacing); 47 | int itemsPerRow = getResources().getInteger(R.integer.items_per_row); 48 | MultiItemRowListAdapter wrapperAdapter = new MultiItemRowListAdapter(this, mContactsAdapter, itemsPerRow, spacing); 49 | setListAdapter(wrapperAdapter); 50 | getLoaderManager().restartLoader(0, null, this); 51 | } 52 | 53 | @Override 54 | protected void onDestroy() { 55 | super.onDestroy(); 56 | mContactsAdapter.close(); 57 | } 58 | 59 | @Override 60 | public boolean onCreateOptionsMenu(Menu menu) { 61 | // Inflate the menu; this adds items to the action bar if it is present. 62 | getMenuInflater().inflate(R.menu.demo, menu); 63 | return true; 64 | } 65 | 66 | @Override 67 | public Loader onCreateLoader(int id, Bundle bundle) { 68 | Uri uri = ContactsContract.Contacts.CONTENT_URI; 69 | String[] projection; 70 | String displayNameColumn; 71 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 72 | displayNameColumn = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY; 73 | projection= new String[] { 74 | ContactsContract.Contacts._ID, 75 | ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, 76 | displayNameColumn 77 | }; 78 | 79 | } else { 80 | displayNameColumn = ContactsContract.Contacts.DISPLAY_NAME; 81 | projection= new String[] { 82 | ContactsContract.Contacts._ID, 83 | displayNameColumn 84 | }; 85 | } 86 | String selection = 87 | ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1' AND " + 88 | "NULLIF(" + displayNameColumn +", '') IS NOT NULL"; 89 | String[] selectionArgs = null; 90 | String sortOrder = ContactsContract.Contacts.STARRED + " DESC, " + ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 91 | return new CursorLoader(DemoActivity.this, uri, projection, selection, selectionArgs, sortOrder); 92 | } 93 | 94 | @Override 95 | public void onLoadFinished(Loader loader, Cursor cursor) { 96 | mContactsAdapter.swapCursor(cursor); 97 | } 98 | 99 | @Override 100 | public void onLoaderReset(Loader arg0) { 101 | mContactsAdapter.swapCursor(null); 102 | } 103 | private static class ContactsAdapter extends BaseAdapter { 104 | private final LayoutInflater mInflater; 105 | 106 | private Cursor mCursor; 107 | private OnItemClickListener mOnItemClickListener; 108 | 109 | private final class ContactViewHolder { 110 | public TextView mContactName; 111 | public ImageView mContactImage; 112 | } 113 | 114 | public ContactsAdapter(Activity context) { 115 | mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 116 | } 117 | 118 | public void setOnItemClickListener(OnItemClickListener listener) { 119 | mOnItemClickListener = listener; 120 | } 121 | 122 | public Cursor swapCursor(Cursor newCursor) { 123 | if (mCursor != null && mCursor != newCursor) { 124 | mCursor.close(); 125 | } 126 | mCursor = newCursor; 127 | notifyDataSetChanged(); 128 | return mCursor; 129 | } 130 | 131 | @Override 132 | public int getCount() { 133 | if (mCursor != null && mCursor.getCount() > 0) { 134 | return mCursor.getCount(); 135 | } 136 | return 0; 137 | } 138 | 139 | @Override 140 | public Object getItem(int position) { 141 | if (mCursor != null) { 142 | mCursor.moveToPosition(position); 143 | return mCursor; 144 | } 145 | return null; 146 | } 147 | 148 | //return the header view, if it's in a section header position 149 | @Override 150 | public View getView(final int position, View convertView, final ViewGroup parent) { 151 | ContactViewHolder viewHolder; 152 | if (convertView == null || !(convertView.getTag() instanceof ContactViewHolder)) { 153 | convertView = mInflater.inflate(R.layout.include_list_item_contact, parent, false); 154 | viewHolder = new ContactViewHolder(); 155 | viewHolder.mContactName = (TextView) convertView.findViewById(R.id.lbl_contact_name); 156 | viewHolder.mContactImage = (ImageView) convertView.findViewById(R.id.image); 157 | convertView.setTag(viewHolder); 158 | } else { 159 | viewHolder = (ContactViewHolder)convertView.getTag(); 160 | } 161 | Cursor c = (Cursor)getItem(position); 162 | 163 | if (c == null) { 164 | return convertView; 165 | } 166 | 167 | int photoColumnIndex; 168 | int nameColumnIndex; 169 | 170 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 171 | photoColumnIndex = mCursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI); 172 | nameColumnIndex = mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY); 173 | } else { 174 | photoColumnIndex = mCursor.getColumnIndex(ContactsContract.Contacts._ID);; 175 | nameColumnIndex = mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 176 | } 177 | 178 | if (nameColumnIndex >= 0) { 179 | viewHolder.mContactName.setText(c.getString(nameColumnIndex)); 180 | } 181 | 182 | if (photoColumnIndex >= 0 && c.getString(photoColumnIndex) != null) { 183 | String photoData = c.getString(photoColumnIndex); 184 | // Creates a holder for the URI. 185 | Uri thumbUri; 186 | // If Android 3.0 or later 187 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 188 | // Sets the URI from the incoming PHOTO_THUMBNAIL_URI 189 | thumbUri = Uri.parse(photoData); 190 | } else { 191 | // Prior to Android 3.0, constructs a photo Uri using _ID 192 | /* 193 | * Creates a contact URI from the Contacts content URI 194 | * incoming photoData (_ID) 195 | */ 196 | final Uri contactUri = Uri.withAppendedPath( 197 | Contacts.CONTENT_URI, photoData); 198 | /* 199 | * Creates a photo URI by appending the content URI of 200 | * Contacts.Photo. 201 | */ 202 | thumbUri = 203 | Uri.withAppendedPath( 204 | contactUri, Photo.CONTENT_DIRECTORY); 205 | } 206 | viewHolder.mContactImage.setImageURI(thumbUri); 207 | } else { 208 | viewHolder.mContactImage.setImageResource(0); 209 | } 210 | 211 | final View clickedView = convertView; 212 | // set the on click listener for each of the items 213 | convertView.setOnClickListener(new OnClickListener() { 214 | @Override 215 | public void onClick(View v) { 216 | if (mOnItemClickListener != null) { 217 | mOnItemClickListener.onItemClick(null, clickedView, position, position); 218 | } 219 | 220 | } 221 | }); 222 | 223 | return convertView; 224 | } 225 | 226 | @Override 227 | public long getItemId(int position) { 228 | return position; 229 | } 230 | 231 | @Override 232 | public boolean areAllItemsEnabled() { 233 | return false; 234 | } 235 | 236 | @Override 237 | public boolean isEnabled(int position) { 238 | return true; 239 | } 240 | 241 | public void close() { 242 | if (mCursor != null) { 243 | mCursor.close(); 244 | } 245 | } 246 | } 247 | @Override 248 | public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) { 249 | if (mContactsAdapter != null) { 250 | Cursor c = (Cursor)mContactsAdapter.getItem(position); 251 | String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY)); 252 | Toast t = Toast.makeText(this, "Clicked " + name, Toast.LENGTH_SHORT); 253 | t.show(); 254 | } 255 | } 256 | } 257 | --------------------------------------------------------------------------------