android 完美解决getview position多次调用


gridview更新数据,发现每次更新getview,position=0的时候回调用很多次,这样我的设置数据会走很多次,造成资源占用;下边我就巧妙的解决这个问题。


下面贴下代码:里边注释地方是重点,重点看哦:


package com.wanzhao.imapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanzhao.imapp.activity.R;
import com.wanzhao.imapp.entity.imbean.User;
import com.wanzhao.imapp.tools.L;
import com.wanzhao.imapp.tools.task.GetHeadImgTask;
import com.wanzhao.imapp.util.BitmapCache;
import com.wanzhao.imapp.view.RoundHeadImageView;

import java.util.ArrayList;
import java.util.List;

public class InstitutionalMemberGridAdapter extends BaseAdapter {


    private Context mContext;
    private List mUser;
    private LayoutInflater mInflate;
    private List mUserIndex; // 为了解决重复调用的问题

    public InstitutionalMemberGridAdapter(Context context) {
        mContext = context;
        mInflate = LayoutInflater.from(mContext);
    }


    public List getUser() {
        return mUser;
    }

    public void setUser(List User) {
        this.mUser = User;

        // 这里是重点
        if (mUser != null) {
            mUserIndex = new ArrayList<>();
            int size = mUser.size();
            for (int i = 0; i < size; i++) {
                mUserIndex.add(i);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mUser != null) {
            return mUser.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    Holder mHolder;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {
            mHolder = new Holder();
            convertView = mInflate.inflate(
                    R.layout.item_fragment_institutional_member, parent, false);
            mHolder.itemInstitutionalName = (TextView) convertView
                    .findViewById(R.id.item_fragment_institutional_member_name);
            mHolder.itemMemberImages = (RoundHeadImageView) convertView
                    .findViewById(R.id.item_fragment_institutional_member_images);
            convertView.setTag(mHolder);
        } else {
            mHolder = (Holder) convertView.getTag();
        }

        // 这里是重点
        if (mUserIndex != null && mUserIndex.contains(position)) {
            // 这里是重点,必须强转为Object类型,不然会报越界,因为remove是重载方法,有两个,但是参数类型不一样
            mUserIndex.remove((Object) position);

            User user = mUser.get(position);
            mHolder.itemInstitutionalName.setText(user.getRealName());
            String userCode = user.getUserCode();
            L.d("mina---zuzhijiagou---userCode=" + userCode + ",position=" + position);
            String id = mUser.get(position).getId();
            mHolder.itemMemberImages.setTag(id);
            try {
                if (userCode.equals(user.getUserCode())) {
                    setAvatar(mHolder.itemMemberImages, id, userCode, true);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return convertView;
    }

    private void setAvatar(final ImageView iv, final String userId, final String userCode,
                           final boolean isOnline) {
        try {
            // ----------------
            if (!TextUtils.isEmpty(userId)) {
                Bitmap bitmapAvatar = BitmapCache.getInstance().getBitmap(
                        userId);
                if (userId.equals(iv.getTag().toString())) {
                    if (bitmapAvatar != null) {
                        iv.setImageBitmap(bitmapAvatar);
                    } else {
                        new GetHeadImgTask(mContext, iv, userId, userCode, false,
                                isOnline).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                    }
                }
            }
        } catch (Throwable e) {
            iv.setImageResource(R.drawable.lxrmrzx2x);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private class Holder {
        private TextView itemInstitutionalName;
        private RoundHeadImageView itemMemberImages;
    }


}


看了就可以解决了。那些重复调用,一次性过滤,有用的话,给我评论个666吧~~~



你可能感兴趣的:(android)