本文主要是介绍Android 模仿城市列表ExpandableListView,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
好久没写博客了。很多东西都没积累,现在要开始重新积累起来了。嘻嘻
最近在写一款获取城市列表,当点击市名称时,将其下面的区名称显示出来,首先我是利用往layout中加入布局文件实现的。第三种是从网上看到有人用ExpandableListView实现的。第四种是自己整理的。
第一种方法跟第二种方法十分类似。第二种方法比较精简一点儿。
public class TwoActivity extends Activity {private LinearLayout layout;private List<String> provinceList = new ArrayList<String>();// 省份列表private List<Map<String, Object>> cityList = new ArrayList<Map<String, Object>>();// 城市列表private final String LIST_PROVINCE = "province";// 省份private final String LIST_CITY = "city";// 城市private final String LIST_IS_OPEN = "isOpen";// 是否展开private final String LIST_AREAS = "areas";// 属于某一个市的区列表@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.two_activity);initView();getData();fillData();}private void initView() {layout = (LinearLayout) findViewById(R.id.layout_two);}private void getData() {for (int i = 0; i < 10; i++) {provinceList.add("province" + i);}for (int j = 0; j < 10; j++) {Map<String, Object> map = new HashMap<String, Object>();map.put(LIST_PROVINCE, provinceList.get(j));map.put(LIST_CITY, "city" + j);map.put(LIST_IS_OPEN, false);List<String> itemList = new ArrayList<String>();for (int m = 0; m < 10; m++) {itemList.add("area is " + m);}map.put(LIST_AREAS, itemList);cityList.add(map);}for (int j = 0; j < 10; j++) {Map<String, Object> map = new HashMap<String, Object>();map.put(LIST_PROVINCE, provinceList.get(j));map.put(LIST_CITY, "cityone" + j);map.put(LIST_IS_OPEN, false);List<String> itemList = new ArrayList<String>();for (int m = 0; m < 10; m++) {itemList.add("area is " + m);}map.put(LIST_AREAS, itemList);cityList.add(map);}}@SuppressWarnings("unchecked")@SuppressLint("InflateParams")private void fillData() {for (int i = 0; i < provinceList.size(); i++) {View v_title = LayoutInflater.from(this).inflate(R.layout.item_province, null);TextView tv_title = (TextView) v_title.findViewById(R.id.tv_title_item);tv_title.setText(provinceList.get(i));layout.addView(v_title);for (int j = 0; j < cityList.size(); j++) {if (provinceList.get(i).equals(cityList.get(j).get(LIST_PROVINCE))) {View v_city = LayoutInflater.from(this).inflate(R.layout.item_city, null);TextView tv_city = (TextView) v_city.findViewById(R.id.tv_city_item);tv_city.setText(cityList.get(j).get(LIST_CITY).toString());final int position = j;tv_city.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {boolean isOpen = !(boolean) cityList.get(position).get(LIST_IS_OPEN);Map<String, Object> newMap = new HashMap<String, Object>();newMap = cityList.get(position);newMap.put(LIST_IS_OPEN, isOpen);layout.removeAllViews();fillData();}});layout.addView(v_city);if ((boolean) cityList.get(j).get(LIST_IS_OPEN)) {List<String> list = new ArrayList<String>();list = (List<String>) cityList.get(j).get(LIST_AREAS);for (int n = 0; n < list.size(); n++) {View v_area = LayoutInflater.from(TwoActivity.this).inflate(R.layout.item_area, null);TextView tv_area = (TextView) v_area.findViewById(R.id.tv_area_item);tv_area.setText(list.get(n));layout.addView(v_area);}}}}}}}
第四种方法是使用ExpandableListView来实现的。比第二种方法简单很多。并且可以实现相同的功能。
public class FourActivity extends Activity {private ExpandableListView expandableListview;private FourAdapter adapter;private List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();// 存储数据的列表@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.four_activity);initView();getListData();adapter.setList(dataList);}private void initView() {expandableListview = (ExpandableListView) findViewById(R.id.elv_four);expandableListview.setGroupIndicator(null);// 设置属性,去掉默认的箭头adapter = new FourAdapter(FourActivity.this);expandableListview.setAdapter(adapter);}private void getListData() {for (int i = 0; i < 10; i++) {Map<String, Object> pMap = new HashMap<String, Object>();pMap.put(Constances.LIST_TYPE, Constances.TYPE_PROVINCE);pMap.put(Constances.LIST_NAME, "province --- " + i);dataList.add(pMap);for (int j = 0; j < 3; j++) {Map<String, Object> map = new HashMap<String, Object>();map.put(Constances.LIST_TYPE, Constances.TYPE_CITY);map.put(Constances.LIST_NAME, "province --- " + i + "city === "+ j);List<String> itemList = new ArrayList<String>();for (int m = 0; m < 10; m++) {itemList.add("province --- " + i + "city === " + j+ "area is " + m);}map.put(Constances.LIST_AREAS, itemList);dataList.add(map);}}}
}
实现的功能十分简单。就是一个城市列表。点击查看更多。
源码下载地址:http://download.csdn.net/detail/u011382076/8752309
直接下载就可以运行的一个工程。欢迎大家指教
这篇关于Android 模仿城市列表ExpandableListView的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!