一、之前在项目里面做的选择器,都是一种弹窗的模式,后来在新项目中遇到一个新需求,需要把选择器布局到Activity或者fragment上面,当时有点蒙蔽了,因为以前都是用第三方框架直接集成。但是既然产品提出了需求,必须要做,所以特地研究了第三方框架Android-PickerView,发现里面有一个自定义类WheelView,可以单独拿出来用,这样的话就好办的多了。
二、先上效果图
条件选择器:
日期选择器:
三、实现过程
1.首先添加依赖,在app的build.bradle文件里面添加
compile 'com.contrarywind:Android-PickerView:4.1.6'
自定义条件选择器的view,代码过程如下
/**
* Created by zzf on 2018/8/15.
*/
public class SelectorView extends LinearLayout{
WheelView wv_room;
private List<String> listRoom = new ArrayList<>();
private ArrayWheelAdapter<String> roomAdapter;
private OnSelectListener mListener;
public SelectorView(Context context) {
this(context,null);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
getParent().requestDisallowInterceptTouchEvent(true);
return super.dispatchTouchEvent(ev);
}
public SelectorView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View mView = LayoutInflater.from(context).inflate(R.layout.selector_view, this);
wv_room = mView.findViewById(R.id.wv_selector);
wv_room.setDividerColor(getResources().getColor(R.color.orange_b79a54));
wv_room.setTextColorCenter(getResources().getColor(R.color.orange_b79a54));
/*日*/
resetListString(listRoom, 1, 30);
roomAdapter = new ArrayWheelAdapter<>(listRoom);
wv_room.setAdapter(roomAdapter);
wv_room.setCurrentItem(0);
wv_room.setCyclic(false);
wv_room.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(int index) {
String currentRoom = listRoom.get(wv_room.getCurrentItem());
mListener.selectRoom(currentRoom);
}
});
}
private void resetListString(List<String> list, int start, int end) {
if (list != null) {
list.clear();
}
for (int i = start; i < end +