English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article shares the android wheel province, city, and county three-level联动 effect for everyone's reference, the specific content is as follows
There is an Android-The open-source control of wheel, code address:https://github.com/maarek/android-wheel
Source Code Download Address:http://xiazai.jb51.net/201610/yuanma/AndroidCascadeMaster(jb51.net).rar
Main Interface Layout
activity_main.xml
<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="wrap_content" android:background="#E"9E9E9" android:orientation="vertical" >" <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" >" <kankan.wheel.widget.WheelView android:id="@"+id/id_province" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" >" </kankan.wheel.widget.WheelView> <kankan.wheel.widget.WheelView android:id="@"+id/id_city" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" >" </kankan.wheel.widget.WheelView> <kankan.wheel.widget.WheelView android:id="@"+id/id_district" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" >" </kankan.wheel.widget.WheelView> </LinearLayout> <Button android:id="@"+id/btn_confirm" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:gravity="center" android:text="Confirm" android:textColor="#000000" /> </LinearLayout>
Under the assets resource file--province_data.xml (part)
<root> <province name="Anhui Province"> <city name="Anqing City"> <district name="Zongyang County" zipcode="246000" /> <district name="Daguan District" zipcode="246000" /> <district name="Huaning County" zipcode="246000" /> <district name="Qianshan County" zipcode="246000" /> <district name="Susong County" zipcode="246000" /> <district name="Taohu County" zipcode="246000" /> <district name="Tongcheng City" zipcode="246000" /> <district name="Wangjiang County" zipcode="246000" /> <district name="Yixiu District" zipcode="246000" /> <district name="Yingjiang District" zipcode="246000" /> <district name="Yuxi County" zipcode="246000" /> <district name="other" zipcode="246000" /> </city>
First look at the bean classes for provinces, cities, and towns
ProvinceModel
package com.mrwujay.cascade.model; import java.util.List; public class ProvinceModel { private String name; private List<CityModel> cityList; public ProvinceModel() { super(); } public ProvinceModel(String name, List<CityModel> cityList) { super(); this.name = name; this.cityList = cityList; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<CityModel> getCityList() { return cityList; } public void setCityList(List<CityModel> cityList) { this.cityList = cityList; } @Override public String toString() { return "ProvinceModel [name=" + name + ", cityList=" + cityList + "]"; } }
package com.mrwujay.cascade.model; import java.util.List; public class CityModel { private String name; private List<DistrictModel> districtList; public CityModel() { super(); } public CityModel(String name, List<DistrictModel> districtList) { super(); this.name = name; this.districtList = districtList; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<DistrictModel> getDistrictList() { return districtList; } public void setDistrictList(List<DistrictModel> districtList) { this.districtList = districtList; } @Override public String toString() { return "CityModel [name=" + name + ", districtList=" + districtList + "]"; } }
DistrictModel
package com.mrwujay.cascade.model; public class DistrictModel { private String name; private String zipcode; public DistrictModel() { super(); } public DistrictModel(String name, String zipcode) { super(); this.name = name; this.zipcode = zipcode; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getZipcode() { return zipcode; } public void setZipcode(String zipcode) { this.zipcode = zipcode; } @Override public String toString() { return "DistrictModel [name=" + name + ", zipcode=" + zipcode + "]"; } }
See the custom XmlParserHandler
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 { /** * Store all the parsed objects */ private List<ProvinceModel> provinceList = new ArrayList<ProvinceModel>(); public XmlParserHandler() { } /** * Methods exposed to the outside */ public List<ProvinceModel> getDataList() { return provinceList; } @Override public void startDocument() throws SAXException { // When the first start tag is read, this method is triggered. } ProvinceModel provinceModel = new ProvinceModel(); CityModel cityModel = new CityModel(); DistrictModel districtModel = new DistrictModel(); /** * <province name="Anhui Province"> <city name="Anqing City"> <district name="Zongyang County" zipcode="246000" /> <district name="Daguan District" zipcode="246000" /> <district name="Huaning County" zipcode="246000" /> <district name="Qianshan County" zipcode="246000" /> <district name="Susong County" zipcode="246000" /> <district name="Taohu County" zipcode="246000" /> <district name="Tongcheng City" zipcode="246000" /> <district name="Wangjiang County" zipcode="246000" /> <district name="Yixiu District" zipcode="246000" /> <district name="Yingjiang District" zipcode="246000" /> <district name="Yuxi County" zipcode="246000" /> <district name="other" zipcode="246000" /> </city> */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // When encountering a start tag, call this method 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 { // When encountering the end tag, this method will be called 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 { } }
Next let's see the base class BaseActivity
package com.mrwujay.cascade.activity; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import android.app.Activity; import android.content.res.AssetManager; import com.mrwujay.cascade.model.CityModel; import com.mrwujay.cascade.model.DistrictModel; import com.mrwujay.cascade.model.ProvinceModel; import com.mrwujay.cascade.service.XmlParserHandler; public class BaseActivity extends Activity { /** * All provinces */ protected String[] mProvinceDatas; /** * Key - Province value - City */ protected Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>(); /** * Key - City values - District */ protected Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>(); /** * Key - District values - Postal code */ protected Map<String, String> mZipcodeDatasMap = new HashMap<String, String>(); /** * Current province name */ protected String mCurrentProviceName; /** * Current city name */ protected String mCurrentCityName; /** * Current district name */ protected String mCurrentDistrictName = ""; /** * Current district postal code */ protected String mCurrentZipCode = ""; /** * Parse the XML data of provinces, cities, and districts */ protected void initProvinceDatas() { //List of province collections List<ProvinceModel> provinceList = null; //Get resources AssetManager asset = getAssets(); try { //Get input stream InputStream input = asset.open("province_data.xml"); // Create a factory object for parsing xml SAXParserFactory spf = SAXParserFactory.newInstance(); // Parse xml SAXParser parser = spf.newSAXParser(); //Parsing tool XmlParserHandler handler = new XmlParserHandler(); //Perform parsing parser.parse(input, handler); input.close(); // Get the parsed data provinceList = handler.getDataList(); // */ Initialize the default selected province, city, and district if (provinceList != null && !provinceList.isEmpty()) { //Get the first province mCurrentProviceName = provinceList.get(0).getName(); List<CityModel> cityList = provinceList.get(0).getCityList(); if (cityList != null && !cityList.isEmpty()) { //Get the name of the first city of the first province mCurrentCityName = cityList.get(0).getName(); List<DistrictModel> districtList = cityList.get(0) .getDistrictList(); //Get the name of the first county of the first city of the first province mCurrentDistrictName = districtList.get(0).getName(); mCurrentZipCode = districtList.get(0).getZipcode(); } } // */ mProvinceDatas = new String[provinceList.size()]; for (int i = 0; i < provinceList.size(); i++) { // Traverse all province data 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++) { // Traverse all city data under the province 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++) { // Traverse all districts under the city/The data of the county DistrictModel districtModel = new DistrictModel( districtList.get(k).getName(), districtList .get(k).getZipcode()); // District/The postal code corresponding to the county, save to mZipcodeDatasMap mZipcodeDatasMap.put(districtList.get(k).getName(), districtList.get(k).getZipcode()); distrinctArray[k] = districtModel; distrinctNameArray[k] = districtModel.getName(); } // City-District/Save the data of the county to mDistrictDatasMap mDistrictDatasMap.put(cityNames[j], distrinctNameArray); } // Province-Save the data of the city to mCitisDatasMap mCitisDatasMap.put(provinceList.get(i).getName(), cityNames); } { catch (Throwable e) { e.printStackTrace(); } } } }
Main interface MainActivity
package com.mrwujay.cascade.activity; import com.mrwujay.cascade.R; import com.mrwujay.cascade.R.id; import com.mrwujay.cascade.R.layout; import kankan.wheel.widget.OnWheelChangedListener; import kankan.wheel.widget.WheelView; import kankan.wheel.widget.adapters.ArrayWheelAdapter; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends BaseActivity implements OnClickListener, OnWheelChangedListener { private WheelView mViewProvince; private WheelView mViewCity; private WheelView mViewDistrict; private Button mBtnConfirm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setUpViews(); setUpListener(); setUpData(); } private void setUpViews() { mViewProvince = (WheelView) findViewById(R.id.id_province); mViewCity = (WheelView) findViewById(R.id.id_city); mViewDistrict = (WheelView) findViewById(R.id.id_district); mBtnConfirm = (Button) findViewById(R.id.btn_confirm); } private void setUpListener() { // Add change event mViewProvince.addChangingListener(this); // Add change event mViewCity.addChangingListener(this); // Add change event mViewDistrict.addChangingListener(this); // Add onclick event mBtnConfirm.setOnClickListener(this); } private void setUpData() { initProvinceDatas(); mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(MainActivity.this, mProvinceDatas)); // Set the number of visible items mViewProvince.setVisibleItems(7); mViewCity.setVisibleItems(7); mViewDistrict.setVisibleItems(7); updateCities(); updateAreas(); } /** * Update the information of the city WheelView based on the current province */ private void updateCities() { //Get the item index value of the current province int pCurrent = mViewProvince.getCurrentItem(); //The current province name of the lake area mCurrentProviceName = mProvinceDatas[pCurrent]; //Get the array collection of cities under the current province String[] cities = mCitisDatasMap.get(mCurrentProviceName); if (cities == null) { cities = new String[] { "" }; } mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities)); mViewCity.setCurrentItem(0); updateAreas(); } /** * Update the information of the WheelView for areaWheelView based on the current city */ 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); } /** * Callback of the interface method implementation */ @Override public void onChanged(WheelView wheel, int oldValue, int newValue) { // TODO Auto-generated method stub if (wheel == mViewProvince) { updateCities(); } else if (wheel == mViewCity) { updateAreas(); } else if (wheel == mViewDistrict) { //Get the name of the county mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue]; //Get the county code mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_confirm: showSelectedResult(); break; default: break; } } private void showSelectedResult() { Toast.makeText(MainActivity.this, "Current selection:"+mCurrentProviceName+","+mCurrentCityName+"," +mCurrentDistrictName+","+mCurrentZipCode, Toast.LENGTH_SHORT).show(); } }
还有2个drawable
wheel_bg.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> </layer-list>
wheel_val.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:angle="90" android:centerColor="#70222222" android:endColor="#70EEEEEE" android:startColor="#70222222" /> <stroke android:width="20dp" android:color="#FF69B4" /> </shape>
That's all for this article. Hope it will be helpful to your study, and also hope everyone will support the Yelling Tutorial.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)