码迷,mamicode.com
首页 > 其他好文 > 详细

ListView里面adapter的不同分类的item

时间:2015-06-05 13:54:10      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:

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);
        }
  }

}

 

ListView里面adapter的不同分类的item

标签:

原文地址:http://www.cnblogs.com/java-g/p/4554296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!