zoukankan      html  css  js  c++  java
  • ListView使用item显示不同布局

     1   /**
     2      * 自定义城市列表适配器
     3      */
     4     private class MyCityListAdapter extends BaseAdapter {
     5 
     6         final int VIEW_TYPE = 2;
     7         final int TYPE_1 = 0;
     8         final int TYPE_2 = 1;
     9 
    10         @Override
    11         public int getCount() {
    12             return cityList.size();
    13         }
    14 
    15         @Override
    16         public Object getItem(int position) {
    17             return cityList.get(position);
    18         }
    19 
    20         @Override
    21         public long getItemId(int position) {
    22             return position;
    23         }
    24 
    25         @Override
    26         public int getItemViewType(int position) {
    27 
    28             if (position == 3||position == 5||position == 10)
    29                 return TYPE_1;
    30             else
    31                 return TYPE_2;
    32         }
    33 
    34         @Override
    35         public int getViewTypeCount() {
    36             return VIEW_TYPE;
    37         }
    38 
    39         ViewHolder viewHolder;
    40 
    41         @Override
    42         public View getView(int position, View convertView, ViewGroup parent) {
    43 
    44             viewHolder = new ViewHolder();
    45             if (convertView == null) {
    46 
    47                 //对listView设置不同布局
    48                 int type = getItemViewType(position);
    49                 Log.e("打印值", "getView " + position + " " + convertView + " type = " + type);
    50 
    51                 switch (type) {
    52                     case TYPE_1:
    53                         convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.city_view_item_sort, null);
    54                         break;
    55                     case TYPE_2:
    56                         convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.city_view_item, null);
    57                         break;
    58                 }
    59                 convertView.setTag(viewHolder);
    60             } else {
    61                 viewHolder = (ViewHolder) convertView.getTag();
    62 
    63             }
    64 
    65             viewHolder.textView = (TextView) convertView.findViewById(R.id.cityName);
    66             viewHolder.textView.setText(cityList.get(position).getCityName());
    67             return convertView;
    68         }
    69     }
    70 
    71     private class ViewHolder {
    72         private TextView textView;
    73     }
  • 相关阅读:
    ubuntu 16.04 tmux
    ubuntu 16.04 samba 文件共享
    ubuntu 16.04 有道词典
    ubuntu bless 16字节每行
    Win7任务栏图标大小调整为等宽
    ubuntu 16.04 vnc server
    ubuntu 16.04 U盘多媒体不自动弹出
    Linux录屏软件
    通过apt-get安装nvidia驱动
    调试X Server
  • 原文地址:https://www.cnblogs.com/yoyohong/p/5974008.html
Copyright © 2011-2022 走看看