android中的适配器(Adapter)是数据与视图(View)之间的桥梁,用于对要显示的数据进行处理,并通过绑定到组件进行数据的显示。

  BaseAdapter是Android应用程序中经常用到的基础数据适配器的基类,它实现了Adapter接口。其主要用途是将一组数据传到像ListView、Spinner、Gallery及GridView等UI显示组件进行显示。我们经常使用的ListView 的adapter(即SimpleAdapter),是继承自BaseAdapter基类的。BaseAdapter是一个基类,没有实现绑定数据的功能。而SimpleAdapter实现了基本控件的绑定,如TextView,Button,ImageView等。并已经为我们实现好了数据优化工作。

  这些适配器使用相同组件动态绑定数据的方式进行优化。为什么需要优化呢?因为如果我们有上亿个(较多个)项目要显示怎么办?为每个项目创建一个新视图?这不可能,因为内存有限制。实际上Android为你缓存了视图。Android中有个叫做Recycler的构件,下图是他的工作原理:

  如果你有10亿个项目(item),其中只有可见的项目存在内存中,其他的在Recycler中。其实我的理解Recyler就是一个队列,用来存储不在屏幕范围内的item,如果item滚出屏幕范围,那么就入队,这里的滚出是完全滚出,即边界等也要完全滚出。如果新的item要滚进来,那么android系统的framework就会查看Recyler是否含有可以重复使用的View,如果有那么就重新设置该View 的数据源,然后显示,即出队。那么这么多的item其实只需要占用一定空间的内存,这个内存大小是多少呢?我的感觉是手机屏幕所包含的item的个数,再加上1,然后乘以每个item占用的内存。但是最后我发现是加上2.可能是为了使得缓存更大吧。。。。但是为什么加上2,大家应该理解,如果你不理解,那你就把滚动list的过程好好想一想。那个队列无非就是一个缓存罢了,因为我们的目的是通过那个缓存来重复使用那些已经创建的View。

  使用BaseAdapter的话需要重载四个方法,这些方法分别是getView()、getCount()、getItem()和getItemId(),其中getView()最为重要。那么getView函数为什么重要呢?因为它是用来刷新它所在的ListView的。它在什么时候调用的呢?就是在每一次item从屏幕外滑进屏幕内的时候,或者程序刚开始的时候创建第一屏item的时候。下面分别对这几个函数进行一个介绍:

  1. getView()

    先看看官方API的解释: 

public abstract View getView (int position, View convertView, ViewGroup parent)

Since: API Level 1

Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use inflate(int, android.view.ViewGroup, boolean) to specify a root view and to prevent attachment to the root.

Parameters

positionThe position of the item within the adapter's data set of the item whose view we want.
convertViewThe old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see getViewTypeCount() and getItemViewType(int)).
parentThe parent that this view will eventually be attached to

Returns

  • A View corresponding to the data at the specified position.

  position是指当前dataset的位置,通过getCount和getItem来使用。如果list向下滑动的话那么就是最低端的item的位置,如果是向上滑动的话那就是最上端的item的位置。convert是指可以重用的视图,即刚刚出队的视图(在上面已经重点讲过)。parent应该就是显示数据的视图(如ListView、GridView等)。

  2. getCount()

    作用:主要是获得项目(Item)的数量。    

    官方API:    

    int getCount()

      How many items are in the data set represented by this Adapter. 

   返回:

    Count of items.

  3.getItem()

    作用:主要是获得当前选项。注意返回值类型

    官方API:    

     Object getItem(int position)

       Get the data item associated with the specified position in the data set.

   参数:

  position - Position of the item whose data we want within the adapter's data set.

  返回:

  The data at the specified position.

  4. getItemId()

    作用:主要是获得当前选项的ID。

    官方API:    

    long getItemId(int position)

     Get the row id associated with the specified position in the list. 

  参数:

  position - The position of the item within the adapter's data set whose row id we want.

  返回:

  The id of the item at the specified position.

  为了更好的理解,下面给出个一个实际的BaseAdapter使用方法(该完整例子可参考明日科技Android从入门到精通第五章例程5.9):

复制代码

 1 public class MainActivity extends Activity {
 2 
 3     public int[] imageId = new int[] { R.drawable.img01, R.drawable.img02,
 4             R.drawable.img03, R.drawable.img04, R.drawable.img05,
 5             R.drawable.img06, R.drawable.img07, R.drawable.img08,
 6             R.drawable.img09, R.drawable.img10, R.drawable.img11,
 7             R.drawable.img12}; // 定义并初始化保存图片id的数组
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.main); // 设置该Activity使用的布局
13         GridView gridview = (GridView) findViewById(R.id.gridView1); // 获取GridView组件
14         BaseAdapter adapter = new BaseAdapter() {
15             @Override
16             public View getView(int position, View convertView, ViewGroup parent) {
17                 ImageView imageview; // 声明ImageView的对象
18                 if (convertView == null) {//判断recycler中是否有可用的View
19                     imageview = new ImageView(MainActivity.this); // 实例化ImageView的对象
20                     /************* 设置图像的宽度和高度 ******************/
21                     imageview.setAdjustViewBounds(true);
22                     imageview.setMaxWidth(180);
23                     imageview.setMaxHeight(135);
24                     /**************************************************/
25                     imageview.setPadding(5, 5, 5, 5); // 设置ImageView的内边距
26                 } else {
27                     imageview = (ImageView) convertView;
28                 }
29                 imageview.setImageResource(imageId[position]); // 为ImageView设置要显示的图片,即设置数据源
30                 return imageview; // 返回ImageView
31             }
32 
33             /*
34              * 功能:获得当前选项的ID
35              */
36             @Override
37             public long getItemId(int position) {
38                 return position;
39             }
40 
41             /*
42              * 功能:获得当前选项
43              */
44             @Override
45             public Object getItem(int position) {
46                 return position;
47             }
48 
49             /*
50              * 获得数量
51              */
52             @Override
53             public int getCount() {
54                 return imageId.length;
55             }
56         };
57 
58         gridview.setAdapter(adapter); // 将适配器与GridView关联
59                 //为gridView的每一项(Item)设置单击事件监听
60         gridview.setOnItemClickListener(new OnItemClickListener() {
61             @Override
62             public void onItemClick(AdapterView<?> parent, View view,
63                     int position, long id) {
64                 Intent intent = new Intent(MainActivity.this, BigActivity.class);
65                 Bundle bundle = new Bundle(); // 创建并实例化一个Bundle对象
66                 bundle.putInt("imgId", imageId[position]); // 保存图片ID
67                 intent.putExtras(bundle); // 将Bundle对象添加到Intent对象中
68                 startActivity(intent); // 启动新的Activity
69 
70             }
71         });
72 
73     }
74 }
75     

复制代码

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐