列表视图和性能中的3个持有者

时间:2013-01-02 12:51:47

标签: android performance listview

我有3个持有人。 1个持有人1项。 方法getView看起来:

public View getView(int position, View convertView, ViewGroup parent) {
    mCursor.moveToPosition(position);
    int type = checkDialogType(mCursor);
    Holder holder = null;
    if (convertView != null){
        holder = (Holder)convertView.getTag(type);
        if (holder == null){
            holder = createHolderByType(type, parent);
        }
    } else {
        holder = createHolderByType(type, parent);
    }
    return holder.fillView(mCursor, position);         //convertView
}

createHolderByType()的内容:

public Holder createHolderByType(int type, ViewGroup parent){
    View v;
    if (type == IN_TYPE){
        v = mInflater.inflate(R.layout.dialog_incoming_item, parent, false);
        return new InHolder(v, mCursor, mContext, this, IN_TYPE);
    } else if (type == OUT_TYPE){
        v = mInflater.inflate(R.layout.dialogs_outcoming_item, parent,false);
        return new OutHolder(v, mCursor, mContext, this, OUT_TYPE);
    } else {
        v = mInflater.inflate(R.layout.dialogs_chat_item, parent, false);
        return new ChatHolder(v, mCursor, mContext, this, CHAT_TYPE);
    }
}

它工作正常,但是对于listView方法中的39个项,createHolderByType被调用了19次。为了创建一个持有者,我需要膨胀xml。这个很贵。如果我尝试使用相同的视图持有人或在内存中保存持有人 - 它不起作用。我可以用一些技巧提高性能吗?

UPD: 有一个持有人:

public class InHolder extends Holder {
private View baseView;
private TextView fio;
private TextView message;
private TextView date;
private ImageView isOnline;
private int type;
private ImageView senderIMG;

public InHolder(View v, Cursor dialogCursor, Context context, DialogCurAdapter adapter, int type){
    super(dialogCursor, context, adapter);
    fio = (TextView)v.findViewById(R.id.fio);
    senderIMG = (ImageView)v.findViewById(R.id.sender_image);
    message = (TextView)v.findViewById(R.id.message_preview);
    date = (TextView)v.findViewById(R.id.date);
    isOnline = (ImageView)v.findViewById(R.id.isonline);
    this.type= type;
    baseView = v;
    baseView.setTag(type, this);
}

@Override
public View fillView(Cursor dialogCursor, final int position) {
    try{
        int ownerID = dialogCursor.getInt(ownerIndex);
        User usr = mDButils.getUserByID(ownerID);
        String messageText = Html.fromHtml(dialogCursor.getString(bodyIndex)).toString();
        date.setText(DateUtils.getTime(dialogCursor.getInt(dateIndex), mContext));
        message.setText(messageText);

        if (messageText == null || messageText.equals("")){
            if (dialogCursor.getInt(hasAttIndex) == 1){
                message.setText(mContext.getResources().getString(R.string.attachment));
                message.setTextColor(getColor(R.color.date_blue_to_white_selector));
            }

        }

        if (dialogCursor.getInt(hasAttIndex)  == 1){
            String[] attaches = dialogCursor.getString(attTypeIndex).split(",");
        }

        if (dialogCursor.getInt(readstateIndex) == 0){
            baseView.setBackgroundDrawable(getDrawable(R.drawable.lightblue_to_transparent_selector));
        } else baseView.setBackgroundDrawable(getDrawable(R.drawable.white_to_blue_selector));

        if (usr != null){
            mImageLoader.displayImage(usr.getImageURL(), senderIMG);
            fio.setText(usr.getFirstName() + " " + usr.getLastName());
            isOnline.setVisibility(usr.isOnLine()  == 1  ? View.VISIBLE : View.INVISIBLE);
        } else {
            AsyncUserLoader userLoader = new AsyncUserLoader(mContext) {
                @Override
                protected void onPostExecute(User user) {
                    if (user != null){
                        mDialogAdapter.updateItem(position);
                    }
                }
            };
            userLoader.execute(Integer.parseInt(ownerID + ""));
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return baseView;
}

@Override
public int getType() {
    return type;
}
}

2 个答案:

答案 0 :(得分:0)

您可以为this postarticle

中给出的BaseAdapter实现多个布局实现

注意:在getItemViewType(int position)中你可以使用checkDialogType(mCursor);找到要使用的布局类型。但是getItemViewType(int position)应该返回0到2之间的值(当count为3时)。

答案 1 :(得分:0)

我找到了解决方案。当我需要的所有视图时,我制作了一个复杂的容器。并且取决于视图的类型,我使它们可见或不可见。所以当convertView与类型不匹配时,我不需要膨胀xml。