一、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. [MySQL-笔记]创建高性能索引

    索引,MySQL中也叫“键”,是存储引擎中用于快速找到记录的一种数据结构,具体的工作方式就像书本中的索引一样,但是具体的实现方式会有差别. 一.索引分类 B-Tree索引: 优点: MyISAM中,索 ...

  2. ceph部署过程中的错误

    ceph版本-jewel 用ssd盘来journal ,格式分区权限问题 [ceph-node2][WARNIN] ceph_disk.main.FilesystemTypeError: Cannot ...

  3. 【WIN10】使用自己的PageLoader加載Page

    源碼下載:http://yunpan.cn/cFwwrT4V5rHIM  访问密码 1b97 在上一篇博客中,我已經說明了為什麼要自己寫一個PageLoader.原因就是,Frame的GoBack只是 ...

  4. Codeforces 1073G Yet Another LCP Problem $SA$+单调栈

    题意 给出一个字符串\(s\)和\(q\)个询问. 每次询问给出两个长度分别为\(k,l\)的序列\(a\)和序列\(b\). 求\(\sum_{i=1}^{k}\sum_{j=1}^{l}lcp(s ...

  5. 20162325 金立清 S2 W11 C20

    20162325 2017-2018-2 <程序设计与数据结构>第11周学习总结 教材关键概念摘要 在哈希方法中,元素保存在哈希表中,其在表中的位置由哈希函数确定. 两个元素或关键字映射到 ...

  6. Codeforces Round #257 (Div. 2) C. Jzzhu and Chocolate

    C. Jzzhu and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. SQLServer存储过程返回值总结

    1.  存储过程没有返回值的情况(即存储过程语句中没有return之类的语句)  用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况  (1)假如通 ...

  8. Windows程序的打包,部署(vs项目打包vs2013)---ShinePans

    Windows 应用程序在开发完毕之后,怎样将程序打包并制作成安装程序在客户机上部署 是每一个windows应用程序开发完毕之后都必须面对的问题. 学习目标:                    部 ...

  9. mysql 连接出错 'mysqladmin flush-hosts'

    本文章 转载于: http://blog.itechol.com/space-33-do-blog-id-5670.html    求助QQ:499628121   环境说明:   内网测试服务器19 ...

  10. .NET泛型02,泛型的使用

    在" .NET泛型01,为什么需要泛型,泛型基本语法"中,了解了泛型的基本概念,本篇偏重于泛型的使用.主要包括: ■ 泛型方法重载需要注意的问题■ 泛型的类型推断■ 泛型方法也可以 ...