ExpandableListView显示省市列表(一)

上午面试被问到某app中离线状态下城市选择如何实现,瞎扯一番过后,看到了面试官鄙视的眼神,下午开始写了个demo实现此功能。

效果图如下:


关键技术点总结:

<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>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用ExpandableListView可以实现QQ好友分组列表。首先需要创建一个ExpandableListView控件,并为其设置适配器。适配器需要继承BaseExpandableListAdapter,并实现以下方法: 1. getGroupCount():获取分组的数量。 2. getChildrenCount():获取某个分组下子项的数量。 3. getGroup():获取某个分组的数据。 4. getChild():获取某个分组下某个子项的数据。 5. getGroupId():获取某个分组的ID。 6. getChildId():获取某个分组下某个子项的ID。 7. hasStableIds():判断分组和子项的ID是否稳定。 8. getGroupView():获取分组的视图。 9. getChildView():获取子项的视图。 10. isChildSelectable():判断子项是否可选中。 在实现适配器的过程中,需要根据数据源的结构来设置分组和子项的数据。例如,可以使用一个List<List<String>>来存储分组和子项的数据,其中外层List表示分组,内层List表示子项。在getGroup()和getChild()方法中,需要根据groupPosition和childPosition来获取对应的数据。 最后,需要为ExpandableListView设置分组的展开和收起事件。可以通过设置OnGroupClickListener和OnChildClickListener来实现。在OnGroupClickListener中,需要根据groupPosition来判断当前分组是否已经展开,如果已经展开则返回false,否则返回true。在OnChildClickListener中,可以根据childPosition来获取对应的数据,并进行相应的操作。 通过以上步骤,就可以实现一个简单的QQ好友分组列表

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值