This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathSimpleSectionAdapter.java
235 lines (197 loc) · 7.47 KB
/
SimpleSectionAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright (C) 2012 Mobs and Geeks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mobsandgeeks.adapters;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
/**
* A very simple adapter that adds sections to adapters written for {@link ListView}s.
* <br />
* <b>NOTE: The adapter assumes that the data source of the decorated list adapter is sorted.</b>
*
* @author Ragunath Jawahar R <rj@mobsandgeeks.com>
* @version 0.2
*/
public class SimpleSectionAdapter<T> extends BaseAdapter {
// Debug
static final boolean DEBUG = false;
static final String TAG = SimpleSectionAdapter.class.getSimpleName();
// Constants
private static final int VIEW_TYPE_SECTION_HEADER = 0;
// Attributes
private Context mContext;
private BaseAdapter mListAdapter;
private int mSectionHeaderLayoutId;
private int mSectionTitleTextViewId;
private Sectionizer<T> mSectionizer;
private LinkedHashMap<String, Integer> mSections;
/**
* Constructs a {@linkplain SimpleSectionAdapter}.
*
* @param context The context for this adapter.
* @param listAdapter A {@link ListAdapter} that has to be sectioned.
* @param sectionHeaderLayoutId Layout Id of the layout that is to be used for the header.
* @param sectionTitleTextViewId Id of a TextView present in the section header layout.
* @param sectionizer Sectionizer for sectioning the {@link ListView}.
*/
public SimpleSectionAdapter(Context context, BaseAdapter listAdapter,
int sectionHeaderLayoutId, int sectionTitleTextViewId,
Sectionizer<T> sectionizer) {
if(context == null) {
throw new IllegalArgumentException("context cannot be null.");
} else if(listAdapter == null) {
throw new IllegalArgumentException("listAdapter cannot be null.");
} else if(sectionizer == null) {
throw new IllegalArgumentException("sectionizer cannot be null.");
} else if(!isTextView(context, sectionHeaderLayoutId, sectionTitleTextViewId)) {
throw new IllegalArgumentException("sectionTitleTextViewId should be a TextView.");
}
this.mContext = context;
this.mListAdapter = listAdapter;
this.mSectionHeaderLayoutId = sectionHeaderLayoutId;
this.mSectionTitleTextViewId = sectionTitleTextViewId;
this.mSectionizer = sectionizer;
this.mSections = new LinkedHashMap<String, Integer>();
// Find sections
findSections();
}
private boolean isTextView(Context context, int layoutId, int textViewId) {
View inflatedView = View.inflate(context, layoutId, null);
View foundView = inflatedView.findViewById(textViewId);
return foundView instanceof TextView;
}
@Override
public int getCount() {
return mListAdapter.getCount() + getSectionCount();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
SectionHolder sectionHolder = null;
switch (getItemViewType(position)) {
case VIEW_TYPE_SECTION_HEADER:
if(view == null) {
view = View.inflate(mContext, mSectionHeaderLayoutId, null);
sectionHolder = new SectionHolder();
sectionHolder.titleTextView = (TextView) view.findViewById(mSectionTitleTextViewId);
view.setTag(sectionHolder);
} else {
sectionHolder = (SectionHolder) view.getTag();
}
break;
default:
view = mListAdapter.getView(getIndexForPosition(position),
convertView, parent);
break;
}
if(sectionHolder != null) {
String sectionName = sectionTitleForPosition(position);
sectionHolder.titleTextView.setText(sectionName);
}
return view;
}
@Override
public boolean areAllItemsEnabled() {
return mListAdapter.areAllItemsEnabled() ?
mSections.size() == 0 : false;
}
@Override
public int getItemViewType(int position) {
int positionInCustomAdapter = getIndexForPosition(position);
return mSections.values().contains(position) ?
VIEW_TYPE_SECTION_HEADER :
mListAdapter.getItemViewType(positionInCustomAdapter) + 1;
}
@Override
public int getViewTypeCount() {
return mListAdapter.getViewTypeCount() + 1;
}
@Override
public boolean isEnabled(int position) {
return mSections.values().contains(position) ?
false : mListAdapter.isEnabled(getIndexForPosition(position));
}
@Override
public Object getItem(int position) {
return mListAdapter.getItem(getIndexForPosition(position));
}
@Override
public long getItemId(int position) {
return mListAdapter.getItemId(getIndexForPosition(position));
}
@Override
public void notifyDataSetChanged() {
mListAdapter.notifyDataSetChanged();
findSections();
super.notifyDataSetChanged();
}
/**
* Returns the actual index of the object in the data source linked to the this list item.
*
* @param position List item position in the {@link ListView}.
* @return Index of the item in the wrapped list adapter's data source.
*/
public int getIndexForPosition(int position) {
int nSections = 0;
Set<Entry<String, Integer>> entrySet = mSections.entrySet();
for(Entry<String, Integer> entry : entrySet) {
if(entry.getValue() < position) {
nSections++;
}
}
return position - nSections;
}
static class SectionHolder {
public TextView titleTextView;
}
private void findSections() {
int n = mListAdapter.getCount();
int nSections = 0;
mSections.clear();
for(int i=0; i<n; i++) {
String sectionName = mSectionizer.getSectionTitleForItem((T) mListAdapter.getItem(i));
if(!mSections.containsKey(sectionName)) {
mSections.put(sectionName, i + nSections);
nSections ++;
}
}
if(DEBUG) {
Log.d(TAG, String.format("Found %d sections.", mSections.size()));
}
}
private int getSectionCount() {
return mSections.size();
}
private String sectionTitleForPosition(int position) {
String title = null;
Set<Entry<String, Integer>> entrySet = mSections.entrySet();
for(Entry<String, Integer> entry : entrySet) {
if(entry.getValue() == position) {
title = entry.getKey();
break;
}
}
return title;
}
}