首页 > 代码库 > 解决adapter中使用ViewHolder出现重复加载的问题

解决adapter中使用ViewHolder出现重复加载的问题

   Map<Integer, View> map = new HashMap<Integer, View>();    @Override    public View getView(int position, View convertView, ViewGroup parent) {        View view;        ViewHolder viewHolder = null;        if (map.get(position) == null) {            viewHolder = new ViewHolder();            view = View.inflate(context, R.layout.setting_item, null);            viewHolder.portrait = (ImageView) view.findViewById(R.id.portrait);            viewHolder.divider = (View) view.findViewById(R.id.divider);            viewHolder.text = (TextView) view.findViewById(R.id.tv_item_name);            viewHolder.more = (ImageView) view.findViewById(R.id.set_more);            map.put(position, view);            view.setTag(viewHolder);        } else {            view = map.get(position);            viewHolder = (ViewHolder) view.getTag();        }        if (position == 0) {            viewHolder.portrait.setVisibility(ImageView.VISIBLE);        }        if (position == 4 || position == 5) {            viewHolder.more.setImageResource(R.drawable.ok_icon);        }        if (position == 2 || position == 5) {            viewHolder.divider.setVisibility(View.VISIBLE);        }        viewHolder.text.setText((String) itemList.get("name" + position));        return view;    }    private class ViewHolder {        private TextView text;        private ImageView portrait;        private View divider;        private ImageView more;    }

 

解决adapter中使用ViewHolder出现重复加载的问题