RxBinding2.0的使用

简单介绍RxBinding2.0的使用,老套路,直接上代码

 

public class RxBindActivity extends AppCompatActivity {

    private TextView mTextView;
    private Button mButton;
    private ListView mListView;
    private EditText mEditText;
    private CheckBox mCheckBox;

    private ArrayAdapter mArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rx_bind);
        findId();
        initView();
        initListener();
    }

    private void findId() {
        mTextView = (TextView) findViewById(R.id.tv_binding);
        mEditText = (EditText) findViewById(R.id.et_binding);
        mButton = (Button) findViewById(R.id.btn_binding);
        mCheckBox = (CheckBox) findViewById(R.id.cb_binding);
        mListView = (ListView) findViewById(R.id.lv_binding);
    }

    private void initView() {
        mArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, testData());
        mListView.setAdapter(mArrayAdapter);
    }


    private void initListener() {
        //1).常规处理方式
        RxView.clicks(mTextView).subscribe(v -> showToast("单机了mTextView"));
        //2).3秒钟之内只取一个点击事件,防抖操作(用户快速的连续点击了好几下)
        RxView.clicks(mButton).throttleFirst(3, TimeUnit.SECONDS)
                .subscribe(v -> showToast("单机了mButton"));
        //RxView.longClicks()长时间点击
        //3).ListView的点击
        RxAdapterView.itemClicks(mListView)
                .subscribe(position -> showToast("单机了" + position));
        //4).EditText数据改变的监听
        RxTextView.textChanges(mEditText)
                .debounce(600, TimeUnit.MILLISECONDS) //去除发送频率过快的项
                .map(charSequence -> charSequence.toString()) //将监听到输入的数据转化为string
                .subscribeOn(Schedulers.io())
                .map(s -> {
                            List dataList = new ArrayList<>();
                            if (!TextUtils.isEmpty(s)) {
                                for (String testData : testData()) {
                                    if (!testData.isEmpty() && testData.contains(s)) {
                                        dataList.add(testData);
                                    }
                                }
                                return dataList;
                            }else {
                                return testData();
                            }
                        }
                ).observeOn(AndroidSchedulers.mainThread())
                .subscribe(listData -> {
                    mArrayAdapter.clear();
                    mArrayAdapter.addAll(listData);
                    mArrayAdapter.notifyDataSetChanged();
                });
        //5).CheckBox的监听
        RxCompoundButton.checkedChanges(mCheckBox).subscribe(checkFlag -> {
            mCheckBox.setChecked(checkFlag);
            mButton.setBackgroundColor(checkFlag ? Color.GREEN : Color.RED);
        });

        //6)........
    }

    //模拟测试的数据
    private List testData() {
        List list = new ArrayList<>();
        list.add("android");
        list.add("math");
        list.add("english");
        list.add("pe");
        list.add("sience");
        return list;
    }

    private void showToast( String str) {
         runOnUiThread(() -> Toast.makeText(RxBindActivity.this, str, Toast.LENGTH_SHORT).show());
    }

}
 
布局代码就省略了......

 

 

 

你可能感兴趣的:(Android系列)