具有2行布局的ListView的AsyncTask

时间:2013-01-26 00:01:07

标签: listview android-asynctask android-listfragment

我有一个包含2种不同行类型的列表视图(如本问题所示)。它工作正常但滚动时非常滞后。所以我读到了解决这个问题的方法是使用AsyncTask。当有两种不同的行类型使用时,我该如何实现呢?

我尝试过使用两个不同的AsyncTasks(在doInBackground中给行充气)但是我遇到了各种各样的错误:

getView()中的

if (convertView == null) {

            switch (type) {
            case TYPE_DAY:

                new ForecastDayTask(position, forecastHolder, convertView,
                        forecastHolder, mInflater, forecastdata).execute();

                break;
            case TYPE_DATA:
                new ForecastTask(position, forecastHolder, convertView,
                        forecastHolder, mInflater, forecastdata).execute();
                break;
            }

我也尝试使用相同的AsyncTask类并传递相同的数据和TYPE_DAY / TYPE_DATA,但这非常复杂,我得到了NullPointerExceptions。

有什么想法吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

  • 我扩展了一个BaseAdapter,其中包含所有可能的ListItem的列表。
  • getView使用ViewHolderPattern回收视图
  • 视图的宽度取决于HolderItem resp。在所需时间(其水平列表显示具有不同持续时间的广播)
  • GetView启动一个AsyncTask,它带有两个Holders,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