public static SearchUsersFragment newInstance(String query) { Bundle args = new Bundle(); if (query != null) { args.putString(SearchManager.QUERY, query); } SearchUsersFragment f = new SearchUsersFragment(); f.setArguments(args); return f; }
private void search(String query) { if (searchReposFragment != null) { searchReposFragment.setQuery(query); } if (searchUsersFragment != null) { searchUsersFragment.setQuery(query); } }
@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()); } }); }