Adapter初步学习

Adapter初步学习
public interface Adapter
可以看出Adapter是个接口,所以我们如果要使用Adapter,要么继承一个实现了该接口的类,要么实现这个接口。

Class Overview

An Adapter object acts as a bridge between an  AdapterView  and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a  View  for each item in the data set.

大概的翻译一下上面英文的意思。

一个适配器对象作为一个桥梁,链接AdapterView和这个View。适配器提供了访问数据项目的访问通道,适配器也负责给数据集合中的每个项目制作视图。

通过翻译,可以很清楚的看到Adapter的定位了,它就是一个桥梁,连接的是View对象和Data数据。负责把Data数据显示到View对象上。

接下来我们看看Adapter给我们提供了什么方法。

Public methods

getCount:How many items are in the data set represented by this Adapter.
声明在该Adapter上的Data集合内的项目数量。

getItem(int position):Get the data item associated with the specified position in the data set.
给个明确的position,会在Data数据集合中找到这个position的数据。

getItemId(int position):Get the row id associated with the specified position in the list.
给个明确的position,会得到列表中该position的列的id

getView(int position, View convertView, ViewGroup parent):Get a View that displays the data at the specified position in the data set.
给明确的position和View,会得到一个Data集合中该position的数据的展示。

getItemViewType(int position) :Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.
得到指定位置的数据项目所被展示的View类型。

getViewTypeCount() :Returns the number of types of Views that will be created by getView(int, View, ViewGroup).
返回该View类型的数目。

hasStableIds() :Indicates whether the item ids are stable across changes to the underlying data.
表明项目的ID是否稳定的指向相关数据,

registerDataSetObserver(DataSetObserver observer) :Register an observer that is called when changes happen to the data used by this adapter.
注册一个观察者,当该Adapter使用的数据发生改变时会被调用。

unregisterDataSetObserver(DataSetObserver observer): Unregister an observer that has previously been registered with this adapter via registerDataSetObserver(DataSetObserver).
取消之前注册的观察者。

有一个重点的方法。
public abstract View getView (int position, View convertView, ViewGroup parent):
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. 
获取视图用来显示数据集合内指定位置的数据。你要么手动创造一个View,要么从一个XML布局文件填充。当该视图被填充,父布局会请求默认布局参数,除非你使用inflate(int, android.view.ViewGroup, boolean)去明确一个根view并且阻止和根视图的连接。


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.
返回给定位置的数据的视图,最终和父视图联系起来。

Returns: A View corresponding to the data at the specified position.
返回一个匹配该位置数据的视图。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值