Use CityPicker.from(activity) to initialize the picker. You can configure hot cities using HotCity, set the currently located city using LocatedCity, and handle selection or location events via OnPickListener.
Note: To handle location updates, use CityPicker.getInstance().locateComplete(LocatedCity, LocateState) within your location callback.
List<HotCity> hotCities = new ArrayList<>();
hotCities.add(new HotCity("北京", "北京", "101010100"));
hotCities.add(new HotCity("上海", "上海", "101020100"));
CityPicker.from(activity)
.enableAnimation(true)
.setAnimationStyle(anim)
.setLocatedCity(new LocatedCity("杭州", "浙江", "101210101"))
.setHotCities(hotCities)
.setOnPickListener(new OnPickListener() {
@Override
public void onPick(int position, City data) {
// data contains name, code, etc.
Toast.makeText(getApplicationContext(), data.getName(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel(){
Toast.makeText(getApplicationContext(), "取消选择", Toast.LENGTH_SHORT).show();
}
@Override
public void onLocate() {
// Implement your own location logic here
// Once location is successful, update the picker:
CityPicker.getInstance()
.locateComplete(new LocatedCity("深圳", "广东", "101280601"), LocateState.SUCCESS);
}
})
.show();