package com.example.pickerviewlibrary.picker;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.content.res.ColorStateList;
|
import android.graphics.Color;
|
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.GradientDrawable;
|
import android.text.TextUtils;
|
import android.util.DisplayMetrics;
|
import android.view.Gravity;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.view.WindowManager;
|
import android.widget.AdapterView;
|
import android.widget.FrameLayout;
|
import android.widget.LinearLayout;
|
import android.widget.ListView;
|
import android.widget.PopupWindow;
|
import android.widget.RadioButton;
|
import android.widget.RadioGroup;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.example.pickerviewlibrary.R;
|
import com.example.pickerviewlibrary.picker.adapter.DataAdapter;
|
import com.example.pickerviewlibrary.picker.entity.AddressBean;
|
import com.example.pickerviewlibrary.picker.entity.PickerData;
|
import com.example.pickerviewlibrary.picker.listener.OnPickerClickListener;
|
import com.example.pickerviewlibrary.picker.util.AddressSqLite_DB_Utile;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class TeaPickerView extends PopupWindow implements View.OnClickListener {
|
private FrameLayout contentLayout;
|
private RadioButton mTextFirst, mTextSecond, mTextThird, mTextFourth;
|
private RadioGroup groupSelect;
|
private View line;
|
private ListView pickerList;
|
private TextView pickerTitleName;
|
private TextView pickerConfirm;
|
private View view;
|
private int index = 1;
|
private List<AddressBean> currData;
|
private DataAdapter adapter;
|
private Activity context;
|
private OnPickerClickListener listener;
|
private PickerData pickerData;
|
|
private int height;
|
private int radius = 0;
|
private int backgroundColor = Color.parseColor("#ffffff");
|
public static int dataSize = 15;
|
public static int dataColor;
|
public static int dataHeight;
|
private float alpha = 0.5f;
|
public static boolean contentLine = false;
|
public static Drawable contentLineDrawable;
|
public static boolean scrollBal = false;
|
public static boolean discolour = true;
|
public static int discolourColor;
|
public static boolean discolourHook = false;
|
public static Drawable customHook;
|
|
public TeaPickerView(Activity context) {
|
super(context);
|
this.context = context;
|
// this.pickerData = pickerData;
|
|
pickerData = new PickerData();
|
AddressBean addressBean = new AddressBean();
|
addressBean.setName("请选择地址");
|
pickerData.setInitSelectText(addressBean);
|
initView();
|
}
|
|
/**
|
* 初始化地址数据
|
*/
|
public void initAddressData() {
|
List<AddressBean> first = AddressSqLite_DB_Utile.getInit(context).addressDao().findByPcode("41");
|
pickerData.setFirstDatas(first);
|
}
|
|
|
//显示具体的高度(dp),设置0是自适应
|
public TeaPickerView setHeights(int mHeight) {
|
if (mHeight > 0) {
|
height = getPixelsFromDp(mHeight);
|
}
|
return this;
|
}
|
|
//显示的高度占屏幕的百分比,高度没有默认值,需要主动设置
|
public TeaPickerView setScreenH(int num) {
|
if (num > 0) {
|
height = getScreenH(context) / num;
|
}
|
return this;
|
}
|
|
//设置整体的背景颜色 默认是#ffffff
|
public TeaPickerView setBackground(int color) {
|
backgroundColor = color;
|
return this;
|
}
|
|
//设置圆角,默认0
|
public TeaPickerView setRadius(int mRadius) {
|
this.radius = mRadius;
|
return this;
|
}
|
|
//内容栏的背景颜色 默认是#ffffff
|
public TeaPickerView setContentBackground(int color) {
|
GradientDrawable drawable = new GradientDrawable();
|
//设置圆角大小
|
drawable.setCornerRadii(new float[]{radius, radius, radius, radius, 0, 0, 0, 0});
|
//设置shape背景色
|
drawable.setColor(color);
|
contentLayout.setBackground(drawable);
|
return this;
|
}
|
|
//内容栏的高度(dp) 默认是50dp
|
public TeaPickerView setContentHeight(int mHeight) {
|
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) groupSelect.getLayoutParams();
|
params.height = getPixelsFromDp(mHeight);
|
groupSelect.setLayoutParams(params);
|
return this;
|
}
|
|
//内容栏字体的大小和颜色, 默认是16sp,#0aa666,此方法不会变换颜色
|
public TeaPickerView setContentText(int size, int color) {
|
mTextFirst.setTextSize(size);
|
mTextFirst.setTextColor(color);
|
mTextSecond.setTextSize(size);
|
mTextSecond.setTextColor(color);
|
mTextThird.setTextSize(size);
|
mTextThird.setTextColor(color);
|
mTextFourth.setTextSize(size);
|
mTextFourth.setTextColor(color);
|
return this;
|
}
|
|
//自定义内容栏字体颜色变换器 在res目录下创建color文件夹用selector 默认颜色#555 选中颜色#0aa666
|
public TeaPickerView setContentText(ColorStateList colorStateList) {
|
mTextFirst.setTextColor(colorStateList);
|
mTextSecond.setTextColor(colorStateList);
|
mTextThird.setTextColor(colorStateList);
|
mTextFourth.setTextColor(colorStateList);
|
return this;
|
}
|
|
//内容栏选中是否有下划线 默认不开启
|
public TeaPickerView setContentLine(boolean bl) {
|
contentLine = bl;
|
return this;
|
}
|
|
//自定义内容栏下划线用layer-list 默认是下边框描边 颜色#0fbc72 高度1dp
|
public TeaPickerView setContentLineColor(Drawable drawable) {
|
contentLineDrawable = drawable;
|
return this;
|
}
|
|
//分割线的高度和颜色 默认是0.5dp #e5e5e5
|
public TeaPickerView setLine(int mHeight, int color) {
|
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) line.getLayoutParams();
|
params.height = getPixelsFromDp(mHeight);
|
line.setLayoutParams(params);
|
line.setBackgroundColor(color);
|
return this;
|
}
|
|
//设置list的item的高度(dp) 默认是40dp
|
public TeaPickerView setitemHeight(int mHeight) {
|
dataHeight = getPixelsFromDp(mHeight);
|
return this;
|
}
|
|
//设置list的字体大小和颜色 默认是15 #555
|
public TeaPickerView setListText(int size, int color) {
|
dataSize = size;
|
dataColor = color;
|
return this;
|
}
|
|
//设置list是否显示滚动条,默认false
|
public TeaPickerView setScrollBal(boolean bl) {
|
scrollBal = bl;
|
return this;
|
}
|
|
//设置阴影层的透明度 默认是0.5f
|
public TeaPickerView setAlpha(float mFloat) {
|
alpha = mFloat;
|
return this;
|
}
|
|
//设置选中项是否加色,默认true
|
public TeaPickerView setDiscolour(boolean bl) {
|
discolour = bl;
|
return this;
|
}
|
|
//设置选中项加色的颜色值,默认#0aa666
|
public TeaPickerView setDiscolourColor(int color) {
|
discolourColor = color;
|
return this;
|
}
|
|
//设置选中项是否有√图标,默认false
|
public TeaPickerView setDiscolourHook(boolean bl) {
|
discolourHook = bl;
|
return this;
|
}
|
|
//自定义√图标
|
public TeaPickerView setCustomHook(Drawable drawable) {
|
customHook = drawable;
|
return this;
|
}
|
|
//参数设置完毕,一定要build一下
|
public void build() {
|
initPicker();
|
initData();
|
}
|
|
private GradientDrawable getSharp() {
|
GradientDrawable drawable = new GradientDrawable();
|
//设置圆角大小
|
drawable.setCornerRadii(new float[]{radius, radius, radius, radius, 0, 0, 0, 0});
|
//设置shape背景色
|
drawable.setColor(backgroundColor);
|
return drawable;
|
}
|
|
public void setOnPickerClickListener(OnPickerClickListener listener) {
|
this.listener = listener;
|
}
|
|
private void initPicker() {
|
this.setContentView(view);
|
this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
|
if (height > 0) {
|
this.setHeight(height);
|
}
|
this.setFocusable(true);
|
this.setAnimationStyle(R.style.AnimBottom);
|
this.setBackgroundDrawable(getSharp());
|
setOnDismissListener(new OnDismissListener() {
|
@Override
|
public void onDismiss() {
|
WindowManager.LayoutParams lp = context.getWindow().getAttributes();
|
lp.alpha = 1f;
|
context.getWindow().setAttributes(lp);
|
}
|
});
|
}
|
|
private void initView() {
|
//默认参数
|
dataColor = context.getResources().getColor(R.color.picker_text_color);
|
dataHeight = getPixelsFromDp(40);
|
discolourColor = context.getResources().getColor(R.color.picker_select_text_color);
|
contentLineDrawable = context.getResources().getDrawable(R.drawable.tab_sharp);
|
//加载view
|
LayoutInflater inflater = (LayoutInflater) context
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
view = inflater.inflate(R.layout.picker_view, null);
|
contentLayout = view.findViewById(R.id.contentLayout);
|
pickerTitleName = view.findViewById(R.id.pickerTitleName);
|
pickerConfirm = view.findViewById(R.id.pickerConfirm);
|
groupSelect = view.findViewById(R.id.groupSelect);
|
mTextFirst = view.findViewById(R.id.mTextFirst);
|
mTextSecond = view.findViewById(R.id.mTextSecond);
|
mTextThird = view.findViewById(R.id.mTextThird);
|
mTextFourth = view.findViewById(R.id.mTextFourth);
|
line = view.findViewById(R.id.line);
|
pickerList = view.findViewById(R.id.pickerList);
|
mTextFirst.setOnClickListener(this);
|
mTextSecond.setOnClickListener(this);
|
mTextThird.setOnClickListener(this);
|
pickerConfirm.setOnClickListener(this);
|
|
|
if (!TextUtils.isEmpty(pickerData.getPickerTitleName())) {
|
pickerTitleName.setText(pickerData.getPickerTitleName());
|
}
|
|
}
|
|
public void show(View view) {
|
if (pickerData.getFirstDatas() != null) {
|
WindowManager.LayoutParams lp = context.getWindow().getAttributes();
|
lp.alpha = alpha;
|
context.getWindow().setAttributes(lp);
|
showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
|
switch (index) {
|
case 1:
|
adapter.setList(currData, mTextFirst.getText().toString());
|
underlineState();
|
break;
|
case 2:
|
adapter.setList(currData, mTextSecond.getText().toString());
|
underlineState();
|
break;
|
case 3:
|
adapter.setList(currData, mTextThird.getText().toString());
|
underlineState();
|
break;
|
case 4:
|
adapter.setList(currData, mTextFourth.getText().toString());
|
underlineState();
|
break;
|
}
|
} else {
|
Toast.makeText(context, "正在加载地址数据请稍后", Toast.LENGTH_SHORT).show();
|
}
|
}
|
|
private void initData() {
|
currData = pickerData.getCurrDatas(index);
|
adapter = new DataAdapter(context, currData);
|
pickerList.setVerticalScrollBarEnabled(scrollBal);
|
pickerList.setAdapter(adapter);
|
if (pickerData.getFirstText() != null) {
|
mTextFirst.setText(pickerData.getFirstText().getName());
|
if (pickerData.getSecondText() != null) {
|
mTextSecond.setText(pickerData.getSecondText().getName());
|
if (pickerData.getThirdText() != null) {
|
mTextThird.setText(pickerData.getThirdText().getName());
|
if (pickerData.getFourthText() != null)
|
mTextFourth.setText(pickerData.getFourthText().getName());
|
}
|
}
|
mTextFirst.setChecked(true);
|
}
|
pickerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
@Override
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
AddressBean currBean = currData.get(position);
|
pickerData.clearSelectText(index);
|
// mTextFirst.setText(pickerData.getFirstText().getName());
|
// mTextFirst.setTag(pickerData.getFirstText().getCode());
|
// mTextSecond.setText(pickerData.getSecondText().getName());
|
// mTextThird.setText(pickerData.getThirdText().getName());
|
List<AddressBean> data = new ArrayList<>();
|
|
data = AddressSqLite_DB_Utile.getInit(context).addressDao().findByPcode(currBean.getCode());
|
|
if (index == 1) {
|
pickerData.setFirstText(currBean);
|
mTextFirst.setText(currBean.getName());
|
mTextFirst.setTag(currBean.getCode());
|
groupSelect.check(mTextFirst.getId());
|
pickerData.setSecondDatas(data);
|
new UpdateData(currBean.getName(), data).invoke();
|
} else if (index == 2) {
|
pickerData.setSecondText(currBean);
|
mTextSecond.setText(currBean.getName());
|
mTextSecond.setTag(currBean.getCode());
|
groupSelect.check(mTextSecond.getId());
|
pickerData.setThirdDatas(data);
|
new UpdateData(currBean.getName(), data).invoke();
|
} else if (index == 3) {
|
pickerData.setThirdText(currBean);
|
mTextThird.setText(currBean.getName());
|
mTextThird.setTag(currBean.getCode());
|
groupSelect.check(mTextThird.getId());
|
pickerData.setFourthDatas(data);
|
new UpdateData(currBean.getName(), data).invoke();
|
} else if (index == 4) {
|
pickerData.setFourthText(currBean);
|
mTextFourth.setText(currBean.getName());
|
mTextFourth.setTag(currBean.getCode());
|
groupSelect.check(mTextFourth.getId());
|
|
if (listener != null) {
|
listener.OnPickerClick(pickerData);
|
}
|
}
|
}
|
});
|
}
|
|
@Override
|
public void onClick(View v) {
|
int id = v.getId();
|
if (id == R.id.mTextFirst) {
|
index = 1;
|
currData = pickerData.getCurrDatas(index);
|
adapter.setList(currData, mTextFirst.getText().toString());
|
underlineState();
|
} else if (id == R.id.mTextSecond) {
|
index = 2;
|
currData = pickerData.getCurrDatas(index);
|
adapter.setList(currData, mTextSecond.getText().toString());
|
underlineState();
|
} else if (id == R.id.mTextThird) {
|
index = 3;
|
currData = pickerData.getCurrDatas(index);
|
adapter.setList(currData, mTextThird.getText().toString());
|
underlineState();
|
} else if (id == R.id.mTextFourth) {
|
index = 4;
|
currData = pickerData.getCurrDatas(index);
|
adapter.setList(currData, mTextFourth.getText().toString());
|
underlineState();
|
}
|
/*else if (id == R.id.pickerConfirm) {
|
if (listener != null) {
|
dismiss();
|
listener.OnPickerConfirmClick(pickerData);
|
}
|
}*/
|
}
|
|
private void underlineState() {
|
if (!contentLine) {
|
return;
|
}
|
switch (index) {
|
case 1:
|
mTextFirst.setBackground(contentLineDrawable);
|
mTextSecond.setBackground(null);
|
mTextThird.setBackground(null);
|
mTextFourth.setBackground(null);
|
break;
|
case 2:
|
mTextFirst.setBackground(null);
|
mTextSecond.setBackground(contentLineDrawable);
|
mTextThird.setBackground(null);
|
mTextFourth.setBackground(null);
|
break;
|
case 3:
|
mTextFirst.setBackground(null);
|
mTextSecond.setBackground(null);
|
mTextThird.setBackground(contentLineDrawable);
|
mTextFourth.setBackground(null);
|
break;
|
case 4:
|
mTextFirst.setBackground(null);
|
mTextSecond.setBackground(null);
|
mTextThird.setBackground(null);
|
mTextFourth.setBackground(context.getResources().getDrawable(R.drawable.tab_sharp));
|
break;
|
}
|
}
|
|
private class UpdateData {
|
private String text;
|
private List<AddressBean> data;
|
|
/**
|
* @param text 地址码
|
*/
|
UpdateData(String text, List<AddressBean> data) {
|
this.text = text;
|
this.data = data;
|
}
|
|
void invoke() {
|
if (!data.isEmpty()) {
|
if (data != null && data.size() > 0) {
|
currData = data;
|
adapter.setList(currData, text);
|
underlineState();
|
index++;
|
} else {
|
if (listener != null) {
|
listener.OnPickerClick(pickerData);
|
}
|
}
|
|
} else {
|
if (listener != null) {
|
listener.OnPickerClick(pickerData);
|
}
|
}
|
}
|
}
|
|
|
public int getScreenW(Context aty) {
|
DisplayMetrics dm = aty.getResources().getDisplayMetrics();
|
return dm.widthPixels;
|
}
|
|
|
public int getScreenH(Context aty) {
|
DisplayMetrics dm = aty.getResources().getDisplayMetrics();
|
return dm.heightPixels;
|
}
|
|
private int getPixelsFromDp(int size) {
|
|
DisplayMetrics metrics = new DisplayMetrics();
|
context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
|
return (size * metrics.densityDpi) / DisplayMetrics.DENSITY_DEFAULT;
|
}
|
}
|