gpt4 book ai didi

android - 使用不同布局时的 Listview 回收

转载 作者:行者123 更新时间:2023-11-30 00:28:02 26 4
gpt4 key购买 nike

我已经使用适配器创建了一个 ListView,但唯一的问题是我们使用了不同的布局,因为它是一个消息传递应用程序。我想通过使用 View 回收来提高性能,但不知道如何回收 View 。

是否可以更改 View 布局或是否可能:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
MessageLog message = this.mData.get(position);

MessageRowViewHolder holder = new MessageRowViewHolder();
int type = getItemViewType(position);

switch (type) {
case TYPE_ITEM:

// Inflate the correct view depending on whether the message is a reply or
// own message.
if (message.isReply()) {
convertView = mInflater.inflate(R.layout.row_prisoners_voicemail_message, parent, false);
} else {
convertView = mInflater.inflate(R.layout.row_own_voicemail_message, parent, false);

}

// The holder simply holds views for convenient access.
// It is nothing more than a dumb container.
holder.type = type;
holder.position = position;

holder.messageBackground = (RelativeLayout) convertView.findViewById(R.id.voiceMessageRectangleBackground);

holder.commentView = (TextView) convertView.findViewById(R.id.voiceMessageCommentText);
holder.messageDurationView = (TextView) convertView.findViewById(R.id.voiceMessageDurationText);
holder.metaTextView = (TextView) convertView.findViewById(R.id.voiceMessageMetaText);
holder.timestampView = (TextView) convertView.findViewById(R.id.voiceMessageTimestampText);

holder.editButtonView = (ImageView) convertView.findViewById(R.id.voiceMessageEditButtonImage);
holder.favouritedImageView = (ImageView) convertView.findViewById(R.id.voiceMessageFavouritedImage);
holder.playPauseImageView = (ImageView) convertView.findViewById(R.id.voiceMessagePlayPauseImage);

// Numeric tags to detect which view has been clicked.
holder.editButtonView.setTag(position+"");
holder.metaTextView.setTag(position+"");

holder.editButtonView.setOnClickListener(this);
holder.metaTextView.setOnClickListener(this);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.grid_date_row, parent, false);

holder.type = type;
holder.dateView = (TextView) convertView.findViewById(R.id.date);

break;
case TYPE_FILLER:
convertView = mInflater.inflate(R.layout.grid_filler_row, parent, false);
holder.type = type;
holder.dateView = (TextView) convertView.findViewById(R.id.refreshText);

break;
case TYPE_ITEM_UPLOAD:
convertView = mInflater.inflate(R.layout.row_upload_voicemail_message, parent, false);

holder.type = type;
holder.messageBackground = (RelativeLayout) convertView.findViewById(R.id.voiceMessageRectangleBackground);
break;
}

// Short-hand notations for the resources and the message date.
Resources r = context.getResources();
Date messageDate = message.getDate();

switch (holder.type) {
case TYPE_ITEM:

// Set the duration of the call and the timestamp.
holder.messageDurationView.setText(PvmUtils.formatMessageDuration(context, message.getDuration()));
holder.timestampView.setText(PvmUtils.formatMessageTimestamp(messageDate));


// Set the on click listener for editing.


// Show/hide the favourited view and comment.
if (message.isFavourited()) {
holder.favouritedImageView.setVisibility(View.VISIBLE);

String description = message.getDescription();
if (description != null) {
holder.commentView.setVisibility(View.VISIBLE);
holder.commentView.setText(description);
} else {
holder.commentView.setVisibility(View.GONE);
}

} else {
holder.favouritedImageView.setVisibility(View.INVISIBLE);
holder.commentView.setVisibility(View.GONE);
}


if (message.isReply()) {
// If it is a reply, set the 'NEW' label if it is a new message.
if (message.isRead()) {
holder.metaTextView.setText("");
} else {
holder.metaTextView.setText(r.getString(R.string.new_voice_message_label));
}

} else {
// If it is own message, set whether the message has been uploaded and
// listened to; and if so, when.

if (message.isRead()) {

String dateRead = "";
if (message.getReadAt() != null) {
dateRead = PvmUtils.formatMediumLongDate(message.getReadAt());
}

// E.g. ✓✓ 20 Oct 17 15:54
String textToSet = String.format("%s %s", r.getString(R.string.doubleTick), dateRead);
holder.metaTextView.setText(textToSet);
} else {
holder.metaTextView.setText(r.getString(R.string.tick));
}
}

break;

case TYPE_SEPARATOR:

// Just set the formatted date.
SimpleDateFormat formatDate = new SimpleDateFormat("dd MMM yy (EEE)", SharedResources.getLocale(context));
holder.dateView.setText(formatDate.format(messageDate));
break;

case TYPE_FILLER:

// Whatever this is...
int size = list.getHeight();
holder.dateView.setPadding(0, size/2, 0, size/2);
break;

case TYPE_ITEM_UPLOAD:

// Set the background colour when uploading an item to yellow.
int colour;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
colour = r.getColor(R.color.yellow_message, null);
} else {
colour = r.getColor(R.color.yellow_message);
}

holder.messageBackground.setBackgroundColor(colour);
break;
}

return convertView;
}

最佳答案

您可以使用 ListView 的多个布局,因此无需立即迁移到 RecyclerView ;-)

如果您正在使用以某种方式从 BaseAdapter 扩展而来的适配器,您必须覆盖两个方法:

getViewTypeCount()

Returns the number of types of Views that will be created by getView(int, View, ViewGroup).

getItemViewType(int position)

Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.

您只需根据需要定义尽可能多的不同 View 类型:

private static int VIEW_TYPE_REPLY = 0;
private static int VIEW_TYPE_OTHER = 1;

然后 getItemViewType() 的实现可能如下所示:

MessageLog message = this.mData.get(position);
if (message.isReply()) {
return VIEW_TYPE_REPLY;
} else {
return VIEW_TYPE_OTHER;
}

getView()中,可以这样写

int viewType = getItemViewType(position);
switch(viewType){
case VIEW_TYPE_REPLY:
convertView = mInflater.inflate(R.layout.row_prisoners_voicemail_message, parent, false);
break;
default:
convertView = mInflater.inflate(R.layout.row_own_voicemail_message, parent, false);
}
// ....

以后可能会使用不同的 ViewHolder,具体取决于 View 类型的内容变化程度。

通过覆盖这两个方法,您将使框架能够在可能的情况下回收View。因此没有必要检查非 null convertView 是否具有正确的布局。

关于android - 使用不同布局时的 Listview 回收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45008251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com