用于国家标志和图像微调器的ArrayAdapter减慢了视图速度

时间:2018-10-11 16:42:34

标签: java android android-arrayadapter

我有以下课程:

    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:相比之下,这段代码更旧,理论上讲性能更差,不会落后于UI。

 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;
    }
}

0 个答案:

没有答案