ListView

  • getCount 和 getChildCount 方法

ListView.getCount()(实际上是 AdapterView.getCount()) 返回的是其 Adapter.getCount() 返回的值。也就是“所包含的 Item 总个数”。

ListView.getChildCount()(ViewGroup.getChildCount) 返回的是 “显示在界面上的子 View 个数”。即当前可见的 Item 个数。

二者有什么不同?

当 ListView 中的 Item 比较少无需滚动即可全部显示时:二者是等价的

当 Item 个数较多。需要滚动才能浏览全部的话:getChildCount() < getCount()

 

 

  • onItemClick方法

public abstract void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)

public abstract void onItemSelected (AdapterView<?> parent, View view, int position, long id)

Since: API Level 1 Callback method to be invoked when an item in this view has been selected. Impelmenters can call getItemAtPosition(position) if they need to access the data associated with the selected item.

arg0  parent  The AdapterView where the selection happened   解析:我们可以简单的看为下拉框(当我们为下拉框添加这个监听器时可以这么做),其他情况类似

arg1  view    The view within the AdapterView that was clicked 对应的就是ArrayAdapter构造函数中的第二个参数R.layout.list_item

arg2     position      The position of the view in the adapter           表示当前view在adapter中的位置

arg3  id    The row id of the item that is selected

 

AdapterView<?> arg0 官方解释说:the AdapterView where the click happened. 也就是当前点击的adapterview,这个参数是系统自动传入的,
我们也不用调用,一般常用第二个和第三个参数。

AdapterView<?> ,这个属于java泛型,就是告诉你传入的参数是哪种类型。 比如String<?>,List<T>,Map<K,V>,String<E>

?表示不确定的java类型。
T 表示java类型。
K V 分别代表java键值中的Key Value。
E 代表Element。

ListView, GridView, Spinner and Gallery 中都要用到adapter,所以这里用问好表示不确定传入的是哪种类型,不用我们关心,系统自动传入。
posted @ 2014-11-25 16:14  马小豆包  阅读(233)  评论(0编辑  收藏  举报