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
position The position of the item within the adapter's data set of the item whose view we want.
convertView The 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)).
parent The 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):

 public class MainActivity extends Activity {

     public int[] imageId = new int[] { R.drawable.img01, R.drawable.img02,
R.drawable.img03, R.drawable.img04, R.drawable.img05,
R.drawable.img06, R.drawable.img07, R.drawable.img08,
R.drawable.img09, R.drawable.img10, R.drawable.img11,
R.drawable.img12}; // 定义并初始化保存图片id的数组 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // 设置该Activity使用的布局
GridView gridview = (GridView) findViewById(R.id.gridView1); // 获取GridView组件
BaseAdapter adapter = new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageview; // 声明ImageView的对象
if (convertView == null) {//判断recycler中是否有可用的View
imageview = new ImageView(MainActivity.this); // 实例化ImageView的对象
/************* 设置图像的宽度和高度 ******************/
imageview.setAdjustViewBounds(true);
imageview.setMaxWidth();
imageview.setMaxHeight();
/**************************************************/
imageview.setPadding(, , , ); // 设置ImageView的内边距
} else {
imageview = (ImageView) convertView;
}
imageview.setImageResource(imageId[position]); // 为ImageView设置要显示的图片,即设置数据源
return imageview; // 返回ImageView
} /*
* 功能:获得当前选项的ID
*/
@Override
public long getItemId(int position) {
return position;
} /*
* 功能:获得当前选项
*/
@Override
public Object getItem(int position) {
return position;
} /*
* 获得数量
*/
@Override
public int getCount() {
return imageId.length;
}
}; gridview.setAdapter(adapter); // 将适配器与GridView关联
//为gridView的每一项(Item)设置单击事件监听
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this, BigActivity.class);
Bundle bundle = new Bundle(); // 创建并实例化一个Bundle对象
bundle.putInt("imgId", imageId[position]); // 保存图片ID
intent.putExtras(bundle); // 将Bundle对象添加到Intent对象中
startActivity(intent); // 启动新的Activity }
}); }
}

以后有更深的理解或者有更好的参考再补充。。。

android开发中的BaseAdapter之理解(引用自网络,总结的很好,谢谢)的更多相关文章

  1. 在Android开发中如何判读当前设备是否连接网络

    1:前言: 我们在Android开发的过程中,很多实现是要向远程服务器拿数据的,但是未必当前设备一定连接了网络啊,那么此时我们就是要进行判断的了, 如果是有网络的话,那么此时就去向远程服务器去拿数据, ...

  2. Android开发中Handler的经典总结--转载至网络

    一.Handler的定义: 主要接受子线程发送的数据, 并用此数据配合主线程更新UI. 解释:当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件 ...

  3. Android开发中常见的设计模式

    对于开发人员来说,设计模式有时候就是一道坎,但是设计模式又非常有用,过了这道坎,它可以让你水平提高一个档次.而在android开发中,必要的了解一些设计模式又是非常有必要的.对于想系统的学习设计模式的 ...

  4. Android开发中常见的设计模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. Android开发中JavaBean类和序列化知识的理解

    原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6296121.html Android开发中,我们经常用到JavaBean类以及序列化的知识,但经 ...

  6. Android 开发中,as或者idea对gradle的使用

    原文:Android 开发中,as或者idea对gradle的使用 本文属于转载收藏,侵删,出处:私人博客 ---------------------------------------------- ...

  7. 在android开发中使用multdex的方法-IT蓝豹为你整理

    Android系统在安装应用时,往往需要优化Dex,而由于处理工具DexOpt对id数目的限制,导致其处理的数目不能超过65536个,因此在Android开发中,需要使用到MultiDex来解决这个问 ...

  8. Android开发中Eclispe相关问题及相应解决(持续更新)

    1.Eclipse项目中的Android Private Libraries没有自动生成. 一般而言,在Android开发中,项目中引用到的jar包会放到项目目录中的libs中,引入库会放到Andro ...

  9. Android开发中的问题及相应解决(持续更新)

    最近博客写的少了,以后还得经常更新才行. ------------------------------------------------------------ 1.特定业务需求下try cath ...

随机推荐

  1. mongodb spring 集成

    参考文档 mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?op ...

  2. swift学习之-- UIAlertViewController -alert

    // //  ViewController.swift //  alertView // //  Created by su on 15/12/7. //  Copyright © 2015年 tia ...

  3. POJ1789 Truck History 2017-04-13 12:02 33人阅读 评论(0) 收藏

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27335   Accepted: 10634 D ...

  4. 团体程序设计天梯赛L1-020 帅到没朋友 2017-03-22 17:46 72人阅读 评论(0) 收藏

    L1-020. 帅到没朋友 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为 ...

  5. Ubuntu再图形登录中以root的身份进入???

    Ubuntu再图形登录中以root的身份进入??? 这样做的需求,应该就是,可以再图形页面以root的身份进行图形化操作,比较方便更改配置文件. 1. 可以实现,但是不建议这么做,之后会出现一个警告提 ...

  6. [label][HTML5-APP]通过使用HTML5特性开发WebApp的Framework收集

    1. jQuery mobile  http://jquerymobile.com 2. Intel's App Framework  http://app-framework-software.in ...

  7. Android-自定义开关(升级版)

    效果图: 定义一个类,取名为MySwitch.java,此类去继承View,为何是继承View而不是去继承ViewGroup呢,是因为自定义开关没有子控件,之需要操作自身绘制即可 package cu ...

  8. XML--使用XML来将字符串分隔成行数据

    DECLARE @xml XML SET @xml=CAST(REPLACE('<ROOT><X>'+'AA,AB,AC,AD'+'</X></ROOT> ...

  9. xml与json的区别和总结

    JSON和XML的比较 ◆可读性 JSON和XML的可读性可谓不相上下,一边是简易的语法,一边是规范的标签形式,很难分出胜负. ◆可扩展性 XML天生有很好的扩展性,JSON当然也有,没有什么是XML ...

  10. eFrameWork学习笔记-eOleDB

    eOleDB是eFrameWork框架下基础的数据访问类,用于执行SQL语句,返回DataTable,分页,返回数据库所有库,库的所有表,表的所有列,Json导入.导出等. HTML: <div ...