我工作的ListActivity有一個內部類是SimpleAdapter實現SectionIndexer的子類。FastScroll與自定義ListAdapter奇怪行爲實現SectionIndexer
class MySimpleAdapter extends SimpleAdapter implements SectionIndexer {
HashMap<String, Integer> letters;
Object[] sections;
AlphabetIndexer alphaIndexer;
public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to,
HashMap<String, Integer> letters, Object[] sections) {
super(context, data, resource, from, to);
this.letters = letters;
this.sections = sections;
}
@SuppressWarnings("unchecked")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.common_image_row1, null);
}
HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
Integer idArtist = Integer.parseInt((String) data.get("idArtist"));
convertView.setTag(idArtist);
((TextView) convertView.findViewById(R.id.item_name))
.setText((String) data.get("sName"));
ImageView image = (ImageView) convertView.findViewById(R.id.item_image);
image.setTag((String) data.get("sImageUrl"));
image.setImageResource(R.drawable.default_artwork);
((TextView) convertView.findViewById(R.id.item_count))
.setText((String) data.get("iSongs") + " Songs");
if (!bScrolling) {
new API.DownloadImagesTask().execute(image);
}
return convertView;
}
public int getPositionForSection(int section) {
String letter = (String) sections[section];
return letters.get(letter);
}
public int getSectionForPosition(int position) {
return 0;
}
public Object[] getSections() {
return sections;
}
}
該活動在單獨的AysncTask中接收JSON對象。該對象由各種JSONArrays組成,這些JSONArrays的鍵是項目首字母的數組項。所以,這個數組是由一堆以字母「B」開始的項目組成的。因此,JSONArray的關鍵是「B」。
{
B: ["ball", "buck", "bill"]
C: ["charlie", "chuck", "chap"]
}
該對象不一定具有字母表中的所有字母。我也知道使用JSONObjects不能保證順序,所以我對它們進行排序。
List> maps = new ArrayList>(); ArrayList sections = new ArrayList(); HashMap letters = new HashMap();
String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N"
,"O","P","Q","R","S","T","U","V","W","X","Y","Z"};
int k = 0;
for (int i = 0; i < alphabet.length; i++) {
if (playlistArtists.optJSONArray(alphabet[i]) != null) {
try {
JSONArray artists = playlistArtists.getJSONArray(alphabet[i]);
sections.add(alphabet[i]);
for (int j = 0; j < artists.length(); j++) {
JSONObject artist = (JSONObject) artists.get(j);
HashMap<String, String> map= new HashMap<String, String>();
map.put("idArtist", artist.getString("idArtist"));
map.put("sName", artist.getString("sName"));
map.put("sImageUrl", artist.getString("sImageUrl-thumb"));
map.put("iSongs", artist.getString("iSongs"));
maps.add(map);
letters.put(alphabet[i], k);
k++;
}
} catch (JSONException e) {
Log.d(TAG,"JSONException in Music::GetMusicCatlog.doInBackground");
e.printStackTrace();
}
}
}
SimpleAdapter adapter = new MySimpleAdapter(Catalog.this,
maps,
R.layout.common_image_row1,
new String[] {"idArtist","sName", "sImageUrl", "iSongs" },
new int[] {R.id.item_id, R.id.item_name, R.id.item_image, R.id.item_count },
letters,
sections.toArray());
setListAdapter(adapter);
我遇到的問題是FastScroll。我有一切工作的大部分。該列表按第一個字母分組,當使用FastScroll時,該字母出現在彈出窗口中並轉到正確的組。問題是,當我將FastScroll「跳轉」到所需列表的任意部分後,我放開FastScroll。它不會留在我放開它的位置。我認爲它會在該部分的任意位置,因爲我沒有實施SectionIndexer。我認爲問題在於這種方法。
public int getSectionForPosition(int position) {
return 0;
}
我只是不知道如何正確地貫徹SectionIndexer方法...
作爲旁註:getPositionForSection()應該返回起始索引,而不是最後一個索引(你正在做的)。所以,假設藝術家是AZ排序的,或者向後瀏覽藝術家陣列,或者只是將索引添加到字母hashmap(如果該關鍵字尚不存在)(因此您只需添加第一個索引) –