Android学习笔记11—自定义 ListView开发详解(一)

引言

该节主要介绍如何在开发中实现自定义ListView

开发流程

Step-1 新建一个Module,命名为CustomerListViewActivity
Step-2 在CustomerListViewActivity绑定的布局文件中添加ListView组件

<ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Step-3 添加自定义布局文件设置每个Item的基本样式

<ImageView
    android:id="@+id/imgPhoto"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@drawable/photo01"/>

<TextView
    android:id="@+id/txtNickName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="貂蝉"
    android:layout_toRightOf="@id/imgPhoto"
    android:layout_marginLeft="10dp"
    android:textColor="#4a90e6"
    android:textSize="16sp"/>

<TextView
    android:id="@+id/txtMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="这里是我发送的消息,呵呵呵呵这里是我发送的消息,呵呵呵呵这里是我发送的消息,呵呵呵呵这里是我发送的消息,呵呵呵呵"
    android:layout_toRightOf="@id/imgPhoto"
    android:layout_marginLeft="10dp"
    android:layout_below="@id/txtNickName"
    android:layout_marginTop="5dp"
    android:lineSpacingMultiplier="1.1"
    android:textSize="12sp"
    android:textColor="#3e3d3d"/>

Step-4 声明一个集合作为列表选项的数据源

private List<Map<String, ?>> listItems;

Step-5 声明一个ListView组件对象

private ListView myListView;

Step-6 自定义一个方法用于完成对列表选项数据源的封装实例化操作

private List<Map<String, ?>> getListItems(){
    // 步骤3-1:声明一个空的List集合对象
    List<Map<String, ?>> listItems = new ArrayList<>();

    // 步骤3-2:创建一个Map列表选项对象
    Map<String, Object> item01 = new HashMap<>();
    item01.put("id", 1);
    item01.put("imgPhoto", R.drawable.photo01);
    item01.put("txtNickName", "貂蝉");
    item01.put("txtMessage", "貂蝉发送的消息消息消息发送的消息消息消息发送的消息消息消息发送的消息消息消息");
    // 步骤3-3:将封装好的列表选项对象item添加到集合中
    listItems.add(item01);


    Map<String, Object> item02 = new HashMap<>();
    item02.put("id", 2);
    item02.put("imgPhoto", R.drawable.photo02);
    item02.put("txtNickName", "西施");
    item02.put("txtMessage", "西施发送的消息消息消息发送的消息消息消息发送的消息消息消息发送的消息消息消息");

    listItems.add(item02);

    Map<String, Object> item03 = new HashMap<>();
    item03.put("id", 3);
    item03.put("imgPhoto", R.drawable.photo03);
    item03.put("txtNickName", "吕布");
    item03.put("txtMessage", "吕布发送的消息消息消息发送的消息消息消息发送的消息消息消息发送的消息消息消息");

    listItems.add(item03);

    return listItems;
}

Step-7 实例化List列表选项集合

this.listItems = getListItems();

Step-8 创建SimpleAdapter适配器装载集合数据并设置选项样式布局

SimpleAdapter adapter = new SimpleAdapter(this, this.listItems,
            R.layout.listitem_customerlistvew_activity,
            new String[]{"imgPhoto", "txtNickName", "txtMessage"},
            new int[]{R.id.imgPhoto, R.id.txtNickName, R.id.txtMessage});

Step-9 将列表视图组件与适配器进行绑定

this.myListView = (ListView) findViewById(R.id.myListView);
this.myListView.setAdapter(adapter);

Step-10 自定义适配器点击监听事件

// 自定义一个适配器短点击事件监听器
private class AdapterViewOcl implements AdapterView.OnItemClickListener{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 获取当前选中的列表选项对象
        Map<String, ?> selectedItem = listItems.get(position);
        Toast.makeText(getApplicationContext(), "选中["+selectedItem.get("txtNickName")+"]", Toast.LENGTH_SHORT).show();
    }
}

// 自定义一个适配器长点击事件监听器
private class AdapterViewLongOCL implements AdapterView.OnItemLongClickListener{
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        // 获取当前选中的列表选项对象
        Map<String, ?> selectedItem = listItems.get(position);
        Toast.makeText(getApplicationContext(), "长点击了["+selectedItem.get("txtNickName")+"]", Toast.LENGTH_SHORT).show();
        return true;
    }
}

Step-11将列表组件与点击监听事件进行绑定

// 步骤8:列表组件绑定短点击监听器
this.myListView.setOnItemClickListener(new AdapterViewOcl());
// 步骤10:列表组件绑定长点击监听器
this.myListView.setOnItemLongClickListener(new AdapterViewLongOCL());
结果展示

CustomerListView.png
代码清单

activity_customer_list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.chinasofti.demo04listview.CustomerListViewActivity">

<ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</RelativeLayout>

listitem_customerlistvew_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<ImageView
    android:id="@+id/imgPhoto"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@drawable/photo01"/>

<TextView
    android:id="@+id/txtNickName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="貂蝉"
    android:layout_toRightOf="@id/imgPhoto"
    android:layout_marginLeft="10dp"
    android:textColor="#4a90e6"
    android:textSize="16sp"/>

<TextView
    android:id="@+id/txtMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="这里是我发送的消息,呵呵呵呵这里是我发送的消息,呵呵呵呵这里是我发送的消息,呵呵呵呵这里是我发送的消息,呵呵呵呵"
    android:layout_toRightOf="@id/imgPhoto"
    android:layout_marginLeft="10dp"
    android:layout_below="@id/txtNickName"
    android:layout_marginTop="5dp"
    android:lineSpacingMultiplier="1.1"
    android:textSize="12sp"
    android:textColor="#3e3d3d"/>

</RelativeLayout>

CustomerListViewActivity.java

package com.chinasofti.demo04listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
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 CustomerListViewActivity extends AppCompatActivity {

// 步骤1:声明一个集合作为列表选项的数据源
private List<Map<String, ?>> listItems;
// 步骤2:声明一个ListView组件对象
private ListView myListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customer_list_view);

    // 步骤4:实例化List列表选项集合
    this.listItems = getListItems();
    // 步骤5:创建SimpleAdapter适配器装载集合数据并设置选项样式布局
    SimpleAdapter adapter = new SimpleAdapter(this, this.listItems,
            R.layout.listitem_customerlistvew_activity,
            new String[]{"imgPhoto", "txtNickName", "txtMessage"},
            new int[]{R.id.imgPhoto, R.id.txtNickName, R.id.txtMessage});
    // 步骤6:列表视图组件绑定适配器
    this.myListView = (ListView) findViewById(R.id.myListView);
    this.myListView.setAdapter(adapter);
    // 步骤8:列表组件绑定短点击监听器
    this.myListView.setOnItemClickListener(new AdapterViewOcl());
    // 步骤10:列表组件绑定长点击监听器
    this.myListView.setOnItemLongClickListener(new AdapterViewLongOCL());
}

// 步骤3:自定义一个方法用于完成对列表选项数据源的封装实例化操作
private List<Map<String, ?>> getListItems(){
    // 步骤3-1:声明一个空的List集合对象
    List<Map<String, ?>> listItems = new ArrayList<>();

    // 步骤3-2:创建一个Map列表选项对象
    Map<String, Object> item01 = new HashMap<>();
    item01.put("id", 1);
    item01.put("imgPhoto", R.drawable.photo01);
    item01.put("txtNickName", "貂蝉");
    item01.put("txtMessage", "貂蝉发送的消息消息消息发送的消息消息消息发送的消息消息消息发送的消息消息消息");
    // 步骤3-3:将封装好的列表选项对象item添加到集合中
    listItems.add(item01);


    Map<String, Object> item02 = new HashMap<>();
    item02.put("id", 2);
    item02.put("imgPhoto", R.drawable.photo02);
    item02.put("txtNickName", "西施");
    item02.put("txtMessage", "西施发送的消息消息消息发送的消息消息消息发送的消息消息消息发送的消息消息消息");

    listItems.add(item02);

    Map<String, Object> item03 = new HashMap<>();
    item03.put("id", 3);
    item03.put("imgPhoto", R.drawable.photo03);
    item03.put("txtNickName", "吕布");
    item03.put("txtMessage", "吕布发送的消息消息消息发送的消息消息消息发送的消息消息消息发送的消息消息消息");

    listItems.add(item03);

    return listItems;
}

// 步骤7:自定义一个适配器监听器
private class AdapterViewOcl implements AdapterView.OnItemClickListener{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 步骤7-1:获取当前选中的列表选项对象
        Map<String, ?> selectedItem = listItems.get(position);
        Toast.makeText(getApplicationContext(), "选中["+selectedItem.get("txtNickName")+"]", Toast.LENGTH_SHORT).show();
    }
}

// 步骤9:自定义个适配器长点击监听器
private class AdapterViewLongOCL implements AdapterView.OnItemLongClickListener{
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        // 步骤9-1:获取当前选中的列表选项对象
        Map<String, ?> selectedItem = listItems.get(position);
        Toast.makeText(getApplicationContext(), "长点击了["+selectedItem.get("txtNickName")+"]", Toast.LENGTH_SHORT).show();
        return true;
    }
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,284评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,115评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,614评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,671评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,699评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,562评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,309评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,223评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,668评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,859评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,981评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,705评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,310评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,904评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,023评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,146评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,933评论 2 355

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,139评论 25 707
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,761评论 22 665
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,657评论 18 139
  • 引言 该节主要介绍如何在开发中实现在一个item中添加两行数据 总体流程 新建一个Module,并让XxxActi...
    advance_bravely阅读 339评论 0 0
  • 七月盛夏 云朵逃离了炙热的天空 你美丽的脸庞,沉睡在夏,仿佛一切都是清凉。 一个人的记忆,可以笼罩一座城市。 时间...
    何弃了阅读 201评论 0 0