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,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...
随机推荐
- Entity Framework学习笔记
原文地址:http://www.cnblogs.com/frankofgdc/p/3600090.html Entity Framework学习笔记——错误汇总 之前的小项目做完了,到了总结经验和 ...
- 关于ztree打开关闭所有节点,选中指定id节点
var isOneByOneExpand=false;//是否递归展开 //展开节点 function expendNode(nodeId){ var node = treeObj.getNodeBy ...
- 5.4const对象
常成员函数 一个const对象可以调用const函数,但不能调用非const成员函数.必须将关键字const放在函数参数表之后,才能说明该函数是一个const成员函数. 声明常成员函数的格式如下: 类 ...
- ChartControl一个小Demo
我X轴设置的是时间,类型是DatetimeY轴设置的是数量,类型是Numerical当日期不一样时显示曲线正常,但是如果是同一天的话就成了下面的效果 怎么做才能让全部是同一天的时候显示小时为单位的曲线 ...
- DML
DML(Data Manipulation Language 数据操控语言),使用insert.update.delete进行数据库的操作.DML一:插入语句 标准的插入语句 insert into ...
- java日期处理 calendar 和date
抄的人家的,仅作学习使用. java Date获取 年月日时分秒 package com.util; import java.text.DateFormat; import java.ut ...
- Semantic UI中的验证控件的事件的使用
1.Semantic UI中的验证控件,功能挺不错的,中文官网的文档写的都比较详细了,我再这里就不再进行重复了,主要是想说一下它的事件的使用方法,这个可能有部分朋友刚开始接触的时候不太了解 注意看这几 ...
- 2014.12.01 B/S之windows8.1下安装IIS
1.打开 控制面板——程序——程序和功能——启用或关闭windows功能 2.找到Internet信息服务 3.等待安装完毕即可 4.控制面板——系统和安全——管理工具——Intern ...
- 自定义UICollectionViewLayout 实现瀑布流
今天研究了一下自定义UICollectionViewLayout. 看了看官方文档,要自定义UICollectionViewLayout,需要创建一个UICollectionViewLayout的子类 ...
- Java 获取 文件md5校验码
讯雷下载的核心思想是校验文件的md5值,两个文件若md5相同则为同一文件. 当得到用户下载某个文件的请求后它根据数据库中保留的文件md5比对出拥有此文件的url, 将用户请求挂接到此url上并仿造一个 ...