实际开发中个人觉得用的比较多是BaseAdapter,尽管使用起来比其他适配器有些麻烦,但是使用它却能实现很多自己喜欢的列表布局,比如ListView、GridView、Gallery、Spinner等等。它是直接继承自接口类Adapter的,使用BaseAdapter时需要重写很多方法,其中最重要的当属getView,因为这会涉及到ListView优化等问题,至于ListView优化问题本篇暂不作讨论。
BaseAdapter中需要实现一个继承自BaseAdapter的类,并且重写里面的很多方法,例如:
1 class MyAdapter extends BaseAdapter 2 { 3 private Context context; 4 public MyAdapter(Context context) 5 { 6 this.context = context; 7 } 8 @Override 9 public int getCount() { 10 // How many items are in the data set represented by this Adapter.(在此适配器中所代表的数据集中的条目数) 11 return 0; 12 } 13 @Override 14 public Object getItem(int position) { 15 // Get the data item associated with the specified position in the data set.(获取数据集中与指定索引对应的数据项) 16 return null; 17 } 18 @Override 19 public long getItemId(int position) { 20 // Get the row id associated with the specified position in the list.(取在列表中与指定索引对应的行id) 21 return 0; 22 } 23 @Override 24 public View getView(int position, View convertView, ViewGroup parent) { 25 // Get a View that displays the data at the specified position in the data set. 26 return null; 27 } 28 29 }
这里面主要是getView方法处理麻烦点,所以重点梳理下getView方法。
第一种:没有任何处理,不建议这样写。如果数据量少还将就,但是如果列表项数据量很大的时候,会每次都重新创建View,设置资源,严重影响性能,所以从一开始就不要用这种方式:
1 @Override 2 public View getView(int position, View convertView, ViewGroup parent) { 3 View item = mInflater.inflate(R.layout.list_item, null); 4 ImageView img = (ImageView)item.findViewById(R.id.img) 5 TextView title = (TextView)item.findViewById(R.id.title); 6 TextView content= (TextView)item.findViewById(R.id.content); 7 img.setImageResource(R.drawable.ic_launcher); 8 title.setText("Hello"); 9 content.setText("world"); 10 11 return item; 12 }
第二种ListView优化:通过缓存convertView,这种利用缓存contentView的方式可以判断如果缓存中不存在View才创建View,如果已经存在可以利用缓存中的View,提升了性能
1 public View getView(int position, View convertView, ViewGroup parent) { 2 if(convertView == null) 3 { 4 convertView = mInflater.inflate(R.layout.list_item, null); 5 } 6 7 ImageView img = (ImageView)convertView.findViewById(R.id.img) 8 TextView title = (TextView)convertView.findViewById(R.id.title); 9 TextView content= (TextView)ConvertView.findViewById(R.id.content); 10 img.setImageResource(R.drawable.ic_launcher); 11 title.setText("Hello"); 12 content.setText("world"); 13 14 return convertView; 15 }
注:mInflater是LayoutInflater对象一个实例,而LayoutInflater在Android中是“扩展”的意思,作用类似于findViewById(),不同的是LayoutInflater是用来获得布局文件对象的。
第三种ListView优化:通过convertView+ViewHolder来实现,ViewHolder就是一个静态类,使用 ViewHolder 的关键好处是缓存了显示数据的视图(View),加快了 UI 的响应速度。
当我们判断 convertView == null 的时候,如果为空,就会根据设计好的List的Item布局(XML),来为convertView赋值,并生成一个viewHolder来绑定converView里面的各个View控件(XML布局里面的那些控件)。再用convertView的setTag将viewHolder设置到Tag中,以便系统第二次绘制ListView时从Tag中取出。(看下面代码中)
如果convertView不为空的时候,就会直接用convertView的getTag(),来获得一个ViewHolder。
1 //在外面先定义,ViewHolder静态类 2 static class ViewHolder 3 { 4 public ImageView img; 5 public TextView title; 6 public TextView content; 7 } 8 //然后重写getView 9 @Override 10 public View getView(int position, View convertView, ViewGroup parent) { 11 ViewHolder holder; 12 if(convertView == null) 13 { 14 holder = new ViewHolder(); 15 convertView = mInflater.inflate(R.layout.list_item, null); 16 holder.img = (ImageView)item.findViewById(R.id.img) 17 holder.title = (TextView)item.findViewById(R.id.title); 18 holder.content = (TextView)item.findViewById(R.id.content); 19 convertView.setTag(holder); 20 }else 21 { 22 holder = (ViewHolder)convertView.getTag(); 23 } 24 holder.img.setImageResource(R.drawable.ic_launcher); 25 holder.title.setText("Hello"); 26 holder.content.setText("World"); 27 } 28 29 return convertView; 30 }
下面是我写一个demo,直接上代码:
activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.example.baseadapterdemo.MainActivity" > 6 7 <ListView 8 android:id="@+id/lv" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent"> 11 </ListView> 12 13 </RelativeLayout>
list_item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <LinearLayout 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" > 10 11 <LinearLayout 12 android:layout_width="wrap_content" 13 android:layout_height="match_parent" 14 android:orientation="vertical" > 15 16 <ImageView 17 android:id="@+id/img" 18 android:layout_width="wrap_content" 19 android:layout_height="match_parent" 20 android:src="@drawable/ic_launcher" /> 21 22 </LinearLayout> 23 24 <LinearLayout 25 android:layout_width="match_parent" 26 android:layout_height="match_parent" 27 android:orientation="vertical" > 28 29 <TextView 30 android:id="@+id/textView1" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:layout_weight="1" 34 android:layout_marginLeft="10dp" 35 android:gravity="center" 36 android:textSize="@dimen/activity_itle" 37 android:text="tv1" /> 38 39 <TextView 40 android:id="@+id/textView2" 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:layout_weight="2" 44 android:layout_marginLeft="10dp" 45 android:gravity="center" 46 android:textSize="@dimen/activity_content" 47 android:text="tv2" /> 48 49 </LinearLayout> 50 51 </LinearLayout> 52 53 </LinearLayout>
MainActivity.java:
1 package com.example.baseadapterdemo; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import android.app.Activity; 9 import android.os.Bundle; 10 import android.widget.ListView; 11 12 public class MainActivity extends Activity { 13 14 private ListView listView = null; 15 public List<Map<String, Object>> data; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 listView = (ListView) findViewById(R.id.lv); 23 data = getData(); 24 25 MyAdapter adapter = new MyAdapter(this); 26 adapter.setData(data); 27 listView.setAdapter(adapter); 28 29 } 30 31 public List<Map<String, Object>> getData() { 32 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 33 Map<String, Object> map; 34 for (int i = 0; i < 15; i++) { 35 map = new HashMap<String, Object>(); 36 map.put("img", R.drawable.ic_launcher); 37 map.put("title", "标题"); 38 map.put("content", "内容"); 39 list.add(map); 40 } 41 return list; 42 } 43 44 }
MyAdapter.java:
1 package com.example.baseadapterdemo; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import android.content.Context; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.BaseAdapter; 11 import android.widget.ImageView; 12 import android.widget.TextView; 13 14 public class MyAdapter extends BaseAdapter { 15 16 public LayoutInflater mInflater; 17 public Context mContext; 18 19 List<Map<String, Object>>myData; 20 21 public void setData(List<Map<String, Object>> A) { 22 this.myData = A; 23 } 24 25 public MyAdapter(Context context) { 26 //根据context上下文加载布局 27 this.mInflater = LayoutInflater.from(context); 28 mContext = context; 29 } 30 31 public MyAdapter(Context context,List<Map<String, Object>> m_Data) { 32 //根据context上下文加载布局 33 this.mInflater = LayoutInflater.from(context); 34 mContext = context; 35 this.myData = m_Data; 36 } 37 38 @Override 39 public int getCount() { 40 // TODO Auto-generated method stub 41 return myData.size(); 42 } 43 44 @Override 45 public Object getItem(int position) { 46 // TODO Auto-generated method stub 47 return position; 48 } 49 50 @Override 51 public long getItemId(int position) { 52 // TODO Auto-generated method stub 53 return position; 54 } 55 56 @Override 57 public View getView(int position, View convertView, ViewGroup parent) { 58 59 ViewHolder holder = null; 60 if (convertView == null) { 61 holder = new ViewHolder(); 62 convertView = mInflater.inflate(R.layout.list_item, null); 63 holder.imgView = (ImageView) convertView.findViewById(R.id.img); 64 holder.title = (TextView) convertView.findViewById(R.id.textView1); 65 holder.content = (TextView) convertView 66 .findViewById(R.id.textView2); 67 convertView.setTag(holder); 68 } else { 69 holder = (ViewHolder) convertView.getTag(); 70 } 71 holder.imgView.setBackgroundResource((Integer)myData.get(position).get("img")); 72 holder.title.setText((String)myData.get(position).get("title")); 73 holder.content.setText((String)myData.get(position).get("content")); 74 return convertView; 75 } 76 77 public static class ViewHolder { 78 public ImageView imgView; 79 public TextView title; 80 public TextView content; 81 } 82 83 }
运行效果: