省市區(qū)三級聯動
下載地址:https://code.google.com/p/android-wheel/自備梯子
效果圖
如果想更改樣式需要更改源碼
單級滾動
把下載好的做為module導入項目挣饥,并添加依賴
這樣就可以像使用普通控件一樣使用了
<kankan.wheel.widget.WheelView
android:id="@+id/wheelviewTimer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Activty
mWheelViewTimer = (WheelView) findViewById(R.id.wheelviewTimer);
for(int i = 0; i <24 ; i++) {
mDatas.add(i+"");
}
//設置可以見的條目數量亮瓷, 上中下
mWheelViewTimer.setVisibleItems(5);
//設置適配器
mTimerAdapter = new TimerAdapter(this,mDatas);
mWheelViewTimer.setViewAdapter(mTimerAdapter);
//放在最后面 設置當前顯示的
mWheelViewTimer.setCurrentItem(5);
適配器
感覺和ListView有沒有一點像呢,都是要設置適配器
public class TimerAdapter extends AbstractWheelTextAdapter {
List<String> mDatas;
Context mContext;
public TimerAdapter(Context context, List<String> datas) {
super(context, R.layout.item_time, NO_RESOURCE);
mContext = context;
mDatas = datas;
setItemTextResource(R.id.time);
}
@Override
protected CharSequence getItemText(int index) {
return mDatas.get(index);
}
@Override
public int getItemsCount() {
return mDatas.size();
}
@Override
public View getItem(int index, View convertView, ViewGroup parent) {
View view = super.getItem(index, convertView, parent);
return view;
}
這樣就完成了,一個滾動
三級聯動
看了一級的,三級的說白了就是3個這個控件
布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<kankan.wheel.widget.WheelView
android:id="@+id/province"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<kankan.wheel.widget.WheelView
android:id="@+id/city"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<kankan.wheel.widget.WheelView
android:id="@+id/area"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
適配器和上面一樣
public class AddressTextAdapter extends AbstractWheelTextAdapter {
List<String> mDatas;
Context mContext;
public AddressTextAdapter(Context context, List<String> datas) {
super(context, R.layout.item_address,NO_RESOURCE);
mContext = context;
mDatas=datas;
//設置資源要顯示在那個控件上
setItemTextResource(R.id.tempValue);
}
@Override
protected CharSequence getItemText(int index) {
return mDatas.get(index);
}
@Override
public int getItemsCount() {
return mDatas.size();
}
@Override
public View getItem(int index, View convertView, ViewGroup parent) {
View view = super.getItem(index,convertView,parent);
return view;
}
public String getName(int index){
return mDatas.get(index);
}
Activity初始化數據
- 找到3個控件
mProvince = (WheelView) findViewById(R.id.province);
mCity = (WheelView) findViewById(city);
mArea = (WheelView) findViewById(R.id.area);
- 初始化數據
//讀取文件
String str = FileUtils.readAssetsFile(this, "china-city-area-zip.min.json");
Log.d("vivi", "initData: " + str);
//轉化為對象集合
List<RegionJson> datas = JSON.parseArray(str, RegionJson.class);
這里的數據放在Assets目錄下的json文件泼菌,讀取文件,解析就OK,數據在最下面可以找到
- 分類數據
for(RegionJson data : datas) {
//省
mProvinceList.add(data.name);
List<String> mCitysList = new ArrayList<>();
for(RegionJson.ChildEntity city : data.child) {
//市
mCitysList.add(city.name);
List<String> mAreaList = new ArrayList<>();
for(RegionJson.ChildEntity.ChildEntity2 area : city.child) {
//區(qū)
mAreaList.add(area.name);
}
//市-區(qū)對該
mAreaDatasMap.put(city.name,mAreaList);
}
//省支市對應
mCitisDatasMap.put(data.name,mCitysList);
}
- 為三個控件設置適配器
/**
* 省
*/
mProvince.setVisibleItems(5);
mProviceAdapter = new AddressTextAdapter(this, mProvinceList);
mProvince.setViewAdapter(mProviceAdapter);
mProvince.setCurrentItem(0);
/**
* 市
*/
mCity.setVisibleItems(5);
mCityAdapter = new AddressTextAdapter(this, mCitisDatasMap.get(mProvinceList.get(0)));
mCity.setViewAdapter(mCityAdapter);
mCity.setCurrentItem(0);
/**
* 地區(qū)
*/
mArea.setVisibleItems(5);
mAreaAdapter = new AddressTextAdapter(this, mAreaDatasMap.get(mCitisDatasMap.get(mProvinceList.get(0)).get(0)));
mArea.setViewAdapter(mAreaAdapter);
mArea.setCurrentItem(0);
設置監(jiān)聽滾動完成里切換
private void initScrllListener() {
/**
* 省滾動完成時切換 城市與地區(qū)
*/
mProvince.addScrollingListener(new OnWheelScrollListener() {
@Override
public void onScrollingStarted(WheelView wheel) {
//標記位-開始滾動
proviceScrolling =true;
}
@Override
public void onScrollingFinished(WheelView wheel) {
proviceScrolling =false;
//省滾動完成后切換市
updateCities(mCity,mCitisDatasMap.get(mProviceAdapter.getName(mProvince.getCurrentItem())));
//更新地區(qū)
updateArea(mArea,mAreaDatasMap.get(mCityAdapter.getName(mCity.getCurrentItem())));
}
});
/**
* 市滾動 完成時切換地區(qū)
*/
mCity.addScrollingListener(new OnWheelScrollListener() {
@Override
public void onScrollingStarted(WheelView wheel) {
//標記位-開始滾動
cityScrolling=true;
}
@Override
public void onScrollingFinished(WheelView wheel) {
cityScrolling =false;
//市滾動完成后切換
updateArea(mArea,mAreaDatasMap.get(mCityAdapter.getName(mCity.getCurrentItem())));
}
});
}
這樣就基本完成了
完善滾動中時也可以切換
/**
* 省滾動中切換市
*/
mProvince.addChangingListener(new OnWheelChangedListener() {
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
//省在滾動
if(proviceScrolling){
//更新城市
updateCities(mCity,mCitisDatasMap.get(mProviceAdapter.getName(newValue)));
//更新地區(qū)
updateArea(mArea,mAreaDatasMap.get(mCityAdapter.getName(mCity.getCurrentItem())));
}
}
});
mCity.addChangingListener(new OnWheelChangedListener() {
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
if(cityScrolling){
updateArea(mArea,mAreaDatasMap.get(mCityAdapter.getName(newValue)));
}
}
});
更新數據的2個方法
/**
* 切換城市
*/
private void updateCities(WheelView city, List<String> cities) {
mCityAdapter = new AddressTextAdapter(this,cities);
city.setViewAdapter(mCityAdapter);
city.setCurrentItem(0);
}
----------
private void updateArea(WheelView area,List<String> areas){
mAreaAdapter= new AddressTextAdapter(this,areas);
area.setViewAdapter(mAreaAdapter);
area.setCurrentItem(0);
}