最近項目要做一個,類似淘寶手機客戶端的,選擇收貨地址的三級聯動滾動選擇組件,下面是它的大致界面截圖:
在IOS中有個叫UIPickerView的選擇器,並且在dataSource中定義了UIPickerView的數據源和定製內容,所以用只要熟悉它的基本用法,要實現這麼個三級聯動滑動選擇是挺簡單的。
言歸正傳,今天討論的是在Android裏面如何來實現這麼個效果,那麼如何實現呢??? 相信部分童鞋首先想到的是android.widget.DatePicker和android.widget.TimePicker,因爲它們的樣子長得很像,事實就是它們僅僅是長得相而已,Google在設計這個兩個widget的時候,並沒有提供對外的數據源適配接口,帶來的問題就是,我們只能通過它們來選擇日期和時間,至於爲什麼這樣設計,如果有童鞋知道,請給我留言,Thanks~
DatePicker.class包含的方法截圖:
全都是關於時間獲取用的方法.
好了,既然在Android中沒辦法偷懶的用一個系統widget搞定,那麼只能自己來自定義view來實現了,這篇就圍繞這個來展開分享一下,我在項目中實現這個的全過程。首先是做了下開源代碼調研,在github上面有一個叫做 android-wheel 的開源控件,
代碼地址https://github.com/maarek/android-wheel
是一個非常好用的組件,對於數據適配接口的抽取和事件的回調都做了抽取,代碼的耦合度低,唯一不足就是在界面的定製這塊,如果你需要做更改,需要去動源代碼的。我這裏在界面的代碼做了改動,放在我的項目src目錄下了:
在此次項目中,省市區及郵編的數據是放在了assets/province_data.xml裏面,是產品經理花了好幾天時間整理的,絕對是最齊全和完善了,辛苦辛苦!!!
關於XML的解析,一共有SAX、PULL、DOM三種解析方式,這裏就不講了,可以看我的前面的幾篇學習的文章:
Android解析XML方式(一)使用SAX解析
Android解析XML方式(二)使用PULL解析XML
Android解析XML方式(三)使用DOM解析XML
此次項目中使用的是SAX解析方式,因爲它佔用內存少,並且速度快,數據解析代碼寫在了 com.mrwujay.cascade.service/XmlParserHandler.java中,代碼如下:
-
package com.mrwujay.cascade.service;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
import org.xml.sax.Attributes;
-
import org.xml.sax.SAXException;
-
import org.xml.sax.helpers.DefaultHandler;
-
-
import com.mrwujay.cascade.model.CityModel;
-
import com.mrwujay.cascade.model.DistrictModel;
-
import com.mrwujay.cascade.model.ProvinceModel;
-
-
public class XmlParserHandler extends DefaultHandler {
-
-
-
-
-
private List<ProvinceModel> provinceList = new ArrayList<ProvinceModel>();
-
-
public XmlParserHandler() {
-
-
}
-
-
public List<ProvinceModel> getDataList() {
-
return provinceList;
-
}
-
-
@Override
-
public void startDocument() throws SAXException {
-
-
}
-
-
ProvinceModel provinceModel = new ProvinceModel();
-
CityModel cityModel = new CityModel();
-
DistrictModel districtModel = new DistrictModel();
-
-
@Override
-
public void startElement(String uri, String localName, String qName,
-
Attributes attributes) throws SAXException {
-
-
if (qName.equals("province")) {
-
provinceModel = new ProvinceModel();
-
provinceModel.setName(attributes.getValue(0));
-
provinceModel.setCityList(new ArrayList<CityModel>());
-
} else if (qName.equals("city")) {
-
cityModel = new CityModel();
-
cityModel.setName(attributes.getValue(0));
-
cityModel.setDistrictList(new ArrayList<DistrictModel>());
-
} else if (qName.equals("district")) {
-
districtModel = new DistrictModel();
-
districtModel.setName(attributes.getValue(0));
-
districtModel.setZipcode(attributes.getValue(1));
-
}
-
}
-
-
@Override
-
public void endElement(String uri, String localName, String qName)
-
throws SAXException {
-
-
if (qName.equals("district")) {
-
cityModel.getDistrictList().add(districtModel);
-
} else if (qName.equals("city")) {
-
provinceModel.getCityList().add(cityModel);
-
} else if (qName.equals("province")) {
-
provinceList.add(provinceModel);
-
}
-
}
-
-
@Override
-
public void characters(char[] ch, int start, int length)
-
throws SAXException {
-
}
-
-
}
通過XmlParserHandler.java提供的getDataList()方法獲取得到,之後再進行拆分放到省、市、區不同的HashMap裏面方便做數據適配。
這裏是它的具體實現代碼:
-
protected void initProvinceDatas()
-
{
-
List<ProvinceModel> provinceList = null;
-
AssetManager asset = getAssets();
-
try {
-
InputStream input = asset.open("province_data.xml");
-
-
SAXParserFactory spf = SAXParserFactory.newInstance();
-
-
SAXParser parser = spf.newSAXParser();
-
XmlParserHandler handler = new XmlParserHandler();
-
parser.parse(input, handler);
-
input.close();
-
-
provinceList = handler.getDataList();
-
-
if (provinceList!= null && !provinceList.isEmpty()) {
-
mCurrentProviceName = provinceList.get(0).getName();
-
List<CityModel> cityList = provinceList.get(0).getCityList();
-
if (cityList!= null && !cityList.isEmpty()) {
-
mCurrentCityName = cityList.get(0).getName();
-
List<DistrictModel> districtList = cityList.get(0).getDistrictList();
-
mCurrentDistrictName = districtList.get(0).getName();
-
mCurrentZipCode = districtList.get(0).getZipcode();
-
}
-
}
-
-
mProvinceDatas = new String[provinceList.size()];
-
for (int i=0; i< provinceList.size(); i++) {
-
mProvinceDatas[i] = provinceList.get(i).getName();
-
List<CityModel> cityList = provinceList.get(i).getCityList();
-
String[] cityNames = new String[cityList.size()];
-
for (int j=0; j< cityList.size(); j++) {
-
cityNames[j] = cityList.get(j).getName();
-
List<DistrictModel> districtList = cityList.get(j).getDistrictList();
-
String[] distrinctNameArray = new String[districtList.size()];
-
DistrictModel[] distrinctArray = new DistrictModel[districtList.size()];
-
for (int k=0; k<districtList.size(); k++) {
-
DistrictModel districtModel = new DistrictModel(districtList.get(k).getName(), districtList.get(k).getZipcode());
-
mZipcodeDatasMap.put(districtList.get(k).getName(), districtList.get(k).getZipcode());
-
distrinctArray[k] = districtModel;
-
distrinctNameArray[k] = districtModel.getName();
-
}
-
mDistrictDatasMap.put(cityNames[j], distrinctNameArray);
-
}
-
mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);
-
}
-
} catch (Throwable e) {
-
e.printStackTrace();
-
} finally {
-
-
}
-
}
在使用wheel組件時,數據適配起來也很方便,只需要做些數據、顯示數量的配置即可,我這邊設置了一行顯示7條數據
-
initProvinceDatas();
-
mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(MainActivity.this, mProvinceDatas));
-
-
mViewProvince.setVisibleItems(7);
-
mViewCity.setVisibleItems(7);
-
mViewDistrict.setVisibleItems(7);
-
updateCities();
-
updateAreas();
要監聽wheel組件的滑動、點擊、選中改變事件,可以通過實現它的三個事件監聽接口來實現,分別是:
1、OnWheelScrollListener 滑動事件:
-
-
-
-
public interface OnWheelScrollListener {
-
-
-
-
-
void onScrollingStarted(WheelView wheel);
-
-
-
-
-
-
void onScrollingFinished(WheelView wheel);
-
}
2、OnWheelClickedListener 條目點擊事件:
-
-
-
-
-
-
-
public interface OnWheelClickedListener {
-
-
-
-
-
-
void onItemClicked(WheelView wheel, int itemIndex);
-
}
3、OnWheelChangedListener 被選中項的positon變化事件:
-
-
-
-
-
-
-
public interface OnWheelChangedListener {
-
-
-
-
-
-
-
void onChanged(WheelView wheel, int oldValue, int newValue);
-
}
這裏只要知道哪個省、市、區被選中了,實現第三個接口就行,在方法回調時去作同步和更新數據,比如省級條目滑動的時候,市級和縣級數據都要做對應的適配、市級滑動時需要去改變縣級(區)的數據,這樣才能實現級聯的效果,至於如何改變,需要三個HashMap來分別保存他們的對應關係:
-
-
-
-
protected Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
-
-
-
-
protected Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();
-
-
-
-
-
protected Map<String, String> mZipcodeDatasMap = new HashMap<String, String>();
在onChanged()回調方法中,對於省、市、區/縣的滑動,分別做數據的適配,代碼如下:
-
@Override
-
public void onChanged(WheelView wheel, int oldValue, int newValue) {
-
-
if (wheel == mViewProvince) {
-
updateCities();
-
} else if (wheel == mViewCity) {
-
updateAreas();
-
} else if (wheel == mViewDistrict) {
-
mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue];
-
mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
-
}
-
}
-
-
-
-
-
private void updateAreas() {
-
int pCurrent = mViewCity.getCurrentItem();
-
mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
-
String[] areas = mDistrictDatasMap.get(mCurrentCityName);
-
-
if (areas == null) {
-
areas = new String[] { "" };
-
}
-
mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(this, areas));
-
mViewDistrict.setCurrentItem(0);
-
}
-
-
-
-
-
private void updateCities() {
-
int pCurrent = mViewProvince.getCurrentItem();
-
mCurrentProviceName = mProvinceDatas[pCurrent];
-
String[] cities = mCitisDatasMap.get(mCurrentProviceName);
-
if (cities == null) {
-
cities = new String[] { "" };
-
}
-
mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));
-
mViewCity.setCurrentItem(0);
-
updateAreas();
-
}
綜上代碼,最終實現的界面截圖:
源碼下載地址:http://download.csdn.net/detail/wulianghuan/8205211