0

我有一个包含 3 种元素的 ListView。Rows 和 QueueRows 是一组有点复杂的子控件,而 HeaderRow 只包含 textView。我所做的是添加 HeaderRow、少数 QueueRows、headerRow 和少数行。QueueRow 需要每秒更新一次。这就是为什么我编写了一个在适配器上调用 notifyDataSetChanged 的​​代码。它每秒更新一次 queuerow,但也有一个问题——我的两个标题交换了半秒钟的位置。每 0.5 秒后他们交换一次。你有任何想法如何防止它们?

那是我的代码:

class BuildingsAdapter extends ArrayAdapter<BaseRow> {

private static Bitmap SOURCE_BITMAP;

private MainActivity mainActivity_;

private Handler handler_;
private Runnable runnable_;

public BuildingsAdapter(MainActivity mainActivity) {

    super(mainActivity, R.layout.row);
    this.mainActivity_ = mainActivity;
    SOURCE_BITMAP = BitmapFactory.decodeResource(
            mainActivity_.getResources(), R.drawable.images);
    handler_ = new Handler();
    runnable_ = new Runnable() {

        @Override
        public void run() {

            notifyDataSetChanged();
            if (hasQueueItems())
                handler_.postDelayed(runnable_, 1000);
        }
    };
}

@Override
public void add(BaseRow object) {

    if (!hasQueueItems() && object.getClass().equals(QueueRow.class))
        handler_.postDelayed(runnable_, 1000);
    super.add(object);
}

private boolean hasQueueItems() {

    for (int i = 0; i < getCount(); ++i) {
        if (getItem(i).getClass().equals(QueueRow.class))
            return true;
    }

    return false;
}

@Override
public int getItemViewType(int position) {

    return getItem(position).getType();
}

@Override
public int getViewTypeCount() {

    return 3;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    int type = getItemViewType(position);

    View row = convertView;

    switch (type) {
    case BaseRow.QUEUE_ROW: {
        QueueRowViewHolder holder = new QueueRowViewHolder();
        //whatever
                    return convertView;
    }
    case BaseRow.ROW: {
        RowViewHolder holder = new RowViewHolder();
        //whatever
                    return convertView;
    }
    case BaseRow.HEADER_ROW: {
        HeaderViewHolder holder = new HeaderViewHolder();
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.header_row, parent, false);

            HeaderRow headerRow = (HeaderRow) getItem(position);

            holder.nameTextView_ = (TextView) row
                    .findViewById(R.id.categoryNameTextView);
            holder.nameTextView_.setText(headerRow.getName());

            row.setTag(holder);
        } else {
            holder = (HeaderViewHolder) row.getTag();
        }
        return row;
    }
    }
    return null;
}

static class QueueRowViewHolder {

    ImageView qItemImageView_;
    TextView qItemNameTextView_;
    ProgressBar qProgressBar_;
    TextView qLevelTextView_;
    TextView qTimeTextView_;
    int position_;
}

static class RowViewHolder {

    ImageView itemImageView_;
    TextView nameTextView_;
    TextView levelTextView_;
    TextView woodTextView_;
    TextView ironTextView_;
    TextView stoneTextView_;
    TextView goldTextView_;
    Button actionButton_;
    TextView timeTextView_;
}

static class HeaderViewHolder {

    TextView nameTextView_;
}
4

1 回答 1

0

好的!我发现了——我没有设置 HeaderRow 的值;)

于 2013-06-22T00:34:33.333 回答