android入门——UI(5)
最近时间实在匆忙,博客的代码基本没有解释。
介绍ExpandableListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <ExpandableListView
android:id="@+id/demo_expandable_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> </ExpandableListView>
</LinearLayout>
expandable_list_view_index.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/tv_group"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
item_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:paddingLeft="50dp"> <TextView
android:id="@+id/tv_child"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
item_child.xml
我们首先使用SimpleExpandableListAdapte来为其填充数据
package com.ouc.wkp.ui1; import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created by wkp on 2016/8/24.
*/
public class JustLook6 extends Activity { ExpandableListView expandableListView;
String[] groupStringArr = {"腾讯", "百度", "阿里"};
String[][] childStringArrs = {
{"QQ", "微信", "QQ浏览器"},
{"百度搜索", "百度地图", "百度外卖"},
{"淘宝", "支付宝", "天猫商城"}};
List<Map<String, ?>> groupData=new ArrayList<>();
List<List<Map<String,?>>> childData=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list_view_index); expandableListView = (ExpandableListView) findViewById(R.id.demo_expandable_list_view); for (int i = 0; i < groupStringArr.length; i++) {
Map<String, String> map = new HashMap<>();
map.put("groupName", groupStringArr[i]);
groupData.add(map); List<Map<String,?>> itemList=new ArrayList<>();
for(int j=0;j<childStringArrs[i].length;j++){
Map<String,String> map0=new HashMap<>();
map0.put("itemName",childStringArrs[i][j]);
itemList.add(map0);
}
childData.add(itemList);
} SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter(this,
groupData, R.layout.item_group, new String[]{"groupName"}, new int[]{R.id.tv_group},
childData,R.layout.item_child,new String[]{"itemName"},new int[]{R.id.tv_child}); expandableListView.setAdapter(simpleExpandableListAdapter);
}
}
JustLook6.java
SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter(this,
groupData, R.layout.item_group, new String[]{"groupName"}, new int[]{R.id.tv_group},
childData,R.layout.item_child,new String[]{"itemName"},new int[]{R.id.tv_child}); 这个函数比较吓人,有9个参数,但是我们可以拆分成1+4+4的形式,其中的后面两组4个参数和SimpleAdapter类似。
依次为数据源,布局文件,“从哪里来(from)”,“对应到哪里去(to)” 然后我们可以使用实现BaseExpandableListAdapter类来填充数据
package com.ouc.wkp.ui1; import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView; /**
* Created by wkp on 2016/8/24.
*/
public class JustLook7 extends Activity { private ExpandableListView expandableListView;
private MyDemoBaseExpandableListAdapter adapter;
String[] groupStringArr = {"腾讯", "百度", "阿里"};
String[][] childStringArrs = {
{"QQ", "微信", "QQ浏览器"},
{"百度搜索", "百度地图", "百度外卖"},
{"淘宝", "支付宝", "天猫商城"}}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list_view_index); expandableListView = (ExpandableListView) findViewById(R.id.demo_expandable_list_view); adapter = new MyDemoBaseExpandableListAdapter(); expandableListView.setAdapter(adapter);
} class MyDemoBaseExpandableListAdapter extends BaseExpandableListAdapter { @Override
public int getGroupCount() {
return groupStringArr.length;
} @Override
public int getChildrenCount(int i) {
return childStringArrs[i].length;
} @Override
public Object getGroup(int i) {
return groupStringArr[i];
} @Override
public Object getChild(int i, int i1) {
return childStringArrs[i][i1];
} @Override
public long getGroupId(int i) {
return i * 100;
} @Override
public long getChildId(int i, int i1) {
return i * 100 + i1;
} @Override
public boolean hasStableIds() {
return false;
} @Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_group, null);
TextView textView = (TextView) view.findViewById(R.id.tv_group);
Object data = getGroup(i);
textView.setText((String) data);
if (i % 2 == 1) {
textView.setTextColor(Color.RED);
} else {
textView.setTextColor(Color.GREEN);
}
return view;
} @Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_child,null);
TextView textView=(TextView)view.findViewById(R.id.tv_child);
Object childData=getChild(i,i1);
textView.setText((String)childData);
if(i1>1){
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,30);
}else{
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
}
return view;
} @Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
}
}
JustLook7.java
运行效果

android入门——UI(5)的更多相关文章
- Android入门——UI(8)——Fragment(2)
先演示一下如何在一个activity中放置两个Fragment,先定义两个Fragment <?xml version="1.0" encoding="utf-8& ...
- Android入门——UI(9)
SwipRefreshLayout下拉刷新控件 <?xml version="1.0" encoding="utf-8"?> <android ...
- Android入门——UI(7)——Fragment
先上fragment静态加载的代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- android入门——UI(6)——ViewPager+Menu+PopupWindow
一.使用ViewPager开发新特性引导界面 <?xml version="1.0" encoding="utf-8"?> <Relative ...
- android入门——UI(4)
GridView控件实现菜单 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...
- android入门——UI(3)
Spinner控件 ListView控件 一.Spinner控件 点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner选择一个新值. 有两种指定数据源的 ...
- Android入门——UI(2)
介绍SeekBar拖动条控件.ProgressBar进度条控件.DatePicker日历控件.TimePicker时间控件 <?xml version="1.0" encod ...
- android入门——UI(1)
一.使用TextView ImageView Button EditView做出登录页面 <?xml version="1.0" encoding="utf-8&q ...
- 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity
问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...
随机推荐
- Android各层推荐开发书籍及参考资料
Android各层推荐开发书籍及参考资料 转自:http://blog.csdn.net/fancylovejava/article/details/8657058 Android系统按照架构来说一共 ...
- 搭建本地Ubuntu 镜像服务器
一.需求分析 最近公司软件Team 有个需求是这样的:能不能在局域网搭建一个Ubuntu 镜像服务器, 这样作的好处是可以节省Ubuntu某些常用工具的安装时间. 二.部署过程 2.1 测试环境 目前 ...
- 在自定义的js验证规则中调用magento的VarienForm方法验证表单
js部分<script type="text/javascript"> //<![CDATA[ var loginForm = new VarienForm('l ...
- HTML文本框
文本框样式大全 输入框景背景透明:<input style="background:transparent;border:1px solid #ffffff"> 鼠 ...
- MSSQL查询连接数
SELECT * FROM [Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN ( SELECT [DBID] FROM [Master].[dbo].[SYS ...
- SQL Server执行计划那些事儿(2)——查找和扫描
接下来的文章是记录自己曾经的盲点,同时也透漏了自己的发展历程(可能发展也算不上,只能说是瞎混).当然,一些盲点也在工作和探究过程中慢慢有些眉目,现在也愿意发扬博客园的奉献精神,拿出来和大家分享一下. ...
- AngularJS 实战讲义笔记
第一部分 快速上手 1.1 感受AngularJs四大核心特性(MVC, 模块化,指令系统,双向数据绑定)1.2 搭建自动化的前端开发,调试,测试环境 代码编辑工具 (sublime) 断点调试工具 ...
- Python核心编程读笔 2
第三章 python基础 一.语句和语法 \n 标准的行分隔符 \ 继续上一行 ; 将两个语句连接在一行 : 分开代码块的头和体 代码块以缩进块的形式体现 python文件以模块的形式组织 二.变量赋 ...
- MySQL:ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
MySQL在删除一张表时出现 ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fa ...
- onload ready
确保在 <body> 元素的onload事件中没有注册函数,否则不会触发$(document).ready()事件. 可以在同一个页面中无限次地使用$(document).ready()事 ...