今天工作一个半月了,在今天的coding中遇到了一个问题,那就是我的ListView需要显示用户的评论信息,测试数据只有三条,但是ListView却重用了5次,上图
ListView中明明只有三项,getView()方法却调用了5次,于是显示出来15个item的位置,百度了好久,都没有我要的答案,下面的我的item布局图片及代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/type_detail_im_uericon"
android:layout_marginLeft="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon_user_defualt"/>
<TextView
android:id="@+id/type_detail_tv_username"
android:layout_marginLeft="8dp"
android:layout_gravity="center"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="陆**"
android:textSize="18dp"/>
<TextView
android:id="@+id/type_detail_tv_daytime"
android:layout_gravity="center|right"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2016-09-02 17:30"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_marginLeft="40dp"
android:id="@+id/type_detail_tv_usersaying"
android:layout_width="match_parent"
android:text="\t\t今天很残酷,明天更残酷,后天很美好,绝大部分人死在明天晚上看不见后天的太阳。"
android:layout_height="wrap_content"
android:textSize="15dp"
android:padding="4dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginRight="6dp"
android:layout_marginLeft="6dp"
android:background="#AAA"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
注意到了吗?我的ImageView宽和高用的是wrap_content
但是这么运行还是重复调用了5次getView()方法(百度有位仁兄说他重复调用了340多次,真够严重的啊),解决了半天解决不出来,于是只能出此下策改布局了,
<ImageView
android:id="@+id/type_detail_im_uericon"
android:layout_marginLeft="12dp"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/icon_user_defualt"/>
我把ImageView宽和高都改成了60dp后居然好使了,虽然Log中还是重复调用了5次,但是显示的就是3条,不影响用户体验。
好了,我该敲下一步了,这是我的第一篇文章,谢谢观看。