我这样解决了:
- 我扩展了一个 BaseAdapter,其中包含所有可能的 ListItems 的列表。
- getView 使用 ViewHolderPattern 来回收视图
- View 的宽度取决于 HolderItem 。在需要的时间(它是一个水平列表,显示不同持续时间的广播)
- GetView 启动一个 AsyncTask,它需要两个 Holder,ViewHolder 和 DataHolder
- 需要 AsyncTask 来启动 onPostExecute 一个执行 GUIStuff、DrawRunnable 的线程
- 在 DrawRunnable 中,我决定如何为背景着色,以及应该根据广播的持续时间向用户呈现什么
我喜欢根据类型启动两个不同任务的方式。在我的情况下,这将是不同的持续时间类型。所以我应该在 onPostExecute 创建 DrawBroadcastRunnable 时决定哪个 DrawXTypeRunnable 应该开始。我用 if(XTYPE) 标记了代码中的位置
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewBroadcastItemHolder viewItemHolder;
if (convertView == null) {
convertView = (RelativeLayout) layoutInflater.inflate(layoutId,
parent, false);
viewItemHolder = new ViewBroadcastItemHolder();
viewItemHolder.logo = (ImageView) convertView
.findViewById(R.id.broadcast_guide_image);
viewItemHolder.time = (TextView) convertView
.findViewById(R.id.broadcast_guide_time);
viewItemHolder.title = (TextView) convertView
.findViewById(R.id.broadcast_guide_title);
viewItemHolder.divider = convertView
.findViewById(R.id.broadcast_guide_divider);
viewItemHolder.layout = (LinearLayout) convertView
.findViewById(R.id.broadcast_guide_item_linear_layout);
convertView.setTag(viewItemHolder);
} else {
viewItemHolder = (ViewBroadcastItemHolder) convertView.getTag();
convertView.setVisibility(View.GONE);
}
BroadcastHolder broadcastItem = getItem(position);
viewItemHolder.layout.getLayoutParams().width = broadcastItem.width;
BroadcastHolderAsyncArgument holder = new BroadcastHolderAsyncArgument(
convertView, viewItemHolder, broadcastItem, position);
holder.bcViewHolder.logo
.setImageBitmap(controller.getLogoPlaceholder());
BroadcastHolderAsyncArgument[] argument = { holder };
new AsyncTask<BroadcastHolderAsyncArgument, Void, BroadcastHolderAsyncArgument>() {
@Override
protected BroadcastHolderAsyncArgument doInBackground(
BroadcastHolderAsyncArgument... bcHolderArg) {
return bcHolderArg[0];
}
@Override
protected void onPostExecute(
BroadcastHolderAsyncArgument bcHolderArg) {
// Where to put in the decision which Strategy has to get done
//if(XTYPE){ new DrawBroadcast30ViewRunnable(..).run()}
//else if(YTYPE){new DrawBroadcast200ViewRunnable(..).run()}
//else if(ZTYPE){new DrawBroadcast10ViewRunnable(..).run()}
//else{new DrawBroadcast5ViewRunnable(..).run()}
new DrawBroadcastViewRunnable(bcHolderArg, position).run();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, argument);
return convertView;
}
public class DrawBroadcastViewRunnable implements Runnable {
private BroadcastHolderAsyncArgument bcAsyncArgumentHolder;
private int position;
public DrawBroadcastViewRunnable(
BroadcastHolderAsyncArgument bcHolderArgument, int position) {
super();
this.bcAsyncArgumentHolder = bcHolderArgument;
this.position = position;
}
public void run() {
if (bcAsyncArgumentHolder.position == position) {
// Set Background Color of CatchUp, Live and Upcomming
if (Helper.isLiveBroadcast(
bcAsyncArgumentHolder.bcItemHolder.start,
bcAsyncArgumentHolder.bcItemHolder.stop)) {
bcAsyncArgumentHolder.bcViewHolder.isCatchup = false;
bcAsyncArgumentHolder.convertView
.setBackgroundColor(context.getResources()
.getColor(R.color.guide_live_broadcast));
} else if (bcAsyncArgumentHolder.bcViewHolder.isCatchup = Helper
.isCatchupBroadcast(
bcAsyncArgumentHolder.bcItemHolder.start,
bcAsyncArgumentHolder.bcItemHolder.stop)) {
bcAsyncArgumentHolder.convertView
.setBackgroundColor(context.getResources()
.getColor(R.color.guide_replay_broadcast));
} else {
// Upcoming Broadcast
bcAsyncArgumentHolder.convertView
.setBackgroundColor(context.getResources()
.getColor(R.color.guide_upcoming_broadcast));
bcAsyncArgumentHolder.bcViewHolder.isCatchup = false;
}
// very large layout, more than 30min
if (bcAsyncArgumentHolder.bcItemHolder.width >= 20 * controller
.getPixelPerMinute()) {
bcAsyncArgumentHolder.bcViewHolder.logo
.setVisibility(View.VISIBLE);
ImageLoader.getInstance().displayImage(
bcAsyncArgumentHolder.bcItemHolder.imageUrl,
bcAsyncArgumentHolder.bcViewHolder.logo);
bcAsyncArgumentHolder.bcViewHolder.title
.setVisibility(View.VISIBLE);
bcAsyncArgumentHolder.bcViewHolder.time
.setVisibility(View.VISIBLE);
bcAsyncArgumentHolder.bcViewHolder.title
.setText(bcAsyncArgumentHolder.bcItemHolder.title);
bcAsyncArgumentHolder.bcViewHolder.time.setText(Helper
.formatBroadcastTime(
bcAsyncArgumentHolder.bcItemHolder.start,
bcAsyncArgumentHolder.bcItemHolder.stop));
}
// medium layout, between 20 to 15min
if (bcAsyncArgumentHolder.bcItemHolder.width < 20 * controller
.getPixelPerMinute()
&& bcAsyncArgumentHolder.bcItemHolder.width > 15 * controller
.getPixelPerMinute()) {
ImageLoader.getInstance().displayImage(
bcAsyncArgumentHolder.bcItemHolder.imageUrl,
bcAsyncArgumentHolder.bcViewHolder.logo);
bcAsyncArgumentHolder.bcViewHolder.logo
.setVisibility(View.VISIBLE);
bcAsyncArgumentHolder.bcViewHolder.time
.setVisibility(View.VISIBLE);
bcAsyncArgumentHolder.bcViewHolder.title
.setVisibility(View.VISIBLE);
bcAsyncArgumentHolder.bcViewHolder.title
.setText(bcAsyncArgumentHolder.bcItemHolder.title);
bcAsyncArgumentHolder.bcViewHolder.time.setText(Helper
.formatBroadcastTime(
bcAsyncArgumentHolder.bcItemHolder.start,
bcAsyncArgumentHolder.bcItemHolder.stop));
}
// medium layout, between 15 to 5min
if (bcAsyncArgumentHolder.bcItemHolder.width <= 15 * controller
.getPixelPerMinute()
&& bcAsyncArgumentHolder.bcItemHolder.width > 5 * controller
.getPixelPerMinute()) {
bcAsyncArgumentHolder.bcViewHolder.logo
.setVisibility(View.GONE);
bcAsyncArgumentHolder.bcViewHolder.time
.setVisibility(View.GONE);
bcAsyncArgumentHolder.bcViewHolder.title
.setVisibility(View.VISIBLE);
bcAsyncArgumentHolder.bcViewHolder.title
.setText(bcAsyncArgumentHolder.bcItemHolder.title);
}
// smal layout, to 5min
if (bcAsyncArgumentHolder.bcItemHolder.width <= 5 * controller
.getPixelPerMinute()) {
bcAsyncArgumentHolder.bcViewHolder.logo
.setVisibility(View.GONE);
bcAsyncArgumentHolder.bcViewHolder.time
.setVisibility(View.GONE);
bcAsyncArgumentHolder.bcViewHolder.title
.setVisibility(View.GONE);
}
if (bcAsyncArgumentHolder.bcItemHolder.dummy) {
bcAsyncArgumentHolder.bcViewHolder.divider
.setVisibility(View.GONE);
} else {
bcAsyncArgumentHolder.bcViewHolder.divider
.setVisibility(View.VISIBLE);
}
if (!bcAsyncArgumentHolder.bcItemHolder.id.equalsIgnoreCase("")) {
bcAsyncArgumentHolder.bcViewHolder.broadcastId = Long
.parseLong(bcAsyncArgumentHolder.bcItemHolder.id);
}
bcAsyncArgumentHolder.convertView.setVisibility(View.VISIBLE);
}
}
那么你是如何解决你的问题的呢?我的解决方案有问题,viewRecycling 导致视图混淆,导致渲染 5 分钟布局而不是 40 分钟布局,因为如果我快速滚动会有一些并发性。我知道为什么会这样,但我不知道如何解决它,但是:-D