使用RXjava封裝通用的輪選擇控件

滾輪控件在我們的項(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ù)源共用,是不是很方便呢~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,835評論 4 61
  • 92年出生的我3月已滿25歲,剛出來工作的時候剛好21歲半,當(dāng)時真的自己都覺得自己確實(shí)非常年輕, 然時間一晃而過轉(zhuǎn)...
    5defe8fb83e1閱讀 377評論 0 0
  • 重新認(rèn)知,這四個字最能代表我二月的狀態(tài)。在原本自以為了解的質(zhì)量體系領(lǐng)域進(jìn)行了深入學(xué)習(xí),在較新的計算機(jī)網(wǎng)絡(luò)方面...
    Ares1981閱讀 456評論 0 3
  • WHAT 是什么 什么是職業(yè)倦怠這個概念是1974年,美國臨床心理學(xué)家弗洛伊登貝格爾(Herbet J. Freu...
    文雅羊閱讀 1,200評論 0 0

友情鏈接更多精彩內(nèi)容