简书 php三级联动,城市选择的三级联动

先看两张效果图:

ac523441b64d

效果图-1

ac523441b64d

效果图-2

省市县这些玩意儿一般不会变的,所以借力竭力说服了领导,将地址的json文件放在本地,不用每次都请求,json结构是这样的:

ac523441b64d

json结构

用到的三方库:

compile 'com.github.open-android:WheelPicker:v1.0.0'

自定义PopWindow

public class AddPopWindow extends PopupWindow {

private View conentView;

protected Contextm Context;

/**

*初始化一个PopupWindow

*

*@paramcontext上下文对象

*@paramresId自定义的布局文件

*/

publi cAddPopWindow(final Activity context, intresId) {

mContext= context;

/**

*加载自定义布局

*/

//初始化布局加载器

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

conentView= inflater.inflate(resId, null);

DisplayMetrics metrics =newDisplayMetrics();

WindowManager manager = context.getWindowManager();

manager.getDefaultDisplay().getMetrics(metrics);

//获取屏幕的宽和高

inth = metrics.heightPixels- com.lzp.statusbar.utils.SystemUtils.getStatusHeight(context);

intw = metrics.widthPixels;

//设置SelectPicPopupWindow的View

this.setContentView(conentView);

//设置SelectPicPopupWindow弹出窗体的宽

this.setWidth(w);

//设置SelectPicPopupWindow弹出窗体的高

//this.setHeight(h);

this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

//设置SelectPicPopupWindow弹出窗体可点击

this.setFocusable(true);

this.setOutsideTouchable(true);

//软盘弹出时,显示的效果

setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);

setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

//刷新状态

this.update();

//实例化一个ColorDrawable颜色为透明

ColorDrawable dw =newColorDrawable(0x00000000);

//点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener,设置其他控件变化等操作

this.setBackgroundDrawable(dw);

setAnimationStyle(R.style.PopWindow_y_anim_style);

//this.setAnimationStyle(android.R.style.Animation_Translucent);

//设置SelectPicPopupWindow弹出窗体动画效果

//this.setAnimationStyle(R.style.AnimationPreview);

}

/**

*获取自定义到PopupWindow中的布局

*

*@return

*/

publicViewgetWindowRootView() {

returnconentView;

}

/**

*显示popupWindow

*必须调用这个方法才能显示出来PopupWindow

*/

public voidshowPopupWindow(View parent) {

if(!this.isShowing()) {

//以下拉方式显示popupwindow

this.showAtLocation(parent,Gravity.BOTTOM,0,0);

}else{

this.dismiss();

}

}

/**

*关闭PopupWindow

*/

public voidclosePopupWindow() {

if(isShowing())

dismiss();

}

}

地址选择的PopupWindow:

public classAddressSelectPopWindowextendsAddPopWindow {

privateWheelCurvedPickermWheelProvince;

privateWheelCurvedPickermWheelCity;

privateWheelCurvedPickermWheelCounty;

privateImageViewmIvClose;

privateTextViewmTvConfig;

privateListmAddressData;

privateListmProvinceData;

privateListmCityData;

privateListmCountryData;

privateWheelChangeListenermChangeListener;

private intmProvincePosition;

private intmCityPosition;

private intmCountryPosition;

/**

*初始化一个PopupWindow

*

*@paramcontext上下文对象

*/

publicAddressSelectPopWindow(Activity context) {

super(context,R.layout.layout_pop_select_address);

SelectAddressPopBinding binding = SelectAddressPopBinding.bind(getWindowRootView());

mWheelProvince= binding.wheelProvince;

mWheelCity= binding.wheelCity;

mWheelCounty=binding.wheelCounty;

mIvClose= binding.ivClose;

mTvConfig= binding.tvConfig;

initData(context);

init();

}

private voidinitData(Context context) {

try{

mAddressData= JsonFormFileUtils.getJsonArray(context,"address.json",AddressBean.class);

}catch(IOException e) {

e.printStackTrace();

}

}

public voidsetWheelChangeListener(WheelChangeListener listener) {

mChangeListener= listener;

}

private voidinit() {

/*设置显示字体的小*/

mWheelProvince.setTextSize(18);

mWheelCity.setTextSize(18);

mWheelCounty.setTextSize(18);

mWheelProvince.setMinimumHeight(40);

mWheelCity.setMinimumHeight(40);

mWheelCounty.setMinimumHeight(40);

mWheelProvince.setItemCount(10);

mWheelCity.setItemCount(10);

mWheelCounty.setItemCount(10);

mProvinceData= getAddressProvinceList(mAddressData);

mWheelProvince.setData(mProvinceData);

mWheelProvince.setOnWheelChangeListener(newSimpleWheelChangeListener() {

@Override

public voidonWheelSelected(intindex,String data) {

mProvincePosition= index;

mCityData= getAddressCityList(mAddressData,mProvincePosition);

mWheelCity.setData(mCityData);

mWheelCity.setItemIndex(0);

mCityPosition=0;

mCountryData=getAddressCountry(mAddressData.get(mProvincePosition).getSub(),mCityPosition);

mWheelCounty.setData(mCountryData);

mWheelCounty.setItemIndex(0);

mCountryPosition=0;

}

});

mWheelCity.setOnWheelChangeListener(newSimpleWheelChangeListener() {

@Override

public voidonWheelSelected(intindex,String data) {

mCityPosition= index;

mCountryData=getAddressCountry(mAddressData.get(mProvincePosition).getSub(),mCityPosition);

mWheelCounty.setData(mCountryData);

mWheelCounty.setItemIndex(0);

mCountryPosition=0;

}

});

mWheelCounty.setOnWheelChangeListener(newSimpleWheelChangeListener(){

@Override

public voidonWheelSelected(intindex,String data) {

mCountryPosition=index;

}

});

mIvClose.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View v) {

dismiss();

}

});

mTvConfig.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View v) {

dismiss();

if(mChangeListener!=null) {

String provinceNo =mAddressData.get(mProvincePosition).getId();

String cityNo =mAddressData.get(mProvincePosition).getSub().get(mCityPosition).getId();

String countyNo=mAddressData.get(mProvincePosition).getSub().get(mCityPosition).getSub2().get(mCountryPosition).getId();

mChangeListener.onWheelSelected(provinceNo,cityNo,countyNo,mProvinceData.get(mProvincePosition)

,mCityData.get(mCityPosition),mCountryData.get(mCountryPosition));

}

}

});

}

public voidaddressReset() {

mWheelProvince.setItemIndex(0);

mProvincePosition=0;

mCityData= getAddressCityList(mAddressData,mProvincePosition);

mWheelCity.setData(mCityData);

mCityPosition=0;

mWheelCity.setItemIndex(0);

mCountryData=getAddressCountry(mAddressData.get(mProvincePosition).getSub(),mCityPosition);

mWheelCounty.setData(mCountryData);

mCountryPosition=0;

mWheelCounty.setItemIndex(0);

}

privateListgetAddressProvinceList(List addressList) {

List provinceList =newArrayList<>();

for(AddressBean addressLinkageBean : addressList) {

provinceList.add(addressLinkageBean.getName());

}

returnprovinceList;

}

privateListgetAddressCityList(List addressList, intprovincePosition) {

List cityList =newArrayList<>();

List cityBeanList = addressList.get(provincePosition).getSub();

for(AddressBean.SubBean cityBean : cityBeanList) {

cityList.add(cityBean.getName());

}

returncityList;

}

privateListgetAddressCountry(List addressList,intcityPosition){

List countryList =newArrayList<>();

List cubBeen = addressList.get(cityPosition).getSub2();

for(AddressBean.SubBean.Sub2Bean cityBean : cubBeen) {

countryList.add(cityBean.getName());

}

returncountryList;

}

public interfaceWheelChangeListener {

/**

*@paramprovinceNo省id

*@paramcityNo市id

*@paramcountyNo县id

*@paramprovinceData省名称

*@paramcityData市名称

*@paramcountyData县名称

*/

voidonWheelSelected(String provinceNo,String cityNo,String countyNo

,String provinceData,String cityData,String countyData);

}

}

用法:

mPopWindow=newAddressSelectPopWindow(AddressActivity.this);

mDataBinding.common.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View v) {

mPopWindow.addressReset();

mPopWindow.showPopupWindow(mDataBinding.getRoot());

}

});

mPopWindow.setWheelChangeListener(newAddressSelectPopWindow.WheelChangeListener() {

@Override

public voidonWheelSelected(String provinceNo,String cityNo,String countyNo,String provinceData,String cityData,String countyData) {

mDataBinding.common.setText(provinceData + cityData+countyData);

address=provinceData + cityData+countyData;

mProvenceId=provinceNo;

mCityId=cityNo;

mCountryId=countyNo;

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值