码迷,mamicode.com
首页 > 移动开发 > 详细

一起学android之BaseAdapter使用(14)

时间:2015-01-04 21:21:51      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:android   android教程   java   baseadapter   适配器   

扩展BaseAdapter可以对各列表项进行最大限度的定制,具体描述请看以下代码:

baseadapter_list布局文件:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv_baseadapter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout></span>

baseadapter布局文件:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout 
        android:id="@+id/ll_top"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal">
    <ImageView 
        android:id="@+id/iv_image"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/a5i"
        android:layout_gravity="center"/>
    <LinearLayout 
        android:id="@+id/ll_right"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="4dp">
        <TextView 
            android:id="@+id/tv_title"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="20sp"
            android:text="AS"/>
         <TextView 
            android:id="@+id/tv_des"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="13sp"
            android:text="AS"
            />
    </LinearLayout>
</LinearLayout>
</RelativeLayout></span>

BaseAdapterTest主文件:

<span style="font-size:18px;">public class BaseAdapterTest extends Activity {
	int[] images = new int[] { R.drawable.a5i, R.drawable.a5j, R.drawable.a5k };
	String[] titles = new String[] { "电话", "QQ", "联系人" };
	String[] des = new String[] { "当前无电话记录", "当前无QQ聊天记录", "最近无联系人" };
	
	private ListView lv_baseadapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.baseadapter_list);
		initView();
		setDate();
	}
	
	private void initView(){
		//注册控件
		lv_baseadapter=(ListView)findViewById(R.id.lv_baseadapter);
		//注册单击事件
		lv_baseadapter.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(BaseAdapterTest.this, "点击了第"+position+"项", Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	private void setDate(){
		//绑定适配器
		lv_baseadapter.setAdapter(baseAdapter);
	}
	
	BaseAdapter baseAdapter=new BaseAdapter() {
		/*
		 *返回第postion处的列表项组件
		 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder=new ViewHolder();
			if(null==convertView){
				LayoutInflater layoutInflater=LayoutInflater.from(BaseAdapterTest.this);
				convertView=layoutInflater.inflate(R.layout.baseadapter, null);
				viewHolder.iv_image=(ImageView)convertView.findViewById(R.id.iv_image);
				viewHolder.tv_title=(TextView)convertView.findViewById(R.id.tv_title);
				viewHolder.tv_desc=(TextView)convertView.findViewById(R.id.tv_des);
				/*
				 * 将当前的View和ViewHolder绑定
				 */
				convertView.setTag(viewHolder);
			}else{
				/*
				 * 为已经取得绑定的View调用getTag()方法,当前的view就不必重新绘制,
				 * 减少内存资源。
				 */
				viewHolder=(ViewHolder)convertView.getTag();
			}
			viewHolder.iv_image.setImageResource(images[position]);
			viewHolder.tv_title.setText(titles[position]);
			viewHolder.tv_desc.setText(des[position]);
			return convertView;
		}
		/*
		 * 返回第postion处的列表项的ID
		 * @see android.widget.Adapter#getItemId(int)
		 */
		@Override
		public long getItemId(int position) {
			return position;
		}
		/*
		 * 返回第postion处的列表项的内容。该方法在事件监听时会调用
		 * @see android.widget.Adapter#getItem(int)
		 */
		@Override
		public Object getItem(int position) {
			return position;
		}
		/*
		 * 返回该Adapter将会包含多少个列表项
		 * @see android.widget.Adapter#getCount()
		 */
		@Override
		public int getCount() {
			return titles.length;
		}
	}; 
	private class ViewHolder{
		ImageView iv_image;
		TextView tv_title;
		TextView tv_desc;
	}
}</span>


                                                                                                 技术分享





转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/42400261  情绪控_



一起学android之BaseAdapter使用(14)

标签:android   android教程   java   baseadapter   适配器   

原文地址:http://blog.csdn.net/hai_qing_xu_kong/article/details/42400261

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