列表视图:只有一个包含多个文本视图的列表项

时间:2013-12-15 15:26:20

标签: android android-layout listview android-listview

我只需要在一个列表项中显示多个文本视图,其余的列表视图项只有一个textview。

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

谢谢。

3 个答案:

答案 0 :(得分:1)

使用自定义适配器列出并编辑getView函数:

public class MyListAdapter extends BaseAdapter {

    [...]

    @Override
    public View getView(int position, View view, ViewGroup parentView) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if(position < 1) {
                /* Inflate a layout with 4 textview */
                view = inflater.inflate(R.layout.your_4_textview_layout, parentView, false);

            } else {
                /* Inflate a layout with 1 textview */
                view = inflater.inflate(R.layout.your_1_textview_layout, parentView, false);

            }

            return view;

    }

    [...]

}

注意在构造函数中将Context上下文传递给MyListAdapter,如answer

答案 1 :(得分:1)

制作自己的适配器:

BaseAdapter已经提供了使用不同View类型(列表项单元格的不同布局)和有效回收视图的方法:

  • getViewTypeCount():存在多少个不同视图(布局)的计数。
  • getItemViewType():返回一个整数,以标识单元格中项目的视图类型。请注意,内部BaseAdapter实现使用数组。因此,此处返回的值必须为0到n。您不能跳过索引。

    公共类MyAdapter扩展了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("...");
        }
    
    }
    

    }

答案 2 :(得分:0)

您可以在适配器中进行更改...这样的事情。 @position 1:“空白”视图将被显示 @rest职位:“triplistrow”将显示

   @Override
  public View getView(final int position, View convertView, final ViewGroup parent) 
  {         
      View rowView = convertView;
      if(position !=1)
      {
      if (rowView == null) 
      {
          LayoutInflater inflater = context.getLayoutInflater();
          rowView = inflater.inflate(R.layout.triplistrow, null);
          ViewHolder viewHolder = new ViewHolder();

          viewHolder.no = (TextView) rowView.findViewById(R.id.TVTripNo);
          viewHolder.name = (TextView) rowView.findViewById(R.id.TVTripName);
          viewHolder.date = (TextView) rowView.findViewById(R.id.TVTripDate);

          rowView.setTag(viewHolder);
       }
      }
      else
      {
          LayoutInflater inflater = context.getLayoutInflater();
          rowView = inflater.inflate(R.layout.triplistrow, null);
          ViewHolder viewHolder = new ViewHolder();

          rowView.setTag(viewHolder);
      }

      if(position!=1)
      {
      final ViewHolder holder = (ViewHolder) rowView.getTag();  

      holder.no.setText(String.valueOf(position+1));
      holder.name.setText(values.get(position).name);
      holder.date.setText(values.get(position).fromdate + " to " + values.get(position).tilldate);

      }else
      {



      }


    return rowView;
  }