@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { final SuggestionItem item = mUsersSearchAdapter.getSuggestionItem(position); switch (mUsersSearchAdapter.getItemViewType(position)) { case SuggestionsAdapter.VIEW_TYPE_USER_SUGGESTION_ITEM: { Utils.openUserProfile(this, getAccountId(), item.extra_id, item.summary, null); finish(); break; } case SuggestionsAdapter.VIEW_TYPE_USER_SCREEN_NAME: { Utils.openUserProfile(this, getAccountId(), -1, item.title, null); finish(); break; } case SuggestionsAdapter.VIEW_TYPE_SAVED_SEARCH: case SuggestionsAdapter.VIEW_TYPE_SEARCH_HISTORY: { Utils.openSearch(this, getAccountId(), item.title); finish(); break; } } }
private void init(AttributeSet attrs) { inflate(getContext(), R.layout.searchbar, this); TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.MaterialSearchBar); iconRightResId = array.getResourceId(R.styleable.MaterialSearchBar_iconRight, -1); iconLefttResId = array.getResourceId(R.styleable.MaterialSearchBar_iconLeft, -1); hint = array.getString(R.styleable.MaterialSearchBar_hint); maxSuggestionCount = array.getInt(R.styleable.MaterialSearchBar_maxSuggestionsCount, 3); speechMode = array.getBoolean(R.styleable.MaterialSearchBar_speechMode, false); destiny = getResources().getDisplayMetrics().density; adapter = new SuggestionsAdapter(LayoutInflater.from(getContext())); adapter.setOnClickListener(this); adapter.maxSuggestionsCount = maxSuggestionCount; RecyclerView recyclerView = (RecyclerView) findViewById(R.id.mt_recycler); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); array.recycle(); searchIcon = (ImageView) findViewById(R.id.mt_search); arrowIcon = (ImageView) findViewById(R.id.mt_arrow); searchEdit = (EditText) findViewById(R.id.mt_editText); inputContainer = (LinearLayout) findViewById(R.id.inputContainer); findViewById(R.id.mt_clear).setOnClickListener(this); arrowIcon.setOnClickListener(this); searchEdit.setOnFocusChangeListener(this); setOnClickListener(this); searchEdit.setOnEditorActionListener(this); if (speechMode) searchIcon.setOnClickListener(this); setupIcons(); }
@Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (listenerExists()) onSearchActionListener.onSearchConfirmed(searchEdit.getText()); if (suggestionsVisible) animateLastRequests(getListHeight(), 0); adapter.addSuggestion(searchEdit.getText().toString()); return false; }
private void setSuggestionsCursor(Cursor cursor) { if (mSuggestionsAdapter == null) { return; } Cursor oldCursor = mSuggestionsAdapter.swapCursor(cursor); if (oldCursor != null) { oldCursor.close(); } }
/** Shows search input and close arrow */ public void enableSearch() { adapter.notifyDataSetChanged(); searchEnabled = true; Animation left_in = AnimationUtils.loadAnimation(getContext(), R.anim.fade_in_left); Animation left_out = AnimationUtils.loadAnimation(getContext(), R.anim.fade_out_left); left_in.setAnimationListener(this); inputContainer.setVisibility(VISIBLE); inputContainer.startAnimation(left_in); animateLastRequests(0, getListHeight()); if (listenerExists()) { onSearchActionListener.onSearchStateChanged(true); } searchIcon.startAnimation(left_out); }
private void animateLastRequests(int from, int to) { suggestionsVisible = to > 0 ? true : false; final RelativeLayout last = (RelativeLayout) findViewById(R.id.last); final ViewGroup.LayoutParams lp = last.getLayoutParams(); ValueAnimator animator = ValueAnimator.ofInt(from, to); animator.setDuration(200); animator.addUpdateListener( new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { lp.height = (int) animation.getAnimatedValue(); last.setLayoutParams(lp); } }); if (adapter.getItemCount() > 0) animator.start(); }
@Override public void onLoaderReset(Loader<Cursor> loader) { mUsersSearchAdapter.changeCursor(null); }
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { mUsersSearchAdapter.changeCursor(data); }
private int getListHeight() { return (int) ((adapter.getItemCount() * 50) * destiny); }
/** * Sets the array of recent search queries. It is advisable to save the queries when the activity * is destroyed and call this method when creating the activity. * * @param suggestions an array of queries * @see #getLastSuggestions() * @see #setMaxSuggestionCount(int) */ public void setLastSuggestions(List<String> suggestions) { adapter.setSuggestions(suggestions); }
/** * Returns the last search queries. The queries are stored only for the duration of one activity * session. When the activity is destroyed, the queries will be deleted. To save queries, use the * method getLastSuggestions(). To recover the queries use the method setLastSuggestions(). * * @return array with the latest search queries * @see #setLastSuggestions(List) * @see #setMaxSuggestionCount(int) */ public List<String> getLastSuggestions() { return adapter.getSuggestions(); }
/** * Specifies the maximum number of search queries stored until the activity is destroyed * * @param maxQuery maximum queries */ public void setMaxSuggestionCount(int maxQuery) { this.maxSuggestionCount = maxQuery; adapter.maxSuggestionsCount = maxQuery; }