Android省市區三級聯動滾輪選擇(真實項目中提取出來的組件)

最近項目要做一個,類似淘寶手機客戶端的,選擇收貨地址的三級聯動滾動選擇組件,下面是它的大致界面截圖:



在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中,代碼如下:


  1. package com.mrwujay.cascade.service;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import org.xml.sax.Attributes;  
  6. import org.xml.sax.SAXException;  
  7. import org.xml.sax.helpers.DefaultHandler;  
  8.   
  9. import com.mrwujay.cascade.model.CityModel;  
  10. import com.mrwujay.cascade.model.DistrictModel;  
  11. import com.mrwujay.cascade.model.ProvinceModel;  
  12.   
  13. public class XmlParserHandler extends DefaultHandler {  
  14.   
  15.     /** 
  16.      * 存儲所有的解析對象 
  17.      */  
  18.     private List<ProvinceModel> provinceList = new ArrayList<ProvinceModel>();  
  19.             
  20.     public XmlParserHandler() {  
  21.           
  22.     }  
  23.   
  24.     public List<ProvinceModel> getDataList() {  
  25.         return provinceList;  
  26.     }  
  27.   
  28.     @Override  
  29.     public void startDocument() throws SAXException {  
  30.         // 當讀到第一個開始標籤的時候,會觸發這個方法  
  31.     }  
  32.   
  33.     ProvinceModel provinceModel = new ProvinceModel();  
  34.     CityModel cityModel = new CityModel();  
  35.     DistrictModel districtModel = new DistrictModel();  
  36.       
  37.     @Override  
  38.     public void startElement(String uri, String localName, String qName,  
  39.             Attributes attributes) throws SAXException {  
  40.         // 當遇到開始標記的時候,調用這個方法  
  41.         if (qName.equals("province")) {  
  42.             provinceModel = new ProvinceModel();  
  43.             provinceModel.setName(attributes.getValue(0));  
  44.             provinceModel.setCityList(new ArrayList<CityModel>());  
  45.         } else if (qName.equals("city")) {  
  46.             cityModel = new CityModel();  
  47.             cityModel.setName(attributes.getValue(0));  
  48.             cityModel.setDistrictList(new ArrayList<DistrictModel>());  
  49.         } else if (qName.equals("district")) {  
  50.             districtModel = new DistrictModel();  
  51.             districtModel.setName(attributes.getValue(0));  
  52.             districtModel.setZipcode(attributes.getValue(1));  
  53.         }  
  54.     }  
  55.   
  56.     @Override  
  57.     public void endElement(String uri, String localName, String qName)  
  58.             throws SAXException {  
  59.         // 遇到結束標記的時候,會調用這個方法  
  60.         if (qName.equals("district")) {  
  61.             cityModel.getDistrictList().add(districtModel);  
  62.         } else if (qName.equals("city")) {  
  63.             provinceModel.getCityList().add(cityModel);  
  64.         } else if (qName.equals("province")) {  
  65.             provinceList.add(provinceModel);  
  66.         }  
  67.     }  
  68.       
  69.     @Override  
  70.     public void characters(char[] ch, int start, int length)  
  71.             throws SAXException {  
  72.     }  
  73.   
  74. }  


通過XmlParserHandler.java提供的getDataList()方法獲取得到,之後再進行拆分放到省、市、區不同的HashMap裏面方便做數據適配。

這裏是它的具體實現代碼:

  1. protected void initProvinceDatas()  
  2.     {  
  3.         List<ProvinceModel> provinceList = null;  
  4.         AssetManager asset = getAssets();  
  5.         try {  
  6.             InputStream input = asset.open("province_data.xml");  
  7.             // 創建一個解析xml的工廠對象  
  8.             SAXParserFactory spf = SAXParserFactory.newInstance();  
  9.             // 解析xml  
  10.             SAXParser parser = spf.newSAXParser();  
  11.             XmlParserHandler handler = new XmlParserHandler();  
  12.             parser.parse(input, handler);  
  13.             input.close();  
  14.             // 獲取解析出來的數據  
  15.             provinceList = handler.getDataList();  
  16.             //*/ 初始化默認選中的省、市、區  
  17.             if (provinceList!= null && !provinceList.isEmpty()) {  
  18.                 mCurrentProviceName = provinceList.get(0).getName();  
  19.                 List<CityModel> cityList = provinceList.get(0).getCityList();  
  20.                 if (cityList!= null && !cityList.isEmpty()) {  
  21.                     mCurrentCityName = cityList.get(0).getName();  
  22.                     List<DistrictModel> districtList = cityList.get(0).getDistrictList();  
  23.                     mCurrentDistrictName = districtList.get(0).getName();  
  24.                     mCurrentZipCode = districtList.get(0).getZipcode();  
  25.                 }  
  26.             }  
  27.             //*/  
  28.             mProvinceDatas = new String[provinceList.size()];  
  29.             for (int i=0; i< provinceList.size(); i++) {  
  30.                 mProvinceDatas[i] = provinceList.get(i).getName();  
  31.                 List<CityModel> cityList = provinceList.get(i).getCityList();  
  32.                 String[] cityNames = new String[cityList.size()];  
  33.                 for (int j=0; j< cityList.size(); j++) {  
  34.                     cityNames[j] = cityList.get(j).getName();  
  35.                     List<DistrictModel> districtList = cityList.get(j).getDistrictList();  
  36.                     String[] distrinctNameArray = new String[districtList.size()];  
  37.                     DistrictModel[] distrinctArray = new DistrictModel[districtList.size()];  
  38.                     for (int k=0; k<districtList.size(); k++) {  
  39.                         DistrictModel districtModel = new DistrictModel(districtList.get(k).getName(), districtList.get(k).getZipcode());  
  40.                         mZipcodeDatasMap.put(districtList.get(k).getName(), districtList.get(k).getZipcode());  
  41.                         distrinctArray[k] = districtModel;  
  42.                         distrinctNameArray[k] = districtModel.getName();  
  43.                     }  
  44.                     mDistrictDatasMap.put(cityNames[j], distrinctNameArray);  
  45.                 }  
  46.                 mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);  
  47.             }  
  48.         } catch (Throwable e) {    
  49.             e.printStackTrace();    
  50.         } finally {  
  51.               
  52.         }   
  53.     }  


在使用wheel組件時,數據適配起來也很方便,只需要做些數據、顯示數量的配置即可,我這邊設置了一行顯示7條數據

  1. initProvinceDatas();  
  2.         mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(MainActivity.this, mProvinceDatas));  
  3.         // 設置可見條目數量  
  4.         mViewProvince.setVisibleItems(7);  
  5.         mViewCity.setVisibleItems(7);  
  6.         mViewDistrict.setVisibleItems(7);  
  7.         updateCities();  
  8.         updateAreas();  

要監聽wheel組件的滑動、點擊、選中改變事件,可以通過實現它的三個事件監聽接口來實現,分別是:

1、OnWheelScrollListener 滑動事件:

  1. /** 
  2.  * Wheel scrolled listener interface. 
  3.  */  
  4. public interface OnWheelScrollListener {  
  5.     /** 
  6.      * Callback method to be invoked when scrolling started. 
  7.      * @param wheel the wheel view whose state has changed. 
  8.      */  
  9.     void onScrollingStarted(WheelView wheel);  
  10.       
  11.     /** 
  12.      * Callback method to be invoked when scrolling ended. 
  13.      * @param wheel the wheel view whose state has changed. 
  14.      */  
  15.     void onScrollingFinished(WheelView wheel);  
  16. }  

2、OnWheelClickedListener 條目點擊事件:

  1. /** 
  2.  * Wheel clicked listener interface. 
  3.  * <p>The onItemClicked() method is called whenever a wheel item is clicked 
  4.  * <li> New Wheel position is set 
  5.  * <li> Wheel view is scrolled 
  6.  */  
  7. public interface OnWheelClickedListener {  
  8.     /** 
  9.      * Callback method to be invoked when current item clicked 
  10.      * @param wheel the wheel view 
  11.      * @param itemIndex the index of clicked item 
  12.      */  
  13.     void onItemClicked(WheelView wheel, int itemIndex);  
  14. }  

3、OnWheelChangedListener 被選中項的positon變化事件:

  1. /** 
  2.  * Wheel changed listener interface. 
  3.  * <p>The onChanged() method is called whenever current wheel positions is changed: 
  4.  * <li> New Wheel position is set 
  5.  * <li> Wheel view is scrolled 
  6.  */  
  7. public interface OnWheelChangedListener {  
  8.     /** 
  9.      * Callback method to be invoked when current item changed 
  10.      * @param wheel the wheel view whose state has changed 
  11.      * @param oldValue the old value of current item 
  12.      * @param newValue the new value of current item 
  13.      */  
  14.     void onChanged(WheelView wheel, int oldValue, int newValue);  
  15. }  

這裏只要知道哪個省、市、區被選中了,實現第三個接口就行,在方法回調時去作同步和更新數據,比如省級條目滑動的時候,市級和縣級數據都要做對應的適配、市級滑動時需要去改變縣級(區)的數據,這樣才能實現級聯的效果,至於如何改變,需要三個HashMap來分別保存他們的對應關係:

  1. /** 
  2.      * key - 省 value - 市 
  3.      */  
  4.     protected Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();  
  5.     /** 
  6.      * key - 市 values - 區 
  7.      */  
  8.     protected Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();  
  9.       
  10.     /** 
  11.      * key - 區 values - 郵編 
  12.      */  
  13.     protected Map<String, String> mZipcodeDatasMap = new HashMap<String, String>();   

在onChanged()回調方法中,對於省、市、區/縣的滑動,分別做數據的適配,代碼如下:

  1. @Override  
  2.     public void onChanged(WheelView wheel, int oldValue, int newValue) {  
  3.         // TODO Auto-generated method stub  
  4.         if (wheel == mViewProvince) {  
  5.             updateCities();  
  6.         } else if (wheel == mViewCity) {  
  7.             updateAreas();  
  8.         } else if (wheel == mViewDistrict) {  
  9.             mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue];  
  10.             mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);  
  11.         }  
  12.     }  
  13.   
  14.     /** 
  15.      * 根據當前的市,更新區WheelView的信息 
  16.      */  
  17.     private void updateAreas() {  
  18.         int pCurrent = mViewCity.getCurrentItem();  
  19.         mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];  
  20.         String[] areas = mDistrictDatasMap.get(mCurrentCityName);  
  21.   
  22.         if (areas == null) {  
  23.             areas = new String[] { "" };  
  24.         }  
  25.         mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(this, areas));  
  26.         mViewDistrict.setCurrentItem(0);  
  27.     }  
  28.   
  29.     /** 
  30.      * 根據當前的省,更新市WheelView的信息 
  31.      */  
  32.     private void updateCities() {  
  33.         int pCurrent = mViewProvince.getCurrentItem();  
  34.         mCurrentProviceName = mProvinceDatas[pCurrent];  
  35.         String[] cities = mCitisDatasMap.get(mCurrentProviceName);  
  36.         if (cities == null) {  
  37.             cities = new String[] { "" };  
  38.         }  
  39.         mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));  
  40.         mViewCity.setCurrentItem(0);  
  41.         updateAreas();  
  42.     }  


綜上代碼,最終實現的界面截圖:



源碼下載地址:http://download.csdn.net/detail/wulianghuan/8205211

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章