最近在写一个android listview自定义的adapter的时候发现了一个问题:
自定义布局getView的调用次数的问题。
写了一个与原来写好的一个一模一样的自定义的adapter运行后发现,自己写的adapter运行结果比较卡,点击里面的组件好久没有反应,
后来各种对比,终究没发现什么,最后的最后快要放弃的时候,突然换了ListView 在xml中布局居然好使了,两个布局如下:
<ListView
android:id="@+id/remind_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="#E4E4E4"
android:dividerHeight="1dip"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"
/>
<ListView
android:id="@+id/remind_list"
android:layout_marginTop="10dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeLayout1"
各种对比后发现了:
主要问题出在:android:layout_height="wrap_content"和android:layout_height="match_parent"
当设定为match_parent的时候发现加载adapter 的时候getview只会运行你所填充的数据的length的总数(比如说你添加了四个数据只会运行4次)
当设定为wrap_content的时候发现加载的adapter运行的次数是填充数据的总和*总和(添加了四个数据会运行4*4次);
到此只解决了问题,但是不知道这个是为什么?
期待大侠回答,我也在研究中!