Android学习-ListView

来源:互联网 发布:知乎 大泽佑香 编辑:程序博客网 时间:2024/06/05 16:19

ListView
作用:安卓系统中显示列表的控件,每一个ListView可以包括很多列表项。
数据适配器
作用:把复杂的数据(数组,链表,数据库,集合等)填充到指定视图界面上。
1>ArrayAdapter(数组适配器):用于绑定格式单一的数据
数据源:可以使集合或者数组
2>SimpleAdapter(简单适配器):用于绑定格式复杂的数据
数据源:只能是特定泛型的集合
监听器
作用:相应某个动作,我们可以通过监控这种动作来完成我们需要完成的功能。
1>OnItemClickListener
可以处理视图中单个条目的点击事件
2>OnScrolListener
检测滚动的变化,可以用于视图在滚动中加载数据

package com.example.angel.listviewpro;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AbsListView;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainAcitivity extends Activity implements AdapterView.OnItemClickListener,AbsListView.OnScrollListener{    private ListView listView;    private ArrayAdapter<String> arr_adapter;    private SimpleAdapter sim_adapter;    private List<Map<String,Object>> sim_data;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_layout);        listView = (ListView)findViewById(R.id.listview);        //1.新建一个数据适配器        // AyyatAdapter(上下文,当前的ListView加载的每一个列表项所对应的布局文件,数据源);        /*        *  SimpleAdapter()        *  context:上下文        *  data: 数据源(List<? extends Map<String,?>>data) 一个Map组成的List集合        *  resourse:列表项的布局文件        *  from:Map中的键名 可以自己定义        *  to:绑定数据视图中的id,与from成对应关系        * */        //2.适配器加载数据源        String[] data = {"安琪1","安琪2","安琪3","安琪4","安琪5"};        sim_data = new ArrayList<>();        arr_adapter = new ArrayAdapter<String>                (this,android.R.layout.simple_list_item_1,data);        sim_adapter = new SimpleAdapter                (this,getData(),R.layout.item,new String[]{"image","text"},new int[]{R.id.image,R.id.text});        //3.视图加载适配器        //listView.setAdapter(arr_adapter);        listView.setAdapter(sim_adapter);        listView.setOnItemClickListener(this);        listView.setOnScrollListener(this);    }    private List<Map<String,Object>> getData(){        for(int i=0;i<20;i++){            Map<String ,Object> map = new HashMap<String,Object>();            map.put("image",R.mipmap.ic_launcher);            map.put("text","安琪安琪"+i);            sim_data.add(map);        }        return sim_data;    }    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {        String text = listView.getItemAtPosition(position)+" ";        Toast.makeText(this,"position="+position+"text="+text,Toast.LENGTH_SHORT).show();    }    //i是滚动状态    public void onScrollStateChanged(AbsListView absListView, int i) {        int m=0;        switch(i){            case SCROLL_STATE_FLING:                Log.i("main", "用户手指离开屏幕前,由于用力划了一下,视图由于惯性依然划了一下 ");                Map<String,Object> map = new HashMap<>();                map.put("image",R.mipmap.ic_launcher_round);                map.put("text","栗琛"+m++);                sim_data.add(map);                sim_adapter.notifyDataSetChanged();                break;            case SCROLL_STATE_IDLE:                Log.i("main", "视图已经停止滑动");                break;            case SCROLL_STATE_TOUCH_SCROLL:                Log.i("main", "视图正在滑动,手指没有离开屏幕 ");                break;        }    }    public void onScroll(AbsListView absListView, int i, int i1, int i2) {    }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"/>    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="30sp"        android:textColor="@android:color/holo_green_light"        android:text="demo"/></LinearLayout>

这里写图片描述
这里写图片描述