您的位置:首页 > 移动开发 > Android开发

Android学习小结(一)#2拾遗

2011-03-19 02:19 393 查看
上次说到Activity,我突然感觉到忘记说了一个很重要的东西,数据绑定。既然说到数据绑定,那么我就顺便说说本来不计划要说的User Interface。

User Interface,简称UI,也就是用户交互的界面。主要的用到的东西都在android.widget这个包下,下面的类都主要继承自View。android开发者中心的Resources有个《Hello Views》的Tutorials。可以通过这个大致了解一下。也可以参看Api Demos这个项目(SDK中自带),这是个很好的东西。

这里我主要说说ListView这个类。(事实上是我只会点ListView。)

(说点题外话:Java这套命名习惯我还是挺不习惯的,在语义上我过去的经验一点便宜也没占到。)

好了,现在我们有了View,我们需要Model了,Model就看具体需求了。View和Model都好了以后,怎么把它俩联系起来呢?

这里涉及一些重要的类,叫做XXAdapter。根据不同的Model形式,需要不同的Adapter。

最简单的应该是ArrayAdapter,故名思议,就是数组适配器,这个只能绑定一个字段。复杂点的SimpleAdapter(虽然它叫Simple),可以绑定多个字段。然后有针对Sqlite数据库的CursorAdapter和SimpleCursorAdapter。主要说一下SimpleAdapter吧。(同上,事实上我也只用过这个。)

SimpleAdapter翻译过来是简单适配器。从构造函数SimpleAdapter(Context context, List> data, int resource, String[] from, int[] to)看到from和to这两个参数,这样语义就明朗一些了。大意为从from到to的绑定。参数中,context是上下文,一般是当前活动;data是一个键值对列表,键是String类型;resource是布局资源ID,是指ListView的每一个Item的布局资源;from和int是一一对应的,from为data中的键,to是布局资中View的资源ID,这也解释了为什么from是string类型,而to是int类型。

这样表现层和数据层就分离开来,实现了解耦。大体思想是,我数据按照一定的规范可以随意做变更,你就通知view就好了,view会自动进行更新(当然这个是靠Adapter帮助的)。数据没有View的半点概念。

我从android的reference中SimpleAdapter该类的介绍中摘录了一段比较重要的话,如下:


An easy adapter to map static data to views defined in an XML file. You can specify the data backing the list as an ArrayList of Maps. Each entry in the ArrayList corresponds to one row in the list. The Maps contain the data for each row. You also specify an XML file that defines the views used to display the row, and a mapping from keys in the Map to specific views. Binding data to views occurs in two phases. First, if a SimpleAdapter.ViewBinder is available, setViewValue(android.view.View, Object, String) is invoked. If the returned value is true, binding has occurred. If the returned value is false, the following views are then tried in order:

A view that implements Checkable (e.g. CheckBox). The expected bind value is a boolean.
TextView. The expected bind value is a string and setViewText(TextView, String) is invoked.
ImageView. The expected bind value is a resource id or a string and setViewImage(ImageView, int) or setViewImage(ImageView, String) is invoked.
If no appropriate binding can be found, an IllegalStateException is thrown.


重要的一点是,可以给adapter设置ViewBinder,来进行自定义绑定,这个也就是刚才所说辅助View进行更新的东西。此外,我通过在ViewBinder中设置Log查看,发现每次滚动List的时候,都会触发该类下的setViewValue(View view, Object data, String textRepresentation)方法,也就是说滚动会触发重新绑定。

此外,Api Demos有几个非常好的关于List的Demo,有一些提高效率的小技巧,其中有个Efficient List Adapter和Slow Adapter(我更觉得这个应该叫Lazy Adapter)值得一看。

绑定一个大的List肯定耗时,特别是当要绑定的资源需要从网上重新抓取的时候,所以这个时候,就需要异步进行耗时的操作。

Android提供了一个轻量级的,好用的AsyncTask<Params, Progress, Result>类使用起来非常方便


AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.


简单说来,AsyncTask必须被子类话才可以使用,并且你必须重载doInBackground方法,你可以在这里进行耗时的操作。AsyncTask类有三个范型参数Params, Progress和Result。其中Params是传入AsyncTask的参数,这个会通过实例话AsyncTask调用execute()方法来传入。Progress表示中间进程的指数。而Result的异步任务返回的结果,由onPostExecute()回调函数的参数得到。具体用法参看这里

最后,对于List,我们一般都会见到“滚动加载”这个特性,现在的很多Web应用和手机应用都采用了这种效果。其实实现起来很简单,就是重载ListView的onScroll()方法,然后进行滚动判断,满足条件时异步加载。

注:本人初学,可能一些细节上会有纰漏,如果你有什么迷惑可以留言或者可以Google,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: