滾輪控件在我們的項(xiàng)目開發(fā)中經(jīng)常會用到,在這里給大家推薦一個不錯的滾輪控件EasyPickerView,這個控件的優(yōu)點(diǎn)有:輕量化,只有一個類400多行代碼,文字顯示效果可以自定義,可自定義文字透明度和大小。
首先這個控件大家下載下來后,可以在原控件的基礎(chǔ)上稍加修改,因?yàn)榭丶@示的數(shù)據(jù)類型限制為了String,這肯定不能滿足我們項(xiàng)目的需求,要是我們的數(shù)據(jù)源為List<Model>呢,難道還需要將List<Model>轉(zhuǎn)換為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();
}
經(jīng)過這些調(diào)整后,這個控件可以顯示所有的數(shù)據(jù)類型了,只需被設(shè)置的對象實(shí)現(xiàn)toString()即可。好了,接下來就是結(jié)合RxJava對控件進(jìn)行封裝了,封裝的前提肯定是需要引用RxJava的,這里對Rxjava就不做介紹了,網(wǎng)上相應(yīng)的文章很多,這里直接上封裝好的代碼。
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>
調(diào)用時的代碼
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);
}
});
經(jīng)過這樣封裝后,這個滾輪控件可以適用于所有的數(shù)據(jù)源,并且new 一個對象后可以多個數(shù)據(jù)源共用,是不是很方便呢~