列表视图:与多个textviews只有一个列表项 [英] Listview: Only one list item with multiple textviews

查看:155
本文介绍了列表视图:与多个textviews只有一个列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示只有一个列表项,列表视图其余项目多文本视图将只有一个TextView的。

I need to show multiple text views in only one list item and the rest of the list view items will have just one textview.

我如何实现这一目标?任何样本或教程你可以点我?

How do I achieve this? Any samples or tutorials you can point me at?

感谢您。

推荐答案

请自己的适配器:

BaseAdapter已经防阻方法不同视图类型的工作(用于列表项的细胞不同的布局)和ef​​fektivly回收意见:

BaseAdapter already provieds methods for working with different View types (different layout for list item cells) and to effektivly recycle views:


  • getViewTypeCount():多种不同的视图(布局)如何present计数。

  • getItemViewType():返回一个整数,以确定在所述小区中的项目的视图类型。注意内部BaseAdapter实现使用数组。因此,在这里你的返回值必须是从0到n。你不允许跳过索引。

  • getViewTypeCount(): The count of how many different Views (layouts) are present.
  • getItemViewType(): Returns a integer to identify the view type of the item in the cell. Note the internally BaseAdapter implementation uses an array. Therefore your values returned here must be from 0 to n. You are not allowed to skip indexes.

公共类MyAdapter延伸BaseAdapter {

public class MyAdapter extends BaseAdapter {

private final int VIEW_TYPE_NORMAL = 0;
private final int VIEW_TYPE_4_TEXTS = 1;

/**
 * The inflater for
 */
protected LayoutInflater inflater;
protected Context context;

public MyAdapter(Context context) {
    this.context = context;
    this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getViewTypeCount() {
    // There are two view types
    return 2;
}

@Override
public int getItemViewType(int position) {
    if (position == 0)
        return VIEW_TYPE_4_TEXTS;
    else
        return VIEW_TYPE_NORMAL;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    int type = getItemViewType(position);
    if (convertView == null) {
        convertView = newView(type, parent);
    }
    bindView(position, type, convertView);
    return convertView;
}

@Override
public long getItemId(int position) {
    return 0; // replace it with your id, if you have stable ids in your
                // adapter
}

/** Inflates the correct view accorind the view type **/
public View newView(int type, ViewGroup parent) {

    if (VIEW_TYPE_4_TEXTS == type) {
        View view = inflater.inflate(R.layout.your_layout_with_4_textviews,
                parent, false);
        view.setTag(new ViewHolder4TextViews(view)); // ViewHolder pattern

        return view;
    }

    // Otherwise its a normal item with VIEW_TYPE_NORMAL
    View view = inflater
            .inflate(R.layout.your_normal_layout, parent, false);
    view.setTag(new NormalViewHolder(view)); // ViewHolder pattern

    return view;

}

/** Bind the data for the specified {@code position} to the {@code view}. */
public void bindView(int position, int type, View view) {

    if (VIEW_TYPE_4_TEXTS == type) {
        // set the 4 text view values
        ViewHolder4TextViews holder = (ViewHolder4TextViews) view.getTag();

        holder.textview1.setText("...");
        holder.textview2.setText("...");
        holder.textview3.setText("...");
        holder.textview4.setText("...");

    } else {
        // VIEW_TYPE_NORMAL
        NormalViewHolder holder = (NormalViewHolder) view.getTag();

        holder.textview.setText("...");
    }

}

}

这篇关于列表视图:与多个textviews只有一个列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆