滚轮控件在我们的项目开发中经常会用到,在这里给大家推荐一个不错的滚轮控件EasyPickerView,这个控件的优点有:轻量化,只有一个类400多行代码,文字显示效果可以自定义,可自定义文字透明度和大小。
首先这个控件大家下载下来后,可以在原控件的基础上稍加修改,因为控件显示的数据类型限制为了String,这肯定不能满足我们项目的需求,要是我们的数据源为List<Model>呢,难道还需要将List<Model>转换为List<String>吗,这肯定是非常繁琐的。
改造之前的代码
//50行
private ArrayList<String> dataList = new ArrayList<>();
// 252行
Paint.FontMetrics tempFm = textPaint.getFontMetrics();
String text = dataList.get(index);
float textWidth = textPaint.measureText(text);
canvas.drawText(text, cx - textWidth / 2, tempY - (tempFm.ascent + tempFm.descent) / 2, textPaint);
//345行
public void setDataList(ArrayList<String> dataList) {
this.dataList.clear();
this.dataList.addAll(dataList);
// 更新maxTextWidth
if (null != dataList && dataList.size() > 0) {
int size = dataList.size();
for (int i = 0; i < size; i++) {
float tempWidth = textPaint.measureText(dataList.get(i));
if (tempWidth > maxTextWidth)
maxTextWidth = tempWidth;
}
curIndex = 0;
}
requestLayout();
invalidate();
}
改造之后的代码
private ArrayList<Object> dataList = new ArrayList<>();
Object text = dataList.get(index);
float textWidth = textPaint.measureText(text.toString());
canvas.drawText(text.toString(), cx - textWidth / 2, tempY - (tempFm.ascent + tempFm.descent) / 2, textPaint);
public <T extends Object> void setDataList(List<T> dataList) {
this.dataList.clear();
this.dataList.addAll(dataList);
// 更新maxTextWidth
if (null != dataList && dataList.size() > 0) {
int size = dataList.size();
for (int i = 0; i < size; i++) {
float tempWidth = textPaint.measureText(dataList.get(i).toString());
if (tempWidth > maxTextWidth)
maxTextWidth = tempWidth;
}
curIndex = 0;
}
requestLayout();
invalidate();
}
经过这些调整后,这个控件可以显示所有的数据类型了,只需被设置的对象实现toString()即可。好了,接下来就是结合RxJava对控件进行封装了,封装的前提肯定是需要引用RxJava的,这里对Rxjava就不做介绍了,网上相应的文章很多,这里直接上封装好的代码。
public class MyWhellView extends Dialog implements View.OnClickListener {
EasyPickerView whellView;
List mDatas;
private Observable observable;
private ObservableEmitter observableEmitter;
public MyWhellView(@NonNull Context context) {
super(context, R.style.common_diallog_style);
initView();
}
private void initView() {
View rootView = XYJUiUtil.inflate(R.layout.my_whell);
LinearLayout contentView = (LinearLayout) rootView.findViewById(R.id.ll_vessel);
whellView = (EasyPickerView) contentView.findViewById(R.id.whell_view);
rootView.findViewById(R.id.tv_cancel).setOnClickListener(this);
rootView.findViewById(R.id.tv_confirm).setOnClickListener(this);
setContentView(rootView);
Window dialogWindow = getWindow();
dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.x = 0;
lp.y = 0;
dialogWindow.setAttributes(lp);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.tv_cancel) {
dismiss();
} else if (v.getId() == R.id.tv_confirm) {
dismiss();
int curIndex = whellView.getCurIndex();
if (observableEmitter != null && mDatas != null && curIndex < mDatas.size()) {
Object o = mDatas.get(curIndex);
observableEmitter.onNext(o);
}
}
}
public <M> Observable<M> show(List<M> data) {
mDatas = data;
whellView.setDataList(mDatas);
show();
observable = Observable.create(new ObservableOnSubscribe<M>() {
@Override
public void subscribe(ObservableEmitter<M> e) throws Exception {
observableEmitter = e;
}
});
return observable;
}
}
布局文件:
<?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="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#e6e6e6" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/dp45"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="取消"
android:textColor="@color/orange_ff3a00"
android:textSize="16sp" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/tv_confirm"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="确定"
android:textColor="@color/orange_ff3a00"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#e6e6e6" />
<LinearLayout
android:id="@+id/ll_vessel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal">
<com.xyj.dyd.view.whell.EasyPickerView
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/whell_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
custom:epvMaxShowNum="3"
custom:epvRecycleMode="false"
custom:epvTextColor="#666666"
custom:epvTextMaxScale="1.05"
custom:epvTextMinAlpha="0.4"
custom:epvTextPadding="25dp"
custom:epvTextSize="13sp" />
</LinearLayout>
</LinearLayout>
调用时的代码
List<String> list = new ArrayList<>();
for (int i = 0; i < 50; i++) {
list.add(i + "");
}
MyWhellView whellView = new MyWhellView(getActivity());
whellView.show(list).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Logger.d(s);
}
});
经过这样封装后,这个滚轮控件可以适用于所有的数据源,并且new 一个对象后可以多个数据源共用,是不是很方便呢~