所以,我正在做一个应该像这张图片的应用程序:
它有一个列表视图,用户可以在其中选择要显示的内容,以及一个内容区域,当内容被选择时,它将被动画(滑入/滑出)。
内容区域必须动态构建,其布局会根据 JSON 请求中包含的信息发生变化,它由一个巨大的列表视图组成,每个项目都是一个信息部分(见图)。
问题是在 UI 线程中完成大量视图加载时为内容区域设置动画会导致动画中的巨大滞后和闪烁,并且无法在 UI 线程以外的线程中完成视图加载。
我尝试AsyncTask
在适配器中使用一些 UI 加载在后台完成,但只有 ViewHolder 模式实例化和findviewById
调用可以进入内部,所以我没有获得很大的性能增益。实际问题是 .setText()
,.setImageBitmap()
调用。
最后真正的问题是:为了获得流畅的动画,我能做些什么来在这个 UI 加载中获得更好的性能?
注意:考虑到用于填充内容区域的数据被硬编码以用于测试目的。
编辑: 我的内容区域适配器中使用的代码:
public class InfoAdapter extends ArrayAdapter<Info> {
private final LayoutInflater layoutInflater;
private final Typeface typeface;
public InfoAdapter(final Context context, final List<Info> list) {
super(context, 0, list);
this.layoutInflater = LayoutInflater.from(context);
this.typeface = Typeface.createFromAsset(context.getAssets(), "fonts/"
+ Constants.DEFAULT_FONTNAME);
}
@Override
public View getView(final int position, final View convertView,
final ViewGroup parent) {
View infoView = convertView;
ViewHolder holder;
if (infoView == null) {
infoView = (RelativeLayout) layoutInflater.inflate(getItem(position).getLayout(), null);
holder = new ViewHolder(
(TextView) infoView.findViewById(R.id.highlight_title),
(TextView) infoView.findViewById(R.id.info_title),
(TextView) infoView.findViewById(R.id.info_extra_info01),
(TextView) infoView.findViewById(R.id.info_extra_info02),
(TextView) infoView.findViewById(R.id.info_description),
(ImageView) infoView.findViewById(R.id.info_image),
(TextView) infoView.findViewById(R.id.info_subtitle),
(TextView) infoView.findViewById(R.id.info_subdescription),
(ImageView) infoView.findViewById(R.id.info_subimage));
infoView.setTag(holder);
} else {
holder = (ViewHolder) infoView.getTag();
}
final String extraInfo01 = String.valueOf((int) (getItem(position).getValue() / 1));
final String extraInfo02 = String.valueOf((int) ((getItem(position).getValue() % 1) * 100));
setDataInView(holder.highlight, getItem(position).getFavorite());
holder.title.setText(getItem(position).getName());
holder.title.setTypeface(typeface);
holder.extraInfo01.setText(extraInfo01);
holder.extraInfo01.setTypeface(typeface);
holder.extraInfo02.setText("," + extraInfo02);
holder.extraInfo02.setTypeface(typeface);
holder.description.setText(getItem(position).getDescription());
holder.description.setTypeface(typeface);
if (holder.image != null) {
if (getItem(position).getImage() == 0) {
holder.image.setVisibility(View.GONE);
} else {
holder.image.setVisibility(View.VISIBLE);
holder.image.setImageResource(getItem(position).getImage());
}
}
setDataInView(holder.subTitle, getItem(position).getHistoryTitle());
setDataInView(holder.subDescription, getItem(position).getHistory());
if (holder.subImage != null) {
if (getItem(position).getUri() == null) {
holder.subImage.setVisibility(View.GONE);
} else {
final Drawable subDrawable = Drawable.createFromPath(getItem(
position).getUri());
holder.subImage.setVisibility(View.VISIBLE);
holder.subImage.setImageDrawable(subDrawable);
}
}
return infoView;
}
private void setDataInView(final TextView textView, final String data) {
if (textView != null) {
if (data.isEmpty()) {
textView.setVisibility(View.GONE);
} else {
textView.setVisibility(View.VISIBLE);
textView.setTypeface(typeface);
textView.setText(data);
}
}
}
private static class ViewHolder {
private final TextView highlight;
private final TextView title;
private final TextView extraInfo01;
private final TextView extraInfo02;
private final TextView description;
private final ImageView image;
private final TextView subTitle;
private final TextView subDescription;
private final ImageView subImage;
public ViewHolder(final TextView highlight, final TextView title,
final TextView extraInfo01, final TextView extraInfo02,
final TextView description, final ImageView image,
final TextView subTitle, final TextView subDescription,
final ImageView subImage) {
this.highlight = highlight;
this.title = title;
this.extraInfo01 = extraInfo01;
this.extraInfo02 = extraInfo02;
this.description = description;
this.image = image;
this.subTitle = subTitle;
this.subDescription = subDescription;
this.subImage = subImage;
}
}
}