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,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...
随机推荐
- poj2864
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int main ...
- Spring、整合Spring+JDBC
首先引入Spring包和JDBC所使用到的包: 配置beans.xml步骤: 1.配置dataSource的属性 2.将DataSource交给DataSourceTransactionManager ...
- #include <process.h>
1 _beginthread 单进程,单线程,必须干完一件事情后干另一件事情 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #incl ...
- sdut 3-5 学生成绩统计
3-5 学生成绩统计 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 通过本题目练习能够掌握对象数组的使用方法,主要是对象数组中数据的输入输出操作. 设计 ...
- js中 ===与==
js里面,==比较的是参数的值,不会比较参数的类型,===需要先比较参数的类型是否一致,然后才会去比较值比如,if(3 == "3")这个会返回true,if(3 === &quo ...
- Global.asax文件的说明
每个应用程序可以包含一个特殊的目录(/bin)和两个特殊的文件(Web.config和Global.asax) Global.asax文件的使用: 作用:处理应用程序范围内的事件,声明应用程序范围的对 ...
- localhost和127.0.0.1区别
详情见: http://blog.csdn.net/xifeijian/article/details/12879395
- C# 创建数组的几种方法
第一种 string[] myArray=new string[10]; 第二种 string[] myArray={"1","2"}; 第三种 string[ ...
- 经典:十步完全理解 SQL
经典:十步完全理解 SQL 来源:伯乐在线 链接:http://blog.jobbole.com/55086/ 很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完 ...
- C/C++输入输出
一. cin>>当碰到空格或换行符'\n'时,输入结束 该操作符是根据后面变量的类型读取数据. 输入结束条件 :遇到Enter.Space.Tab键. 对结束符的处理 :丢弃缓冲区中使得输 ...