深入浅出学习Android ListView基础
程序员文章站
2024-02-19 09:01:58
listview 简介
listview 是安卓里常用的控件, 本文介绍一下常用用法,以及优化等方法
1、改写activity_main.xml
<...
listview 简介
listview 是安卓里常用的控件, 本文介绍一下常用用法,以及优化等方法
1、改写activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout>
2、新建一个javabean
package com.example.tomchen.testlistview; public class icon { private int imageid; private string name; public icon(string name, int imageid) { this.name = name; this.imageid = imageid; } public string getname() { return name; } public int getimageid() { return imageid; } }
3、新建icon_item.xml 用于 listview 每个元素的视图
<?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="match_parent" android:orientation="horizontal"> <textview android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="match_parent" /> <imageview android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="match_parent" /> </linearlayout>
4、新建一个class iconadapter 继承自 arrayadapter
package com.example.tomchen.testlistview; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview; import java.util.list; public class iconadapter extends arrayadapter<icon> { private int resourceid; public iconadapter(context context, int resource, list<icon> icons) { super(context, resource, icons); this.resourceid = resource; } @override public view getview(int position, view convertview, viewgroup parent) { icon icon = getitem(position); view view; if (convertview == null) { view = layoutinflater.from(getcontext()).inflate(resourceid, null); } else { view = convertview; } imageview image = (imageview) view.findviewbyid(r.id.icon); image.setimageresource(icon.getimageid()); textview text = (textview) view.findviewbyid(r.id.text); text.settext(icon.getname()); return view; } }
note
这儿有了一个优化, convertview 将之前的布局进行缓存, 以便之后进行重用.
所以我们判断convertview是否为空. 空的话就使用layoutinflater去加载布局, 否则的话就直接复用convertview
测试listview
mainactivity里面加入下面代码
package com.example.tomchen.testlistview; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.listview; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { private listview listview; private list<icon> icons = new arraylist<icon>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview = (listview) findviewbyid(r.id.listview); icons.add(new icon("icon_01", r.drawable.ic_01)); icons.add(new icon("icon_02", r.drawable.ic_02)); icons.add(new icon("icon_03", r.drawable.ic_03)); icons.add(new icon("icon_04", r.drawable.ic_04)); icons.add(new icon("icon_05", r.drawable.ic_05)); icons.add(new icon("icon_06", r.drawable.ic_06)); icons.add(new icon("icon_07", r.drawable.ic_07)); icons.add(new icon("icon_08", r.drawable.ic_08)); icons.add(new icon("icon_09", r.drawable.ic_09)); iconadapter adapter = new iconadapter(this, r.layout.icon_item, icons); listview.setadapter(adapter); } }
效果图如下:
以上就是本文的全部内容,希望对大家的学习android程序设计有所帮助。
推荐阅读