我正在使用ListView
来显示国家/地区列表。对于每个字母,我有一行包含字母,只是为了使ListView
更清晰(所以看起来像这样:'A',然后奥地利,澳大利亚......然后'B'然后比利时,玻利维亚等......)
对于带字母的行,我将clickable和enabled属性设置为false。我也改变了背景和文字颜色。第一眼看上去一切都很完美,但是一旦我开始滚动,屏幕外的行就会开始交换它们的属性:某些国家/地区的行采用“字母行”属性,而某些字母行则转为国家/地区。它只发生在屏幕外的项目,因此不可见。这是我的代码:
final ArrayList<Integer> nonFocusableElements = new ArrayList<Integer>();
//THE COUNTRIES ARE ALREADY SORTED ON THE QUERY. I CHECK THE FIRST LETTER AND A ROW TO SEPARATE THE COUNTRIES BY LETTER
Cursor cursor = sql.rawQuery(query, null);
String firstCharacter = " ";
if (cursor != null && cursor.moveToNext()) {
String[] values = new String[cursor.getCount()];
int i = 0;
do {
if (!cursor.getString(cursor.getColumnIndex("country_name")).substring(0, 1).equals(firstCharacter)) {
firstCharacter = cursor.getString(cursor.getColumnIndex("country_name")).substring(0, 1);
values = java.util.Arrays.copyOf(values, values.length + 1);
values[i] = firstCharacter;
nonFocusableElements.add(i);
i++;
}
values[i] = cursor.getString(cursor.getColumnIndex("country_name"));
i++;
} while (cursor.moveToNext());
sql.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
}
;
/////////////////////////////////////////////////////////
///// post() ensure that listview is RENDERED /////
/////////////////////////////////////////////////////////
//在这里我管理信件行
listView.setCacheColorHint(Color.TRANSPARENT);
listView.post(new Runnable() {
public void run() {
if(!rendered) {
int visiblesElements = listView.getLastVisiblePosition() - listView.getFirstVisiblePosition();
for (int i = 0; i < nonFocusableElements.size(); i++) {
if (listView.getChildAt(nonFocusableElements.get(i)) != null) {
listView.getChildAt(nonFocusableElements.get(i)).setEnabled(false);
listView.getChildAt(nonFocusableElements.get(i)).setClickable(false);
listView.getChildAt(nonFocusableElements.get(i)).setFocusable(false);
listView.getChildAt(nonFocusableElements.get(i)).setOnClickListener(null);
listView.getChildAt(nonFocusableElements.get(i)).setBackgroundColor(Color.parseColor("#AFAFAF"));
TextView text = (TextView) listView.getChildAt(nonFocusableElements.get(i)).findViewById(android.R.id.text1);
text.setTextColor(Color.WHITE);
}
rendered=true;
}
}
}
});
//国家队的聆听者
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Bundle bundle = new Bundle();
bundle.putString("type", "state");
bundle.putString("value", ((TextView) arg1).getText().toString());
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
AlphabeticalListViewFragment liste = new AlphabeticalListViewFragment();
liste.setArguments(bundle);
fragmentTransaction.replace(android.R.id.content, liste, "listfragment");
fragmentTransaction.addToBackStack(liste.getClass().getName());
fragmentTransaction.commit();
}
});
你知道这里有什么问题吗?谢谢。我补充一点,我在SO上尝试了很多解决方案,但没有什么对我有用......