ListView的使用

来源:互联网 发布:蝎子网络第二季5 编辑:程序博客网 时间:2024/06/06 01:04

熟练运用两种适配器 ArrayAdapter,SimpleAdapter

学会熟练运用两种监听器OnScrollListener,OnItemClickListener

学会熟练运用适配器数据的刷新 notifyDataChanged


ArrayAdapter使用

private ListView listView;private ArrayAdapter<String> arr_adatapter;<pre name="code" class="java">listView=(ListView) findViewById(R.id.listView);        //新建一个适配器        //ArrayAdapter(上下文,当前listView每个项加载的布局文件(这里使用自带的布局文件),数据源)        //适配器加载数据源        String [] arr_data={"慕课网","麦子学院",""};        arr_adatapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr_data);        listView.setAdapter(arr_adatapter);


SimpleAdapter使用

创建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="match_parent"    android:orientation="horizontal" >    <LinearLayout  android:layout_width="wrap_content"       android:layout_height="wrap_content">        <ImageView         android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="15dp"        android:src="@drawable/abc_ab_bottom_solid_dark_holo"        />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"       android:layout_height="wrap_content"        android:orientation="vertical" >        <TextView     android:id="@+id/title"    android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="14sp"        android:textColor="#000000"        android:text="demo"        />        <TextView     android:id="@+id/text"    android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="14sp"        android:textColor="#000000"        android:text="demo"        />    </LinearLayout>    <LinearLayout android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:orientation="horizontal">         <ImageView    android:id="@+id/imgx"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="15dp"        android:src="@drawable/abc_ab_bottom_solid_dark_holo"        />    </LinearLayout></LinearLayout>
private SimpleAdapter simp_adapter;private List<Map<String,Object>> datalist;
//context:上下文        //data:数据源(List<? extends Map<String,?>> data) 一个Map所组成的list集合) 每一个Map都会去对应listView列表中的一行 每一个map都是由键值对组成 必须去包含所有from中所有指定键        //resource:列表项的布局文件id        //from:Mpa中的键名        //to:绑定数据视图中的id 与from对应        datalist=new ArrayList<Map<String,Object>>();                simp_adapter=new SimpleAdapter(this, getDate(), R.layout.item, new String []{"img","imgx","title","text"},  new int[]{R.id.img,R.id.imgx,R.id.title,R.id.text});        listView.setAdapter(simp_adapter);
private List<Map<String,Object>> getDate(){    for (int i = 0; i < 20; i++) {Map<String,Object> map=new HashMap<String, Object>();map.put("img", "img"+i);map.put("imgx", "imgx"+i);map.put("title", "title"+i);map.put("text", "text"+i);datalist.add(map);}    return datalist;    }





0 0