一、ListView

效果:

1.activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:scrollbars="vertical" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="没有数据"/> </LinearLayout>

该文件用于控制整个ListView框架的风格.

2.usr.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/logo_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:scaleType="centerCrop"
android:paddingTop="5dp"
android:paddingLeft="10dp"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/ip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="40dp"/> </LinearLayout>

该文件用于控制,每一个ListView中的Item的样式。如通过后期需要添加一些装饰或者变化的话,都可以在这里面设置。比如增加,按钮、图片等等的设置。

3.MainActivity.java

package com.example.listview;

import java.util.ArrayList;
import java.util.HashMap; 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); //-----------------------封装数据-------------------------------
//ArrayList用于存储ListView的所有数据,每一个数据项用HashMap存储
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); //HashMap中存储每一个Item的数据
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>(); map1.put("name", "zhangsan");
map1.put("ip", "123");
map2.put("name", "zhangsan");
map2.put("ip", "123");
map3.put("name", "zhangsan");
map3.put("ip", "123"); list.add(map1);
list.add(map2);
list.add(map3);
//------------------------封装数据完成---------------------
//-------------------数据装入----------------------------
//参数的含义:
//1.上下文对象
//2.每一个item的数据来源
//3.每一个item所使用的布局文件
//4.每一个item数据源又哪几项
//5.每一项对应的控件的风格
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
R.layout.usr, new String[] { "name", "ip" }, new int[] {
R.id.name, R.id.ip }); //绑定item的数据和设置
// ListView listView = (ListView) findViewById(R.id.listView);
// listView.setAdapter(simpleAdapter);
setListAdapter(simpleAdapter);
} }

该java代码只是,简单的实现了listView设置。其核心步骤就是这样。

另外还有几点需要注意:

1.当你的java代码继承的是ListActivity的时候,布局文件中Listvew的id需要使用系统定义好的id。(见activity_main.xml)

方式有两种:

android:id="@id/android:list"

android:id"@android:id/list"

注意一下,这时候你的java代码中没有使用findviewById绑定ListView控件。这是应为当你继承了ListActivity并将布局文件中的ListView的id设置为系统默认id后,系统会自动调用ListView控件。对ListView的所有操作,将由继承自ListActivity的方法实现。这时如果绑定适配就可以直使用setListAdapter方法。

2.相对于第一种方式来说,第二种方式我们更加的熟悉。

当你不希望使用系统默认id而是使用自己定义ListView的id的时候就需要采用这种方式。

首先再第一种方式的基础上,要做如下的改动:

第一,将ListView的id改为自己定义的id

第二,java代码中不再继承ListActicity,改为继承Activity。

第三,由于第二部的原因,你已经不能直接使用setListAdapter方法了。怎么办呢?你需要通过findViewById的方式找到Listview控件(很熟悉的设置吧)。比如,ListView mListView = findviewById(R.id.myListView)。然后使用mListView.setAdapter()方法来设置适配器。这样可以达到和方法一同样的效果。

但是第二种方式也有不足之处:不能使系统自动调用控件。什么意思呢?

如果,使用第一种方式。我在ListView空间下面加一个TextView,把TextView的控件id设为系统默认:android:id="@android:id/empty",那么当LIstView中没有数据的时候系统就会自动调用这个TextView控件显示这个TextView中设置好的文本。但是使用第二种方式就没有这么智能了,即使做了和刚才同样的设置当ListView中没有数据的时候系统也不会自动调用这个TextView

这里有几篇帖子值得参考:点击打开链接 点击打开链接

二、ExpandableListView

效果:

代码:

1.main.xml

<LinearLayout 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"
android:orientation="vertical"
tools:context=".Main" > <ExpandableListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/darker_gray"
android:drawSelectorOnTop="true"
/> <TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="没有数据!" /> </LinearLayout>

2.items.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="vertical" > <TextView
android:id="@+id/item_TextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="10dp"
android:paddingLeft="30dp"
android:paddingTop="10dp"
android:text="没有数据"
android:textSize="24sp" /> </LinearLayout>

3.sub_items.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/logo_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:scaleType="centerCrop"
android:src="@drawable/logo" /> <TextView
android:id="@+id/sub_item_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="40dp"
android:paddingTop="10dp"
android:text="没有数据" /> </LinearLayout>

4.main.java

package com.example.expandablelistactivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map; import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter; public class Main extends ExpandableListActivity{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); /**
* 有关数据的说明: 当一级条目有多个的的时候,需要使用容器把所有的以及条目整合到一起。
*/ // 创建数据容器存放一级条目----------------------------------
ArrayList<Map<String, String>> items = new ArrayList<Map<String, String>>();
// 创建两个一级条目
Map<String, String> item1 = new HashMap<String, String>();
Map<String, String> item2 = new HashMap<String, String>();
// 为一级条目添加数据
item1.put("items", "同學");
item2.put("items", "朋友");
// 绑定一级条目
items.add(item1);
items.add(item2); // 创建容器存放二级条目----------------------------------------
ArrayList<ArrayList<Map<String, String>>> subitems = new ArrayList<ArrayList<Map<String, String>>>(); // -------------第一个二级条目的内容
ArrayList<Map<String, String>> subitem1 = new ArrayList<Map<String, String>>();
Map<String, String> subitem1Date1 = new HashMap<String, String>();
Map<String, String> subitem1Date2 = new HashMap<String, String>();
subitem1Date1.put("subitems", "张飞");
subitem1Date2.put("subitems", "关羽");
subitem1.add(subitem1Date1);
subitem1.add(subitem1Date2); // -------------第二个二级条目的内容
ArrayList<Map<String, String>> subitem2 = new ArrayList<Map<String, String>>();
Map<String, String> subitem2Date1 = new HashMap<String, String>();
Map<String, String> subitem2Date2 = new HashMap<String, String>();
subitem2Date1.put("subitems", "张龙");
subitem2Date2.put("subitems", "赵虎");
subitem2.add(subitem2Date1);
subitem2.add(subitem2Date2); // 整合二级条目
subitems.add(subitem1);
subitems.add(subitem2); // 为适配器绑定数据--------------------------
//参数:
//1.上下文对象
//2.第一级条目的数据源
//3.第一级条目的布局文件
//4.第一级条目的key,相当与HashMap的键值映射的“键”
//5.第一级条目显示控件的id,和第三个参数一一对应
//6.二级条目的数据源
//7.二级条目的布局文件
//8.二级条目的key
//9.二级条目显示控件的id,和第八个参数一一对应
SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter(
this, items, R.layout.items, new String[] { "items" },
new int[] { R.id.item_TextView }, subitems, R.layout.sub_items,
new String[] { "subitems" },
new int[] { R.id.sub_item_TextView }); //适配器绑定到ExpandableListActivity------------------------------------------------------
setListAdapter(simpleExpandableListAdapter);
}
}

A12_ListView & ExpandablelistView的更多相关文章

  1. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

  2. Android中使用ExpandableListView实现好友分组

    一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源 ...

  3. 安卓开发树形控件之ExpandableListView(一)

    这个例子非常简单,简单到一个初学者都能随便开发出来,今天的目的仅仅只是为了将效果实现出来,如果想深入这里有几篇非常不错的博客: Android 之ExpandableListView几个特殊的属性 h ...

  4. android 伸缩控件ExpandableListView 展开失败的可能原因。

    (原创)转载请声明出处http://www.cnblogs.com/linguanh/ 问题原型: ExpandableListView 展开失效. --------------------直接看结论 ...

  5. ExpandableListView实现展开更多和收起更多

    [需求]: 如上面图示 当点开某个一级菜单的时候,其他菜单收起: 子级菜单默认最多5个: 多于5个的显示"展开更多" 点击"展开更多",展开该级所有子级菜单,同 ...

  6. Android UI控件----ExpandableListView的基本用法

    ExpandableListView介绍 ExpandableListView的引入 ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListVie ...

  7. 【原创】Android ExpandableListView使用

    ExpandableView的使用可以绑定到SimpleExpandableListAdapter,主要是看这个Adapter怎么用. 这个类默认的构造函数有9个参数, 很好地解释了什么叫做又臭又长. ...

  8. android原生ExpandableListView

    android原生可扩展ExpandableListView就是可以伸缩的listView,一条标题下面有多条内容. 这个list的adapter对的数据要求与普通ListView的数据要求也有一些差 ...

  9. 可滑动的ExpandableListView

    可以向左滑动的扩展列表 向左滑动源码是参照GitHub上的里的 ListView的思路写出来的,按照他的思路,由于本人水平有限,只写了关键代码,能够完美运行,adapter改变之后能自动收回. 滑出状 ...

随机推荐

  1. web客户端安全之跨站点请求伪造攻击

    CSRF攻击,Cross-site request forgery,跨站点请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种 ...

  2. DP Training(Updating)

    感觉前面做了那么多$dp$全是自己想的还是太少啊…… 好像在LZT的博客上看到了不错的资源?赶紧开坑,以一句话题解为主 Codeforces 419B 第一题就开始盗图 由于只有一个交点,手玩一下发现 ...

  3. MySQL主从复制(Replication)(MySQL数据同步)配置

    MySQL是开源的关系型数据库系统.复制(Replication)是从一台MySQL数据库服务器(主服务器master)复制数据到另一个服务器(从服务器slave)的一个进程. 配置主服务器(mast ...

  4. bzoj 2844 子集异或和名次

    感谢: http://blog.sina.cn/dpool/blog/s/blog_76f6777d0101d0mr.html 的讲解(特别是2^(n-m)的说明). /*************** ...

  5. ASP.NET 构建高性能网站 第2篇

    前言:在对ASP.NET网站进行优化的时候,往往不是只是懂得ASP.NET就足够了的. 在优化的过程中,一般先是找出问题可能存在的地方,然后证明找出的问题就是要解决的问题,确认之后,在进行一些措施.系 ...

  6. Python的静态方法和类成员方法

    http://www.cnblogs.com/2gua/archive/2012/09/03/2668125.html Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还 ...

  7. HDU 4717 The Moving Points (三分)

    The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. Quartz实现动态定时任务

    一. 说明 由于最近工作要实现定时任务的执行,而且要求定时周期是不固定的,所以就用到了quartz来实现这个功能: spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持q ...

  9. nyoj 164&amp;&amp;poj2084 Game of Connections 【卡特兰】

    题意:将1~2n个数依照顺时针排列好.用一条线将两个数字连接起来要求:线之间不能有交点.同一个点仅仅同意被连一次. 最后问给出一个n,有多少种方式满足条件. 分析: ans[n]表示n的中的种类数. ...

  10. JavaScript对于函数的调用及原理

    <js> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>< ...