Android适配器和getView行为

时间:2015-09-06 10:29:02

标签: android android-listview android-adapter getview

我目前对android中的适配器感到困惑,尤其是函数getView的行为。

我遇到了许多答案,声明每当列表更改时都会调用getView()函数,就像滚动listview时一样。

然后我把它放在getView函数中:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.v("GET VIEW", "" + " ITEM accordingly " + position);
    ...
}

当我跑步时,这就是我在logcat中收到的内容:

09-06 17:18:23.812    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 0
09-06 17:18:23.848    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 0
09-06 17:18:23.851    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 0
09-06 17:18:23.868    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 0
09-06 17:18:23.874    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 1
09-06 17:18:23.880    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 2
09-06 17:18:23.887    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 3
09-06 17:18:23.894    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 4
09-06 17:18:23.903    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 5
09-06 17:18:23.910    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 6
09-06 17:18:23.930    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 0
09-06 17:18:23.932    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 1
09-06 17:18:23.937    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 2
09-06 17:18:23.942    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 3
09-06 17:18:23.948    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 4
09-06 17:18:23.958    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 5
09-06 17:18:23.968    3555-3555/com.example.nguyenquanganh.cookingassistance V/GET VIEW﹕ ITEM accordingly 6

更重要的是,当我滚动列表时,它没有打印任何其他内容。以上输出是在我启动活动时,我没有做任何其他事情。

所以有人可以向我解释,为什么在同一个项目上有多次调用getView?以及为什么在滚动列表时没有调用getView?

1 个答案:

答案 0 :(得分:0)

列表视图中的视图被重用,因为列表视图中只有7个项目,列表视图在之前获得足够信息时不会调用适配器的getView。尽量增加孩子的数量,然后再进行回收。