zoukankan      html  css  js  c++  java
  • 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);
            }
      }
    
    }
  • 相关阅读:
    Java的Regex --正则表达式
    Java的包装类
    类的始祖Object
    abstract和interface关键字介绍
    内部类
    Accumulation Degree [换根dp,二次扫描]
    牛客练习赛61 [口胡]
    CF1334G Substring Search [bitset,乱搞]
    CF1175F The Number of Subpermutations [哈希,乱搞]
    CF793G Oleg and chess [线段树优化建边,扫描线,最大流]
  • 原文地址:https://www.cnblogs.com/java-g/p/4554296.html
Copyright © 2011-2022 走看看