1

我有 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;
}
}
4

2 回答 2

0

您可以对 BaseAdapter 有多个布局实现,如本文本文中给出的那样

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

于 2013-01-02T13:28:38.043 回答
0

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

于 2013-01-04T11:19:04.480 回答