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改变之后能自动收回. 滑出状 ...
随机推荐
- 切换 NPM 镜像源
转载:快速切换NPM源 我们介绍过cnpmjs.org和淘宝 npm 两个 NPM 镜像.除此之外,还有一些国外的 NPM 镜像.不同地区访问不同的镜像速度可能有差异,因此有时候需要切换 NPM 镜像 ...
- golang make()的第三个参数
golang分配内存有一个make函数,该函数第一个参数是类型,第二个参数是分配的空间,第三个参数是预留分配空间,前两个参数都很好理解, 对于第三个参数,例如a:=make([]int, 5, 10) ...
- [ 转载 ] Mysql 数据库常用命令
完整的创建数据库例子: >create database db_test default character set utf8 collate utf8_general_ci; >use ...
- Linux下gcc与g++用法以及编写makefile
1. gcc与g++编译流程: 1) 编译流程: 2) 预处理:生成.i的预处理文件. Ø 只激活预处理,这个不生成文件,需要把它重定向一个输出文件. ...
- bzoj 3262
题意:给你一些三维上的点,对于每个点,统计三个坐标都小于等于该点的点数. 如果点的范围在300以内,可以用三维树状数组搞,但这题坐标范围太大. 考虑将所有点按照x坐标排序,从左到右,相当于在一个二维平 ...
- Qt 编译boost
Qt为4.6.2.Boost为1.63.0. 1.安装qt-sdk-win-opensource-2010.02.1.exe. 2.下载boost_1_63_0并解压,如:解压到E盘根目录下. 3.在 ...
- OpenVPN分配静态IP以及同一网段内IP个数(64个)
说明:简单的来说,同一网段内可用的IP数量只有64个:(不一定正确)最直接的解释就是每个客户端占用两个IP,因为根据IP掩码位/30得知可用的IP就是两个.对于为什么只有64个,下面是官方的解释. 解 ...
- LM358资料及引脚图
LM358里面包括有两个高增益.独立的.内部频率补偿的双运放,适用于电压范围很宽的单电源,而且也适用于双电源工作方式,它的应用范围包括传感放大器.直流增益模块和其他所有可用单电源供电的使用运放的地方使 ...
- IDA Supported Processors
IDA supports more than 50 families of processors. The source code of some of the processor modules i ...
- oracle systemtap tracing
https://github.com/LucaCanali?tab=repositories https://github.com/LucaCanali/Linux_tracing_scripts/t ...