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