课程目标
1.理解ListView的基础使用
2.学会熟练运用两种适配器(ArrayAdapter、SimpleAdapter)
3.学会熟练运用两种监听器(OnScrollListener、OnItemClickListener)
4.学会数量运用适配器数据的刷新(notifyDataChanged)

ListView
作用:android系统中显示列表的控件
ListView控件(每一个ListView都可以包含很多个列表项)

数据适配器
作用:把复杂的数据(数组、链表、数据库、集合等)填充在制定视图界面上
ArrayAdapter(数组适配器):用于绑定格式单一的数据
数据源:可以是集合或数组
SimpleAdapter(简单适配器):用于绑定格式复杂的数据
数据源:只能是特定泛型的集合
数据适配器是链接数据源和视图见面的桥梁
实现过程:新建适配器->添加数据源到适配器->视图加载适配器

新建一个数组适配器:ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源)
arr_adapter = new ArrayAdapter<Stirng>(this, android.R.layout.simple_list_item_1, arr_data);
视图加载适配器:listView.setAdapter()方法。

ListView with ArrayAdapter:

        String[] arrData = new String[] { "apple", "banana", "orange", "pear" };
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrData);
        listView.setAdapter(arrayAdapter);

SimpleAdapetr(context, data, resource, from, to)
context:上下文
data:数据源(List<? extends Map<String,?>> data) 一个Map所组成的List集合
 每一个Map都回去对应ListView列表中的一行
 每一个Map(键-值对)中的键必须包含所有在from中所指定的键
resource:列表项的布局文件ID
from:Map中的键名
to:绑定数据视图中的Id,与from成对应关系

对于resource,我们新建一个样式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="30dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_lena" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:text="TextView" 
          android:gravity="left|bottom"
        />

</LinearLayout>
item.xml

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-02
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
猜你喜欢
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2021-12-03
  • 2022-12-23
  • 2021-12-24
相关资源
相似解决方案