A12_ListView & ExpandablelistView
一、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的更多相关文章
- Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)
之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...
- Android中使用ExpandableListView实现好友分组
一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源 ...
- 安卓开发树形控件之ExpandableListView(一)
这个例子非常简单,简单到一个初学者都能随便开发出来,今天的目的仅仅只是为了将效果实现出来,如果想深入这里有几篇非常不错的博客: Android 之ExpandableListView几个特殊的属性 h ...
- android 伸缩控件ExpandableListView 展开失败的可能原因。
(原创)转载请声明出处http://www.cnblogs.com/linguanh/ 问题原型: ExpandableListView 展开失效. --------------------直接看结论 ...
- ExpandableListView实现展开更多和收起更多
[需求]: 如上面图示 当点开某个一级菜单的时候,其他菜单收起: 子级菜单默认最多5个: 多于5个的显示"展开更多" 点击"展开更多",展开该级所有子级菜单,同 ...
- Android UI控件----ExpandableListView的基本用法
ExpandableListView介绍 ExpandableListView的引入 ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListVie ...
- 【原创】Android ExpandableListView使用
ExpandableView的使用可以绑定到SimpleExpandableListAdapter,主要是看这个Adapter怎么用. 这个类默认的构造函数有9个参数, 很好地解释了什么叫做又臭又长. ...
- android原生ExpandableListView
android原生可扩展ExpandableListView就是可以伸缩的listView,一条标题下面有多条内容. 这个list的adapter对的数据要求与普通ListView的数据要求也有一些差 ...
- 可滑动的ExpandableListView
可以向左滑动的扩展列表 向左滑动源码是参照GitHub上的里的 ListView的思路写出来的,按照他的思路,由于本人水平有限,只写了关键代码,能够完美运行,adapter改变之后能自动收回. 滑出状 ...
随机推荐
- poj3537 Crosses and Crosses 博弈论
大意: 给定一个\(1 * n\)的棋盘,你和对手轮流在上面画"X" 当出现三个连续的X时,最后一步操作的人胜利 不难发现,在棋盘中画了一个X之后 问题等价于两个一样的子游戏 然后 ...
- 王彪-20162321-Java程序设计与数据结构2nd-第十周学习总结
学习目标 讨论有向图和无向图 定义带权图并讨论它们的应用 定义图的广度优先遍历和深度优先遍历 定义最小生成树 讨论图的实现策略 书中图的基本定义 图是由结点及结点间的连接组成的,结点称为顶点,结点间的 ...
- Java混剪音频
分享一个之前看过的程序,可以用来剪辑特定长度的音频,将它们混剪在一起,思路如下: 1.使用 FileInputStream 输入两个音频 2.使用 FileInputStream的skip(long ...
- Windows Server 2008 R2的web服务器nginx和Apache的比较
因为很喜欢nginx,所以也想尝试在Windows下使用nginx,前面安装配置都挺顺利,把域名解析尽量后,通过域名代理访问jboss,却异常的慢,起码有3秒的时间才显示页面,而这个页面是jboss的 ...
- windows下配置ssh(FreeSSHD + putty)
windows下配置ssh(FreeSSHD + putty): 1.关于配置过程找到一篇很好的博客,推荐大家先好好看一下,这篇博文解决了大方向问题. 地址:http://blog.csdn.net/ ...
- Codeforces Round #222 (Div. 1) B. Preparing for the Contest 二分+线段树
B. Preparing for the Contest 题目连接: http://codeforces.com/contest/377/problem/B Description Soon ther ...
- poj 2623 Sequence Median 堆的灵活运用
I - Sequence Median Time Limit:1000MS Memory Limit:1024KB 64bit IO Format:%I64d & %I64u ...
- INFORMATION_SCHEMA.COLUMNS-表的字段信息
当前数据库中当前用户可以访问的每一个列在该视图中占一行.INFORMATION_SCHEMA.COLUMNS 视图以 sysobjects.spt_data type_info.systypes.sy ...
- Mac下操作mysql
修改密码 进入到mysql工作目录 cd /usr/local/mysql/bin/ 登录mysql mysql -u root -p 输入初始密码 mysql> 设置新密码 mysql> ...
- MySQL Proxy 实现MySQLDB 读写分离
一.简述 MySQL Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测.分析或改变它们的通信.它使用灵活,没有限制,常见的用途包括:负载平衡,故障.查询分析 ...