ExpandableListView显示省市列表(一)
2015-07-21 18:13
281 查看
上午面试被问到某app中离线状态下城市选择如何实现,瞎扯一番过后,看到了面试官鄙视的眼神,下午开始写了个demo实现此功能。
效果图如下:
关键技术点总结:
工程截图:
xml:
代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/group_provice"
android:background="#B4B595"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="25sp"
android:textColor="#FFFFFF"
android:paddingLeft="10dp"
android:gravity="center_vertical"
/>
</RelativeLayout>
MainActivity
效果图如下:
关键技术点总结:
<pre name="code" class="java"><span style="font-size:18px;">1 List<String> proList 存放省份信息, Map<String,List> cityData 存放城市信息,key为"省份名",List存放该省份对应的城市列表。 2 </span><span style="font-size:18px;">ExpandableListView</span><span style="font-size:18px;"> 默认子项是折叠的,且点击group可在折叠与不折叠之前切换 要设置初始化子项不折叠,且点击group不折叠,需用以下代码: //设置所有子项不折叠 eListView.setAdapter(adapter); for(int i=0;i<proList.size();i++){ eListView.expandGroup(i); } //点击group 不折叠 eListView.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { // TODO Auto-generated method stub return true; } }); </span>
<pre name="code" class="java"><span style="font-size:18px;"> 3 Android_ExpandableListView_子item响应点击事件 如果使用ExpandableListView,需要子item响应一个事件,比如重新启动一个新的activity,需要满足下面的条件: (1) 修改Adapter返回值 覆写BaseExpandableListAdapter的isChildSelectable()的返回值为true; (2) 绑定监听器 调用ExpandableListView对象的setOnChildClickListener()方法,为其绑定监听器 </span>
工程截图:
xml:
代码:
<span style="color:#330000;"><strong><span style="font-size:18px;">xml: </span> </strong><span style="font-size:18px;"><strong>activity_main.xml </strong> </span></span><pre name="code" class="html"><span style="font-size:18px;color:#330000;"><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" tools:context=".MainActivity" > <ExpandableListView android:id="@+id/eListView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <strong>view_child.xml</strong> </span><pre name="code" class="html"><span style="font-size:18px;color:#330000;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/group_city" android:background="#FFFFFF" android:layout_width="match_parent" android:layout_height="50dp" android:textSize="22sp" android:textColor="#000000" android:paddingLeft="10dp" android:gravity="center_vertical" /> </RelativeLayout></span>view_group.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/group_provice"
android:background="#B4B595"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="25sp"
android:textColor="#FFFFFF"
android:paddingLeft="10dp"
android:gravity="center_vertical"
/>
</RelativeLayout>
MainActivity
<span style="font-size:18px;">package com.example.provincecity; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.database.DataSetObserver; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupClickListener; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private ExpandableListView eListView; private List<String> proList; private Map<String,List> cityData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getData(); eListView=(ExpandableListView) findViewById(R.id.eListView); ExpandableListAdapter adapter=new ExpandableListAdapter() { @Override public void unregisterDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } @Override public void registerDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } @Override public void onGroupExpanded(int groupPosition) { // TODO Auto-generated method stub } @Override public void onGroupCollapsed(int groupPosition) { // TODO Auto-generated method stub } @Override public boolean isEmpty() { // TODO Auto-generated method stub return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { HolderView holderView=new HolderView(); if(convertView==null){ holderView=new HolderView(); convertView=View.inflate(getApplicationContext(), R.layout.view_group, null); holderView.proText=(TextView) convertView.findViewById(R.id.group_provice); convertView.setTag(holderView); }else{ holderView=(HolderView) convertView.getTag(); } holderView.proText.setText(proList.get(groupPosition)); return convertView; } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return 0; } @Override public int getGroupCount() { // TODO Auto-generated method stub return proList==null?0:proList.size(); } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return proList.get(groupPosition); } @Override public long getCombinedGroupId(long groupId) { // TODO Auto-generated method stub return 0; } @Override public long getCombinedChildId(long groupId, long childId) { // TODO Auto-generated method stub return 0; } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub String pro=proList.get(groupPosition); ArrayList<String> cityList = (ArrayList<String>) cityData.get(pro); return cityList.size(); } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { String key=(String) getChild(groupPosition, childPosition); View view= View.inflate(getApplicationContext(), R.layout.view_child,null); TextView textView=(TextView)view.findViewById(R.id.group_city); textView.setText(key); return view; } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return 0; } @Override public Object getChild(int groupPosition, int childPosition) { String pro=proList.get(groupPosition); return cityData.get(pro).get(childPosition); } @Override public boolean areAllItemsEnabled() { // TODO Auto-generated method stub return false; } }; //设置所有子项不折叠 eListView.setAdapter(adapter); for(int i=0;i<proList.size();i++){ eListView.expandGroup(i); } //点击group 不折叠 eListView.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { // TODO Auto-generated method stub return true; } }); //点击子项弹出城市信息 eListView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { String pro=proList.get(groupPosition); String city=(String) cityData.get(pro).get(childPosition); Toast.makeText(getApplicationContext(), pro+"省"+city+"市", 1).show(); // TODO Auto-generated method stub return false; } }); } class HolderView { TextView proText; } //获取省份和城市列表 private void getData() { // TODO Auto-generated method stub proList=new ArrayList<String>(); proList.add("湖北"); proList.add("湖南"); proList.add("广东"); cityData=new HashMap<String, List>(); List<String> huBeiCityList=new ArrayList<String>(); huBeiCityList.add("武汉"); huBeiCityList.add("襄阳"); huBeiCityList.add("宜昌"); huBeiCityList.add("荆州"); huBeiCityList.add("十堰"); cityData.put("湖北",huBeiCityList); List<String> huNanCityList=new ArrayList<String>(); huNanCityList.add("长沙"); huNanCityList.add("常德"); huNanCityList.add("衡阳"); huNanCityList.add("株洲"); cityData.put("湖南",huNanCityList); List<String> guangDongCityList=new ArrayList<String>(); guangDongCityList.add("广州"); guangDongCityList.add("深圳"); guangDongCityList.add("珠海"); cityData.put("广东",guangDongCityList); } }</span>
相关文章推荐
- mount:Read-only file system Android手机不能挂载读写,终极解决方案
- c语言中如何妙用do...while(0)语句
- 使用Android开发录音和播放功能小程序示例
- 为什么写测试用例?编写测试用例从何着手?测试用例包含什么内容?
- 基本图形算法指南
- 工作总结(一)、c3p0,
- SlidingMenu 详解系列(一)API介绍
- 无可引导光盘时如何成功安装CUCM/IM&Presence等系统的方法汇总
- Resource interpreted as Stylesheet but transferred with MIME type application/x-css
- 防止图片重复下载方案,图像压缩保存与压缩显示
- mysql source 乱码
- java反射机制入门01
- 程序员人生之路
- Python自学笔记之函数式编程5——返回函数
- android framework SystemUI 修改NavigationBar靠右边
- android开源框架emojicon,妈妈再也不用担心我没表情了
- Qt运行一个实例进程
- 获取单选框的值有三种方式
- apache 使用a2ensite和a2dissite来快速切换站点
- hdu5288 OO’s Sequence 二分 多校联合第一场