public Observable<TranslationTask> getTranslationTaskObservable() {
    Observable<String> originalTextObservable =
        RxTextView.textChanges(mOriginalTextInput).map(CharSequence::toString);
    Observable<TranslationDirection> languageDirectionObservable = mLanguagePicker.getObservable();

    return Observable.combineLatest(
        languageDirectionObservable,
        originalTextObservable,
        (direction, text) -> new TranslationTask(text, direction));
  }
  private void manageEditTexts() {
    EditText usualApproachEditText, reactiveApproachEditText;

    usualApproachEditText = (EditText) findViewById(R.id.editTextUsualApproach);

    usualApproachEditText.addTextChangedListener(
        new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
            onNewTextChanged(s);
          }

          @Override
          public void afterTextChanged(Editable s) {}
        });

    /** Here, we see how it can be easy to manage text changes with RxBindings */
    reactiveApproachEditText = (EditText) findViewById(R.id.editTextReactiveApproach);
    RxTextView.textChanges(reactiveApproachEditText).subscribe(this::onNewTextChanged);
  }
示例#3
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    setTitle("");

    searchView = (EditText) findViewById(R.id.searchView);

    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabStrip);

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

    searchReposFragment = SearchReposFragment.newInstance(null);
    searchUsersFragment = SearchUsersFragment.newInstance(null);

    List<Fragment> listFragments = new ArrayList<>();
    listFragments.add(searchReposFragment);
    listFragments.add(searchUsersFragment);

    viewPager.setAdapter(
        new NavigationPagerAdapter(getSupportFragmentManager(), getResources(), listFragments));
    tabLayout.setupWithViewPager(viewPager);

    searchView.setOnEditorActionListener(
        new TextView.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
            if (textView.length() > 0) {
              switch (actionId) {
                case EditorInfo.IME_ACTION_DONE:
                case EditorInfo.IME_ACTION_SEARCH:
                case EditorInfo.IME_ACTION_SEND:
                case EditorInfo.IME_ACTION_NEXT:
                case EditorInfo.IME_ACTION_GO:
                  if (textView.getText() != null) {
                    search(textView.getText().toString());
                  }
                  break;
              }
            }
            return false;
          }
        });

    subscription =
        RxTextView.textChanges(searchView)
            .filter(
                new Func1<CharSequence, Boolean>() {
                  @Override
                  public Boolean call(CharSequence s) {
                    return s.length() >= 3;
                  }
                })
            .throttleLast(100, TimeUnit.MILLISECONDS)
            .debounce(250, TimeUnit.MILLISECONDS)
            .subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                new Subscriber<CharSequence>() {
                  @Override
                  public void onCompleted() {}

                  @Override
                  public void onError(Throwable e) {}

                  @Override
                  public void onNext(CharSequence charSequence) {
                    search(charSequence.toString());
                  }
                });
  }