解决ListView中图标错乱显示的问题

解决办法:看代码

创新互联专注于企业成都全网营销、网站重做改版、昆都仑网站定制设计、自适应品牌网站建设、H5网站设计成都商城网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为昆都仑等各大城市提供网站开发制作服务。

package com.zhiren.coaltransportsystem.adapters;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhiren.coaltransportsystem.R;
import com.zhiren.coaltransportsystem.activities.Stationed_List_Activity;
import com.zhiren.coaltransportsystem.bean.ChoseAddressBean;

import java.util.List;

/**
 * Created by Administrator on 2016/6/3.
 */
public class LocationAdapter extends BaseAdapter {

    private Context context;
    private List list;
    private ImageView icon;
    private Handler handler;

    public LocationAdapter(List list, Context context, Handler handler) {
        this.list = list;
        this.context = context;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (position > list.size() || position < 0) {
            return convertView;
        }

        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();

            convertView = View.inflate(context, R.layout.find_address_item, null);
            viewHolder.iv_icon_chose = (ImageView) convertView.findViewById(R.id.iv_icon_chose);
            viewHolder.tv_addressname_item = (TextView) convertView.findViewById(R.id.tv_addressname_item);
            viewHolder.rl_icon_item = (RelativeLayout) convertView.findViewById(R.id.rl_icon_item);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.iv_icon_chose.setVisibility(View.GONE);

        final ChoseAddressBean choseAddressBean = (ChoseAddressBean) list.get(position);
        viewHolder.tv_addressname_item.setText(choseAddressBean.name);
        final ViewHolder finalViewHolder = viewHolder;
        viewHolder.rl_icon_item.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                icon.setVisibility(View.GONE);
                finalViewHolder.iv_icon_chose.setVisibility(View.VISIBLE);
                icon = finalViewHolder.iv_icon_chose;
                Message msg = Message.obtain();
                msg.obj = choseAddressBean.name;
                msg.what = Stationed_List_Activity.NEW_ADDRESS;
                handler.sendMessage(msg);
            }
        });
        if (position == 0) {
            viewHolder.iv_icon_chose.setVisibility(View.VISIBLE);
            icon = viewHolder.iv_icon_chose;
        }

        return convertView;
    }

    static class ViewHolder {
        private ImageView iv_icon_chose;
        private TextView tv_addressname_item;
        private RelativeLayout rl_icon_item;
    }
}

出现图片错乱显示的问题是因为快速翻动复用了View,那么每次将其重置即可。


分享标题:解决ListView中图标错乱显示的问题
文章URL:http://ybzwz.com/article/jhsodi.html