zoukankan      html  css  js  c++  java
  • ListView的使用(一)

    在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。

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

    1.ListVeiw 用来展示列表的View。

    2.适配器 用来把数据映射到ListView上的中介。

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

    现在就来实现上述样式的listview

    首先,程序中需要定义两个XML文件,一个定义主Activity的UI界面(Screen Layout)

    ,另一个对Listview每一行的样式进行定义(Row Layout):

    activity_mian.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout android:id="@+id/listLinearLayout"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:orientation="vertical">
            <ListView android:id="@id/android:list" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:drawSelectorOnTop="true"
                android:scrollbars="vertical" />
        </LinearLayout>
    </LinearLayout>

    list.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" >
        <ImageView android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"/>
        <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"/>
            <TextView android:id="@+id/inf"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"/>          
        </LinearLayout>  
    </LinearLayout>

    为了更好的理解程序,先说说适配器。

    以simpleAdapter为例,它的扩展性最好,可以映射各种各样的布局。

    参数:
          1,context:上下文。
          2,data:基于Map的list。Data里边的每一项都和 ListView里边的每一行对应。Data里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。
          3,resource :就是一个布局layout,需要包含to参数所指定的条目。
          4,from:这是个名字数组,每个名字是为了在 ArrayList数组的每一个item索引Map<String,Object>的Object用的。即 map 中得key值
          5,to:里面是一个TextView数组。这些 TextView是以id的形式来表示的。例如:Android.R.id.text1,这个text1在layout当中是可以索引的。

    反应到程序中对应代码就是:

    SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list, 
                    new String[]{"title","info","img"},new int[]{R.id.title,R.id.inf,R.id.img});
            setListAdapter(adapter);

    其中geyData()函数返回List类型的打包数据,代码如下:

    private List<Map<String, Object>> getData() {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "G1");
            map.put("info", "google 1");
            map.put("img", R.drawable.i1);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G2");
            map.put("info", "google 2");
            map.put("img", R.drawable.i2);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G3");
            map.put("info", "google 3");
            map.put("img", R.drawable.i3);
            list.add(map);
            
            return list;
        }

    最终实现效果:

    下一篇将实现listview添加按钮,并添加控制效果。

    附完整代码:

    package com.hixin.mylistview;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.widget.SimpleAdapter;
    
    
    
    
    public class MainActivity extends ListActivity {
        
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list, 
                    new String[]{"title","info","img"},new int[]{R.id.title,R.id.inf,R.id.img});
            setListAdapter(adapter);
            
    
        }    
        private List<Map<String, Object>> getData() {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "G1");
            map.put("info", "google 1");
            map.put("img", R.drawable.i1);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G2");
            map.put("info", "google 2");
            map.put("img", R.drawable.i2);
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("title", "G3");
            map.put("info", "google 3");
            map.put("img", R.drawable.i3);
            list.add(map);
            
            return list;
        }
        
    
    }
  • 相关阅读:
    164 Maximum Gap 最大间距
    162 Find Peak Element 寻找峰值
    160 Intersection of Two Linked Lists 相交链表
    155 Min Stack 最小栈
    154 Find Minimum in Rotated Sorted Array II
    153 Find Minimum in Rotated Sorted Array 旋转数组的最小值
    152 Maximum Product Subarray 乘积最大子序列
    151 Reverse Words in a String 翻转字符串里的单词
    bzoj3994: [SDOI2015]约数个数和
    bzoj 4590: [Shoi2015]自动刷题机
  • 原文地址:https://www.cnblogs.com/hixin/p/4018715.html
Copyright © 2011-2022 走看看