ListView学习

ListView是Android中比较常见的空间,它以列表的形式展现具体内容

列表显示需要三个元素:

  1. ListView 展示列表的View,可以包含多个列表项
  2. 适配器 将数据填充到ListView的工具
  3. 数据 要填充的数据

适配器:

适配器的作用:把复杂的数据(数组、链表、数据库、集合等)填充在指定视图界面上
  1. ArrayAdapter(数组适配器):用于绑定格式单一的数据,可以是集合或者是数组
  2. SimpleAdapter(简单适配器):用于绑定格式复杂的数据,只能是泛型

ArrayAdapter:

ListView listView = (ListView) findViewById(R.id.listView);
        //新建数据
        String[] data = {"苹果", "雪梨", "桃子", "李子", "葡萄", "芒果", "菠萝", "荔枝", "蓝莓", "香蕉", "柿子", "脐橙"};
        //1、新建数据适配器
        //2、添加数据到适配器
        //ArrayAdapter(上下文, 当前ListView加载的每一个列表项所对应的布局文件, 数据源)
        ArrayAdapter<String> arr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
        //3、视图加载适配器
        listView.setAdapter(arr_adapter);

这里写图片描述

SimpleAdapter:

ListView listView = (ListView) findViewById(R.id.listView);
        //data容器
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        //填充数据
        for (int i = 0; i < 20; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("pic", R.drawable.ic_launcher);
            map.put("text", "Demo" + i);
            dataList.add(map);
        }
        //SimpleAdapter(上下文, 数据源(Map组成的List集合), 列表项的布局文件ID, Map中的键名, 绑定数据视图的ID)
        SimpleAdapter sim_adapter = new SimpleAdapter(this, dataList, R.layout.item, new String[]{"pic", "text"}, new int[]{R.id.pic, R.id.text});
        //加载到视图中
        listView.setAdapter(sim_adapter);
<?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" >

    <ImageView 
        android:id="@+id/pic"
        android:layout_marginLeft="15dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView 
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp" 
        android:textColor="#000000"
        android:text="demo"/>
</LinearLayout>

这里写图片描述

监听器

 监听器主要是为了去响应某个动作,通过这种监控这种行为,来完成我们需要的程序功能,是用户(系统)程序交互的桥梁
  1. OnItemClickListener:处理视图中单个条目的点击事件
  2. OnScrollListener:检测滚动的变化,可以用于视图在滚动中加载数据

OnitemClickListener:

listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                //这里返回的是Map数据类型,我强转的String类型
                String text = listView.getItemAtPosition(position).toString();
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });

这里写图片描述

OnScrollListener:

手指滑动有三种状态:

  1. SCROLL_STATE_FLING:用户在手指离开屏幕前,由于用力滑动,视图依靠惯性继续滑动
  2. SCROLL_STATE_TOUCH_SCROLL:手指没有离开屏幕
  3. SCROLL_STATE_IDLE:视图停止滑动
listView.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                switch (scrollState) {
                case SCROLL_STATE_FLING:
                    Log.e("MainActivity", "用户在手指离开屏幕前,由于用力滑动,视图依靠惯性继续滑动");
                    //滑动增加数据
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("pic", R.drawable.ic_launcher);
                    map.put("text", "View");
                    dataList.add(map);
                    //通知UI线程更新数据
                    sim_adapter.notifyDataSetChanged();
                    break;

                case SCROLL_STATE_TOUCH_SCROLL:
                    Log.e("MainActivity", "手指没有离开屏幕");

                case SCROLL_STATE_IDLE:
                    Log.e("MainActivity", "视图停止滑动");
                    break;

                }
            }

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值