常用控件

8、ListView
  列表视图,比如游戏的排行榜。列表数据可以根据屏幕大小自适应

列表的显示需要三个元素:

a、ListVeiw:用来展示列表的View。

b、适配器:用来把数据映射到ListView上的中介。

c、数据:具体的将被映射的字符串,图片,或者基本组件。


ListView用到两种适配器:
1、ArrayAdapter--简单适配器,只显示文字

android学习日记03--常用控件ListView

 

2、SimpleAdapter--自定义适配器,可以显示自定义内容

android学习日记03--常用控件ListView

 

3、SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来,暂时不讲。

使用简单适配器可直接
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
this:当前context
android.R.layout.simple_list_item_1是系统的布局文件
list:ListView的各项数据

SimpleAdapter各项参数
new SimpleAdapter(context, data, resource, from, to)
context:当前context内容
data:ListView的各项数据
resource:ListView的每项布局
from:ListView的组件索引
to:ListView的组件ID
如:
new SimpleAdapter(MainActivity.this, list, R.layout.activity_main, new String[] {"item1","item2","item3","item4","item5"}
, new int[] {R.id.iv,R.id.bigtv,R.id.smalltv,R.id.btn,R.id.cb});

但是SimpleAdapter 不能触发组件的事件,还需
自己写适配器继承BaseAdapter

BaseAdapter主要有四个方法
getCount --列表每一项的长度
getItem
getItemId
getView --绘制每一项的具体组件

例子演示自定义MyAdapter

 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <LinearLayout   
 3     android:orientation="horizontal" 
 4     android:layout_width="fill_parent"   
 5     android:layout_height="fill_parent"   
 6     xmlns:android="http://schemas.android.com/apk/res/android"
 7     >  
 8     <ImageView 
 9         android:id="@+id/iv"   
10         android:layout_width="wrap_content"   
11         android:layout_height="wrap_content"   
12         />
13     <LinearLayout   
14         android:orientation="vertical" 
15         android:layout_width="wrap_content"   
16         android:layout_height="wrap_content"   
17     >  
18     <TextView 
19         android:layout_width="wrap_content"   
20         android:layout_height="wrap_content"  
21         android:textSize="20sp"
22         android:id="@+id/bigtv"
23         />
24     
25     <TextView 
26         android:layout_width="wrap_content"   
27         android:layout_height="wrap_content"  
28         android:textSize="10sp"
29         android:id="@+id/smalltv"
30         />
31     
32     </LinearLayout>
33     
34     <Button 
35         android:layout_width="wrap_content"   
36         android:layout_height="wrap_content" 
37         android:text="button"
38         android:id="@+id/btn"
39         android:focusable="false"
40         />
41     
42     <CheckBox 
43         android:layout_width="wrap_content"   
44         android:layout_height="wrap_content" 
45         android:id="@+id/cb"
46         android:focusable="false"
47         />
48         
49     
50 </LinearLayout>
View Code

相关文章:

  • 2021-12-16
  • 2021-12-02
  • 2021-11-22
  • 2021-09-23
  • 2021-12-05
  • 2021-07-21
  • 2021-08-22
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-11-11
  • 2021-07-22
相关资源
相似解决方案