android上做滚动选取的效果是没有原生的控件,我做这个滚动选取的小过失用的是大神整理好的插件,如图是插件的相关的类
轮子有了做效果是比较容易的,无非就是设置数据和显示,为了自己项目中用着方便,我写了一个自定义dialog来显示,并且把数据机械的方法也都放在里边了。
MyprovinceSelectdialog.class
/**
* 自定义省市区选取控件
*/
public class MyprovinceSelectdialog extends Dialog {
Context mContext;
public MyprovinceSelectdialog(Context context) {
super(context);
mContext = context;
}
public MyprovinceSelectdialog(Context context, int themeResId) {
super(context, themeResId);
}
public static class Builder implements OnWheelChangedListener {
ArrayList data;
private Context context;
private Handler mHandler;
WheelView provice_select;
WheelView city_select;
WheelView district_select;
public Builder(Context context, Handler mHandler) {
this.context = context;
this.mHandler = mHandler;
}
public MyprovinceSelectdialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final MyprovinceSelectdialog dialog = new MyprovinceSelectdialog(context, R.style.dialog);
View layout = inflater.inflate(R.layout.shengshiqu, null);
//初始化布局
provice_select = (WheelView) layout.findViewById(R.id.province_select);
city_select = (WheelView) layout.findViewById(R.id.city_select);
district_select = (WheelView) layout.findViewById(R.id.district_select);
Button shengshiqu_btn_cancel = (Button) layout.findViewById(R.id.shengshiqu_btn_cancel);
Button shengshiqu_btn_sure = (Button) layout.findViewById(R.id.shengshiqu_btn_sure);
initData();
if (null != data) {
//province
String[] item = new String[data.size()];
for (int i = 0; i < data.size(); i++) {
item[i] = data.get(i).getPname();
}
ArrayWheelAdapter adapter = new ArrayWheelAdapter<>(context, item);
adapter.setTextColor(R.color.black);
provice_select.setViewAdapter(adapter);
// 设置可见条目数量
provice_select.setVisibleItems(5);
city_select.setVisibleItems(5);
district_select.setVisibleItems(5);
provice_select.addChangingListener(this);
city_select.addChangingListener(this);
district_select.addChangingListener(this);
updateCities();
updateAreas();
}
shengshiqu_btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
shengshiqu_btn_sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("provinceId", data.get(provice_select.getCurrentItem()).getPid());
bundle.putString("cityId", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getCid());
bundle.putString("districtId",
data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().size() > district_select.getCurrentItem() ?
data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().get(district_select.getCurrentItem()).getDid() :
"");
bundle.putString("province", data.get(provice_select.getCurrentItem()).getPname());
bundle.putString("city", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getCname());
bundle.putString("district", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().size() > district_select.getCurrentItem() ?
data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().get(district_select.getCurrentItem()).getDname() :
"");
message.setData(bundle);
mHandler.sendMessage(message);
dialog.cancel();
}
});
dialog.setContentView(layout);
return dialog;
}
private void updateAreas() {
int cCurrent = city_select.getCurrentItem();
int pCurrent = provice_select.getCurrentItem();
String[] item = new String[data.get(pCurrent).getMyCities().get(cCurrent).getMyDistricts().size()];
for (int i = 0; i < item.length; i++) {
item[i] = data.get(pCurrent).getMyCities().get(cCurrent).getMyDistricts().get(i).getDname();
}
ArrayWheelAdapter adapter = new ArrayWheelAdapter<>(context, item);
adapter.setTextColor(R.color.black_true);
district_select.setViewAdapter(adapter);
district_select.setCurrentItem(0);
}
private void updateCities() {
int pCurrent = provice_select.getCurrentItem();
String[] item = new String[data.get(pCurrent).getMyCities().size()];
for (int i = 0; i < item.length; i++) {
item[i] = data.get(pCurrent).getMyCities().get(i).getCname();
}
ArrayWheelAdapter adapter = new ArrayWheelAdapter<>(context, item);
adapter.setTextColor(R.color.black);
city_select.setViewAdapter(adapter);
city_select.setCurrentItem(0);
updateAreas();
}
//初始化省市区信息
public void initData() {
String json = MyUtils.readLocalJson(context, "jsons.txt");
JSONArray jsonArray;
try {
jsonArray = new JSONArray(json);
//初始化省列表
data = new ArrayList();
for (int y = 0; y < jsonArray.length(); y++) {
Myprovice myprovice = new Myprovice();
myprovice.setPid(jsonArray.getJSONObject(y).getString("provinceId"));
myprovice.setPname(jsonArray.getJSONObject(y).getString("provinceName"));
JSONArray jsonArray1 = jsonArray.getJSONObject(y).getJSONArray("citys");
List myCities = new ArrayList<>();
for (int j = 0; j < jsonArray1.length(); j++) {
MyCity myCity = new MyCity();
myCity.setCid(jsonArray1.getJSONObject(j).getString("cityId"));
myCity.setCname(jsonArray1.getJSONObject(j).getString("cityName"));
if (jsonArray1.getJSONObject(j).has("countys")) {
JSONArray jsonArray2 = jsonArray1.getJSONObject(j).getJSONArray("countys");
List myDistricts = new ArrayList<>();
for (int x = 0; x < jsonArray2.length(); x++) {
Districts districts = new Districts();
districts.setDid(jsonArray2.getJSONObject(x).getString("countyId"));
districts.setDname(jsonArray2.getJSONObject(x).getString("countyName"));
myDistricts.add(districts);
}
myCity.setMyDistricts(myDistricts);
} else {
List myDistricts = new ArrayList<>();
myCity.setMyDistricts(myDistricts);
}
myCities.add(myCity);
}
myprovice.setMyCities(myCities);
data.add(myprovice);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
if (wheel == provice_select) {
updateCities();
} else if (wheel == city_select) {
updateAreas();
} else if (wheel == district_select) {
}
}
}
}
在initData方法中作了数据的解析,首先要在项目的assets目录下放一个json.txt的文件,里边放的是JsonArray 格式的省市区信息,然后分别创建对应的类用来解析数据,因为在真正用到wheelview的地方其实你只需要适配器一个String[]的数据源就行,所以解析的话无关紧要,只要能做出来数据就行。主要的地方就是一个是wheelview的初始化
provice_select = (WheelView) layout.findViewById(R.id.province_select)
ArrayWheelAdapter adapter = new ArrayWheelAdapter<>(context, item);
//adapter.setTextColor(R.color.black);
provice_select.setViewAdapter(adapter);
// 设置可见条目数量
provice_select.setVisibleItems(5);
provice_select.addChangingListener(this);
builder实现了 OnWheelChangedListener 接口,调用onchange方法就可以监听滚动了,最开始的时候吧省市区三个wheelview都默认初始化第一项,当监听到省改变的时侯重新构造市的适配器,然后监听到市的选项改变的
时候重新构造区的适配器。
为了方便开发,我传进来一个handler用来直接将处理结果发送给调用额页面,这里没有给外部设置布局和点击事件的接口,之间在内部设置好了确认的点击事件,直接利用handler发送消息给调用处
shengshiqu_btn_sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("provinceId", data.get(provice_select.getCurrentItem()).getPid());
bundle.putString("cityId", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getCid());
bundle.putString("districtId",
data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().size() > district_select.getCurrentItem() ?
data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().get(district_select.getCurrentItem()).getDid() :
"");
bundle.putString("province", data.get(provice_select.getCurrentItem()).getPname());
bundle.putString("city", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getCname());
bundle.putString("district", data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().size() > district_select.getCurrentItem() ?
data.get(provice_select.getCurrentItem()).getMyCities().get(city_select.getCurrentItem()).getMyDistricts().get(district_select.getCurrentItem()).getDname() :
"");
message.setData(bundle);
mHandler.sendMessage(message);
dialog.cancel();
}
});
使用的方法就行在调用的地方
MyprovinceSelectdialog.Builder builder = new MyprovinceSelectdialog.Builder(AddAddressActivity.this,new Handler(){
@Override
public void handleMessage(Message msg) {
addAddressEditDirtect.setText(bundle.get("province").toString()+"-"+bundle.get("city").toString()+"-"+bundle.get("district").toString());
}
});
MyprovinceSelectdialog dialog = builder.create();
WindowManager wm = getWindowManager();
//让dialog显示在底部,宽度充满屏幕
int width = wm.getDefaultDisplay().getWidth();
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
window.setLayout(width, AutoGridLayout.LayoutParams.WRAP_CONTENT);
dialog.show();
地址http://download.csdn.net/detail/z690798364/9635945
欢迎建议和帮助改进。