ListView里面adapter的不同分类的item

public class PlayAdapter extends BaseAdapter {
  /**
   * 标题的item
   */
  public static final int ITEM_TITLE = 0;
  /**
   * 二级菜单的item
   */
  public static final int ITEM_INTRODUCE = 1;
  private List<ViewItem> mList;
  private Context context;

  private LayoutInflater inflater;
  public PlayAdapter(Context context, List<ViewItem> mList) {
    this.context = context;
    this.mList = mList;
    inflater = LayoutInflater.from(context);
  }

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    System.out.println("mList.size()" + mList.size());
    return mList.size();
  }

  @Override
  public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return mList.get(arg0);
  }
  //返回 代表某一个样式 的 数值  
  @Override
  public int getItemViewType(int position) {     //返回类型
    // TODO Auto-generated method stub
    return mList.get(position).type;
  }

  //两个样式 返回2
  @Override
  public int getViewTypeCount() {           //不同的布局共2 个
    // TODO Auto-generated method stub    
    return 2;
  }

  @Override
  public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    int type = getItemViewType(position);  //取得类型
    Holder1 holder1 = null;
    Holder2 holder2 = null;
    System.out.println("getView " + position + " " + convertView
        + " type = " + type);
    if (convertView == null) {
          //选择某一个样式。。
          switch (type) {
              case ITEM_TITLE:  //一级菜单
                    convertView = inflater.inflate(R.layout.play_item_title, null);
                    holder1 = new Holder1(convertView);
                    holder1.play_title.setText(mList.get(position).name);
                    convertView.setTag(holder1);
                break;
              case ITEM_INTRODUCE:   //二级菜单
                    convertView = inflater.inflate(R.layout.paly_item_introduce,null);
                    holder2 = new Holder2(convertView);
                    holder2.play_introduce_title
                        .setText(mList.get(position).address);
                    convertView.setTag(holder2);
                    break;
              default:
                break;
          }
    } else {   //复用view
      switch (type) {   ////取得类型
          case ITEM_TITLE:
            holder1 = (Holder1) convertView.getTag();
            holder1.play_title.setText(mList.get(position).name);
            break;
          case ITEM_INTRODUCE:
            holder2 = (Holder2) convertView.getTag();
            holder2.play_introduce_title
                .setText(mList.get(position).address);
            break;

          default:
            break;
      }

    }

    return convertView;
  }
  
  // 两个样式 两个holder。100就写100holder。。当然你何以把他抽离出来这里先只为了说明问题
  class Holder1 {
    TextView play_title;

    Holder1(View view) {
      play_title = (TextView) view.findViewById(R.id.play_title);
    }

  }
  class Holder2 {
        TextView play_introduce_title;
        ImageView play_iv;
        Holder2(View view) {
              play_introduce_title = (TextView) view.findViewById(R.id.play_introduce_title);
              play_iv = (ImageView) view.findViewById(R.id.play_iv);
        }
  }

}

 

posted @ 2015-06-05 12:04  每天学习一点点...  阅读(1753)  评论(0编辑  收藏  举报