У меня есть следующий класс:
private class CountryAdapter extends ArrayAdapter<String> {
private LayoutInflater inflater;
CountryAdapter(Context context, int textViewResourceId, ArrayList objects) {
super(context, textViewResourceId, objects);
inflater = getLayoutInflater();
}
@Override
public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {
return getCustomDropdown(position, convertView, parent);
}
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
View getCustomDropdown(int position, View convertView, ViewGroup parent) {
DropViewHolder dropViewHolder;
if (convertView == null) {
dropViewHolder = new DropViewHolder();
convertView = inflater.inflate(R.layout.country_spinner_row, parent, false);
convertView.setTag(dropViewHolder);
dropViewHolder.label = convertView.findViewById(R.id.spinner_countryNameRow);
dropViewHolder.icon = convertView.findViewById(R.id.spinner_countryFlagRow);
} else {
dropViewHolder = (DropViewHolder) convertView.getTag();
}
dropViewHolder.label.setText(String.format("%s (+%s)", countryNameList.get(position), countryList.optJSONObject(position).optString("phoneCountryCode")));
String flagPath = "flag_" + countryList.optJSONObject(position).optString("countryCode").toLowerCase();
dropViewHolder.icon.setImageResource(getResources().getIdentifier(flagPath, "drawable", getPackageName()));
return convertView;
}
View getCustomView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.country_spinner_row_dropdown, parent, false);
convertView.setTag(viewHolder);
viewHolder.icon = convertView.findViewById(R.id.spinner_countryFlagRow);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String flagPath = "flag_" + countryList.optJSONObject(position).optString("countryCode").toLowerCase(); viewHolder.icon.setImageResource(getResources().getIdentifier(flagPath, "drawable", getPackageName()));
return convertView;
}
private class DropViewHolder {
TextView label;
AdapterImageView icon;
}
private class ViewHolder {
AdapterImageView icon;
}
}
Я думаю, что он соответствует всем стандартам для ArrayAdapter, многократного использования представлений и т. Д. Однако при открытии клавиатуры вид перерисовывается и имеется значительная задержка при нажатии кнопки.обновление позиционирования.
Есть ли что-то явно очевидное, чего мне не хватает?
Заранее спасибо :)
РЕДАКТИРОВАТЬ: для большей ясности, AdapterImageView
это просто класс, которыйрасширяет ImageView
и переопределяет requestLayout
, не вызывая super для предотвращения дорогостоящих перерисовок.
EDIT2: В отличие от этого, этот фрагмент кода, который старше и теоретически работает хуже, не задерживает пользовательский интерфейс.
public class CountryAdapter extends ArrayAdapter<String> {
public CountryAdapter(Context context, int textViewResourceId, ArrayList objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.country_spinner_row, parent, false);
TextView label = (TextView) row.findViewById(R.id.spinner_countryNameRow);
label.setText(countryNameList.get(position).toString());
ImageView icon = (ImageView) row.findViewById(R.id.spinner_countryFlagRow);
String flagPath = "flag_" + countryList.optJSONObject(position).optString("countryCode").toLowerCase();
icon.setImageResource(getResources().getIdentifier(flagPath, "drawable", getPackageName()));
return row;
}
}