自定义ListView中的item

[功能]
* AdapterView
- ListView
- GridView
- Gallery
- Spinner

* Adapter
- SimpleAdapter
- SimpleCursorAdapter
- ArrayAdapter

至于 AdapterView & Adapter 如何选择的问题 有2点需要注意:
× AdapterView  的选择 只和界面有关 和具体数据无关
× Adapter 的选择 只喝数据有关 和界面无关
二者耦合度高 互不干涉!

android给出的AdapterView中所使用的Adapter的item都是TextView 即 只能显示一下文字信息 这就限制了它的应用 所以现在告诉大家怎么使用别的View


[思路]
1. 自定义一个 extends BaseAdapter 的 class 如 public class CustomList extends BaseAdapter
2. 填充 CustomList 的一些方法 如下:

Java代码 复制代码
  1. public int getCount()   
  2. public Object getItem(int position)   
  3. public long getItemId(int position)   
  4. public View getView(int position, View convertView, ViewGroup parent)  
public int getCount()
public Object getItem(int position)
public long getItemId(int position)
public View getView(int position, View convertView, ViewGroup parent)




[代码]
1. 比如 现在有下列数据 要求显示之

Java代码 复制代码
  1. String[] week = {   
  2.     "JAN","FEB","MAR","APR",   
  3.             "MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC "  
  4.     };  
String[] week = {
	"JAN","FEB","MAR","APR",
            "MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC "
	};




2. 一些函数的定义如下

Java代码 复制代码
  1. public class CustomList extends BaseAdapter {   
  2.         Activity activity;   
  3.            
  4.         //construct   
  5.         public CustomList(Activity a ) {   
  6.             activity = a;   
  7.         }   
  8.            
  9.         @Override  
  10.         public int getCount() {   
  11.             // TODO Auto-generated method stub   
  12.             return week.length;   
  13.         }   
  14.   
  15.         @Override  
  16.         public Object getItem(int position) {   
  17.             // TODO Auto-generated method stub   
  18.             return week[position];   
  19.         }   
  20.   
  21.         @Override  
  22.         public long getItemId(int position) {   
  23.             // TODO Auto-generated method stub   
  24.             return position;   
  25.         }   
  26.   
  27.         @Override  
  28.         public View getView(int position, View convertView, ViewGroup parent) {   
  29.             // TODO Auto-generated method stub   
  30.             TextView tv = new TextView(activity);   
  31.             tv.setText(week[position]);   
  32.             return tv;   
  33.         }   
  34.            
  35.     }  
public class CustomList extends BaseAdapter {
    	Activity activity;
    	
    	//construct
    	public CustomList(Activity a ) {
    		activity = a;
    	}
    	
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return week.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return week[position];
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView tv = new TextView(activity);
			tv.setText(week[position]);
			return tv;
		}
    	
    }



3. 考虑到美观 我们可以把getView()的一些填充提取出来 即 根据目标的position 得到目标所需View

Java代码 复制代码
  1. public View addCustomView(int position){   
  2.             View view = new View(activity);   
  3.                
  4.             switch(position){   
  5.             case 11:   
  6.                 Button btn = new Button(activity);   
  7.                 btn.setText("Yes!");   
  8.                    
  9.                 view = btn;   
  10.             case 12:   
  11.                 ImageView iv = new ImageView(activity);   
  12.                 iv.setImageResource(R.drawable.robot);   
  13.                    
  14.                 view = iv;   
  15.                    
  16.                 break;   
  17.             default:   
  18.                 TextView tv = new TextView(activity);   
  19.                 tv.setGravity(1);   
  20.                 tv.setText(week[position]);   
  21.                 tv.setPadding(5555);   
  22.                 view = tv;   
  23.             }   
  24.                
  25.             return view;   
  26.         }  
public View addCustomView(int position){
			View view = new View(activity);
			
			switch(position){
			case 11:
				Button btn = new Button(activity);
				btn.setText("Yes!");
				
				view = btn;
			case 12:
				ImageView iv = new ImageView(activity);
				iv.setImageResource(R.drawable.robot);
				
				view = iv;
				
				break;
			default:
				TextView tv = new TextView(activity);
				tv.setGravity(1);
				tv.setText(week[position]);
				tv.setPadding(5, 5, 5, 5);
				view = tv;
			}
			
			return view;
		}



4. 如何使用

Java代码 复制代码
  1. public View getView(int position, View convertView, ViewGroup parent) {   
  2.             // TODO Auto-generated method stub   
  3.             return addCustomView(position);   
  4.         }  
public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			return addCustomView(position);
		}




这样 我们如果要定制某个position的View 就可以增加相应的case..


done!

 

转载来自:http://www.javaeye.com/topic/568927

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值