目录
- 操作环境
- 导读
- 参数说明
- 源码浅析
操作环境
- 操作系统: win7-64bit 旗舰版
- android 版本: android-23
导读
平时在使用 ListView 或 GridView 时,常需要自己复写 Adapter 的 View getView(int position, View convertView, ViewGroup parent) 方法,但对其中的 convertView 却不太了解,没法做到物尽其用;殊不知其用途可不小,可以在一定程度上避免 OOM
参数说明
在 Adapter.java 中,对参数 convertView 的说明如下:
- 说明: convertView 是一个可重用的 View,当然,也有可能是 Null;
- 良好的习惯是,在使用之前都检测一下;
/**
* 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
* {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}
* to specify a root view and to prevent attachment to the root.
*
* @param position The position of the item within the adapter's data set of the item whose view
* we want.
* @param convertView The old view to reuse, if possible. Note: You should check that this view
* is non-null and of an appropriat