Android高级控件之Listview

1、Listview:列表控件,使用的时候高度和宽度最好是match-parent,此外,代码中使用还需要结合适配器。

2、ListView的使用及优化:(1) 使用ConvertView重用组件,即拖动时被遮住、看不见的控件,重用它,而非每次创建一个新的对象。当ListView第一次加载的时候,前面的数据ConvertView肯定是为null,所以我们在使用ConvertView对象的时候,先要判断是否为空,为空则从布局文件中解析一个新的view对象;不为空则直接拿来使用。(2)使用ConvertView.setTag()方法,每一次给布局文件中的控件设置值时,都需要调用findById(id)方法,重复查找会浪费时间,所以我们可以把控件的地址保存到一个对象中,再把这个对象保存在ConvertView中,下次对控件进行设值的时候,直接通过这个对象修改即可。(3)使用分页查询(PullToRefresh),使用AsyncTask(异步任务)加载数据,最少要重写以下这两个方法:doInBackground(后台执行)、onPostExecute(在doInBackground方法执行结束之后再运行,并且运行在UI线程当中 可以对UI空间进行设置)。优化的过程中需要使用LayoutInflater(布局解析器),用来把layout布局文件解析成一个View对象,不可以new,需要使用系统服务获得:inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),下面有一个例子

listview_item.xml里的代码如下:

<?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"    android:orientation="horizontal" >     <ImageView        android:id="@+id/iv_listviewitem_image"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="3"        android:padding="10dp"        android:scaleType="fitXY"        android:src="@drawable/book1" />     <LinearLayout        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="6"        android:orientation="vertical"        android:padding="10dp" >         <TextView            android:id="@+id/tv_listviewitme_title"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="狂人摄影日记"            android:textColor="@color/red"            android:textSize="20sp" />         <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal" >             <TextView                android:layout_width="0dp"                android:layout_height="wrap_content"                android:l
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值