用git上的wheelpicker实现,地址
https://github.com/AigeStudio/WheelPicker
效果预览
Paste_Image.pngPaste_Image.png Paste_Image.png
xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:id="@+id/txt_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完成"/>
</LinearLayout>
<com.aigestudio.wheelpicker.WheelPicker
android:id="@+id/wheel_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
<com.aigestudio.wheelpicker.widgets.WheelDatePicker
android:id="@+id/wheel_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>
<LinearLayout
android:id="@+id/ll_area"
android:layout_width="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="wrap_content">
<com.aigestudio.wheelpicker.WheelPicker
android:id="@+id/wheel_province"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
app:wheel_item_text_size="14sp"
android:layout_marginRight="5dp"/>
<com.aigestudio.wheelpicker.WheelPicker
android:id="@+id/wheel_city"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
app:wheel_item_text_size="14sp"
android:layout_marginRight="5dp"/>
<com.aigestudio.wheelpicker.WheelPicker
android:id="@+id/wheel_area"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
app:wheel_item_text_size="14sp"
android:layout_marginRight="5dp"/>
</LinearLayout>
</LinearLayout>
java代码
import android.app.DialogFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.aigestudio.wheelpicker.WheelPicker;
import com.aigestudio.wheelpicker.widgets.WheelDatePicker;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedHashTreeMap;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import tuqu.no2.ddsd.R;
import tuqu.no2.ddsd.base.TqAppClient;
import tuqu.no2.ddsd.base.TqResultResponse;
import tuqu.no2.ddsd.model.City;
import tuqu.no2.ddsd.model.NullModel;
import tuqu.no2.ddsd.model.TqArea;
import tuqu.no2.ddsd.theme.colorUi.util.SharedPreferencesMgr;
import tuqu.no2.ddsd.ui.activity.UserInfoActivity;
import tuqu.no2.ddsd.utils.Const;
import tuqu.no2.ddsd.utils.ConstanceValue;
import tuqu.no2.ddsd.utils.FileUtils;
import tuqu.no2.ddsd.utils.RequestUtil;
import tuqu.no2.ddsd.utils.ToastUtils;
/**
* Created by WangPC on 2017/6/4 0004.
*/
public class PickDialogFragment extends DialogFragment {
public final static int SEXMODE=0x001;
public final static int DATEMODE=0x002;
public final static int AREAMODE=0x003;
public int mode=SEXMODE;
Unbinder unbinder;
String value;
UserInfoActivity userInfoActivity;
List<TqArea> provinces;
public void setMode(int pMode){
this.mode=pMode;
}
public String getValue(){
return value;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.wheelpicker, container);
FrameLayout.LayoutParams flParams = new FrameLayout.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
// flParams.gravity = Gravity.CENTER;
view.setLayoutParams(flParams);
unbinder = ButterKnife.bind(this, view);
userInfoActivity=(UserInfoActivity)getActivity();
TextView txtCancel=(TextView)view.findViewById(R.id.txt_cancel);
TextView txtConfirm=(TextView)view.findViewById(R.id.txt_confirm);
final WheelPicker picker=(WheelPicker)view.findViewById(R.id.wheel_sex);
final WheelDatePicker pickerDate=(WheelDatePicker)view.findViewById(R.id.wheel_date);
final LinearLayout llArea=(LinearLayout)view.findViewById(R.id.ll_area);
final WheelPicker pickerProvince=(WheelPicker)view.findViewById(R.id.wheel_province);
final WheelPicker pickerCity=(WheelPicker)view.findViewById(R.id.wheel_city);
final WheelPicker pickerArea=(WheelPicker)view.findViewById(R.id.wheel_area);
txtCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PickDialogFragment.this.dismiss();
}
});
txtConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (mode){
case SEXMODE:
value=picker.getCurrentItemPosition()+"";
userInfoActivity.setSex(value);
PickDialogFragment.this.dismiss();
break;
case DATEMODE:
value=pickerDate.getSelectedYear()+"-"+pickerDate.getSelectedMonth()+"-"+pickerDate.getSelectedDay()+"";
userInfoActivity.setTxtBirthday(value);
PickDialogFragment.this.dismiss();
break;
case AREAMODE:
TqArea province =provinces.get(pickerProvince.getCurrentItemPosition());
City city=province.getCity().get(pickerCity.getCurrentItemPosition());
value=province.getName()+" "+city.getName()+" "+city.getArea().get(pickerArea.getCurrentItemPosition());
userInfoActivity.setArea(value);
PickDialogFragment.this.dismiss();
break;
}
}
});
if (mode==SEXMODE) {
pickerDate.setVisibility(View.GONE);
llArea.setVisibility(View.GONE);
picker.setMaximumWidthText("2012阿尼12月12日。。");
picker.setIndicator(true);
picker.setIndicatorColor(getResources().getColor(R.color.gray));
List<String> data = new ArrayList<>();
data.add("女");
data.add("男");
picker.setData(data);
picker.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
@Override
public void onItemSelected(WheelPicker picker, Object data, int position) {
}
});
}
if (mode==DATEMODE) {
picker.setVisibility(View.GONE);
llArea.setVisibility(View.GONE);
pickerDate.setMinimumWidth(500);
pickerDate.setCyclic(true);
pickerDate.setAtmospheric(true);
pickerDate.setIndicator(true);
pickerDate.setIndicatorColor(getResources().getColor(R.color.gray));
}
if (mode==AREAMODE){
picker.setVisibility(View.GONE);
pickerDate.setVisibility(View.GONE);
pickerProvince.setIndicator(true);
pickerProvince.setIndicatorColor(getResources().getColor(R.color.gray));
pickerCity.setIndicator(true);
pickerCity.setIndicatorColor(getResources().getColor(R.color.gray));
pickerArea.setIndicator(true);
pickerArea.setIndicatorColor(getResources().getColor(R.color.gray));
pickerProvince.setMaximumWidthText("黑龙剑圣");
pickerCity.setMaximumWidthText("齐齐哈尔是是是");
pickerArea.setMaximumWidthText("东南西北区");
String jsonstr= FileUtils.getJson( this.getActivity(),"city2.txt");
JSONArray jsonArr= JSONArray.parseArray(jsonstr);
Gson gson = new Gson();
provinces=new ArrayList<>();
List<String > provinceNames=new ArrayList<>();
for (Object obj:jsonArr) {
TqArea tqArea=gson.fromJson(JSON.toJSONString(obj), TqArea.class);
provinces.add(tqArea);
provinceNames.add(tqArea.getName());
}
pickerProvince.setData(provinceNames);
List<String> cities=new ArrayList<String>();
for (City city:provinces.get(0).getCity()) {
cities.add(city.getName());
}
pickerCity.setData(cities);
pickerArea.setData(provinces.get(0).getCity().get(0).getArea());
pickerProvince.setOnWheelChangeListener(new WheelPicker.OnWheelChangeListener() {
@Override
public void onWheelScrolled(int offset) {
}
@Override
public void onWheelSelected(int position) {
List<String> cities=new ArrayList<String>();
for (City city:provinces.get(position).getCity()) {
cities.add(city.getName());
}
pickerCity.setData(cities);
pickerCity.setSelectedItemPosition(0);
pickerArea.setData(provinces.get(position).getCity().get(0).getArea());
}
@Override
public void onWheelScrollStateChanged(int state) {
}
});
pickerCity.setOnWheelChangeListener(new WheelPicker.OnWheelChangeListener() {
@Override
public void onWheelScrolled(int offset) {
}
@Override
public void onWheelSelected(int position) {
List<City> cities=provinces.get(pickerProvince.getCurrentItemPosition()).getCity();
pickerArea.setData( cities.get(position).getArea());
}
@Override
public void onWheelScrollStateChanged(int state) {
}
});
}
/* pickerDate.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
@Override
public void onItemSelected(WheelPicker picker, Object data, int position) {
ToastUtils.showToast(data.toString());
}
});*/
return view;
}
public void areaClick(){
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}
使用
public void showPick(int mode) {
PickDialogFragment pickDialog = new PickDialogFragment();
pickDialog.setMode(mode);
pickDialog.show(getFragmentManager(), "changeSex");
}
其他
地区数据请看这里:全国地区json数据
网友评论