Search in sources :

Example 1 with SearchSuggestion

use of com.arlib.floatingsearchview.suggestions.model.SearchSuggestion in project floatingsearchview by arimorty.

the class FloatingSearchView method setupSuggestionSection.

private void setupSuggestionSection() {
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, true);
    mSuggestionsList.setLayoutManager(layoutManager);
    mSuggestionsList.setItemAnimator(null);
    final GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetectorListenerAdapter() {

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (mHostActivity != null) {
                Util.closeSoftKeyboard(mHostActivity);
            }
            return false;
        }
    });
    mSuggestionsList.addOnItemTouchListener(new OnItemTouchListenerAdapter() {

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            gestureDetector.onTouchEvent(e);
            return false;
        }
    });
    mSuggestionsAdapter = new SearchSuggestionsAdapter(getContext(), mSuggestionsTextSizePx, new SearchSuggestionsAdapter.Listener() {

        @Override
        public void onItemSelected(SearchSuggestion item) {
            mIsFocused = false;
            if (mSearchListener != null) {
                mSearchListener.onSuggestionClicked(item);
            }
            mSkipTextChangeEvent = true;
            if (mIsTitleSet) {
                setSearchBarTitle(item.getBody());
            } else {
                setSearchText(item.getBody());
            }
            setSearchFocusedInternal(false);
        }

        @Override
        public void onMoveItemToSearchClicked(SearchSuggestion item) {
            mSearchInput.setText(item.getBody());
            //move cursor to end of text
            mSearchInput.setSelection(mSearchInput.getText().length());
        }
    });
    refreshShowMoveUpSuggestion();
    mSuggestionsAdapter.setTextColor(this.mSuggestionTextColor);
    mSuggestionsAdapter.setRightIconColor(this.mSuggestionRightIconColor);
    mSuggestionsList.setAdapter(mSuggestionsAdapter);
    int cardViewBottomPadding = Util.dpToPx(CARD_VIEW_CORNERS_AND_TOP_BOTTOM_SHADOW_HEIGHT);
    //move up the suggestions section enough to cover the search bar
    //card's bottom left and right corners
    mSuggestionsSection.setTranslationY(-cardViewBottomPadding);
}
Also used : ViewPropertyAnimatorUpdateListener(android.support.v4.view.ViewPropertyAnimatorUpdateListener) SearchSuggestion(com.arlib.floatingsearchview.suggestions.model.SearchSuggestion) GestureDetectorListenerAdapter(com.arlib.floatingsearchview.util.adapter.GestureDetectorListenerAdapter) GestureDetector(android.view.GestureDetector) OnItemTouchListenerAdapter(com.arlib.floatingsearchview.util.adapter.OnItemTouchListenerAdapter) RecyclerView(android.support.v7.widget.RecyclerView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) SearchSuggestionsAdapter(com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter) MotionEvent(android.view.MotionEvent)

Example 2 with SearchSuggestion

use of com.arlib.floatingsearchview.suggestions.model.SearchSuggestion in project floatingsearchview by arimorty.

the class SearchSuggestionsAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(RecyclerView.ViewHolder vh, int position) {
    SearchSuggestionViewHolder viewHolder = (SearchSuggestionViewHolder) vh;
    if (!mShowRightMoveUpBtn) {
        viewHolder.rightIcon.setEnabled(false);
        viewHolder.rightIcon.setVisibility(View.INVISIBLE);
    } else {
        viewHolder.rightIcon.setEnabled(true);
        viewHolder.rightIcon.setVisibility(View.VISIBLE);
    }
    SearchSuggestion suggestionItem = mSearchSuggestions.get(position);
    viewHolder.body.setText(suggestionItem.getBody());
    if (mOnBindSuggestionCallback != null) {
        mOnBindSuggestionCallback.onBindSuggestion(viewHolder.itemView, viewHolder.leftIcon, viewHolder.body, suggestionItem, position);
    }
}
Also used : SearchSuggestion(com.arlib.floatingsearchview.suggestions.model.SearchSuggestion)

Example 3 with SearchSuggestion

use of com.arlib.floatingsearchview.suggestions.model.SearchSuggestion in project Tusky by Vavassor.

the class MainActivity method setupSearchView.

private void setupSearchView() {
    searchView.attachNavigationDrawerToMenuButton(drawer.getDrawerLayout());
    // Setup content descriptions for the different elements in the search view.
    final View leftAction = searchView.findViewById(R.id.left_action);
    leftAction.setContentDescription(getString(R.string.action_open_drawer));
    searchView.setOnFocusChangeListener(new FloatingSearchView.OnFocusChangeListener() {

        @Override
        public void onFocus() {
            leftAction.setContentDescription(getString(R.string.action_close));
        }

        @Override
        public void onFocusCleared() {
            leftAction.setContentDescription(getString(R.string.action_open_drawer));
        }
    });
    View clearButton = searchView.findViewById(R.id.clear_btn);
    clearButton.setContentDescription(getString(R.string.action_clear));
    searchView.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() {

        @Override
        public void onSearchTextChanged(String oldQuery, String newQuery) {
            if (!oldQuery.equals("") && newQuery.equals("")) {
                searchView.clearSuggestions();
                return;
            }
            if (newQuery.length() < 3) {
                return;
            }
            searchView.showProgress();
            mastodonAPI.searchAccounts(newQuery, false, 5).enqueue(new Callback<List<Account>>() {

                @Override
                public void onResponse(Call<List<Account>> call, Response<List<Account>> response) {
                    if (response.isSuccessful()) {
                        searchView.swapSuggestions(response.body());
                        searchView.hideProgress();
                    } else {
                        searchView.hideProgress();
                    }
                }

                @Override
                public void onFailure(Call<List<Account>> call, Throwable t) {
                    searchView.hideProgress();
                }
            });
        }
    });
    searchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() {

        @Override
        public void onSuggestionClicked(SearchSuggestion searchSuggestion) {
            Account accountSuggestion = (Account) searchSuggestion;
            Intent intent = new Intent(MainActivity.this, AccountActivity.class);
            intent.putExtra("id", accountSuggestion.id);
            startActivity(intent);
        }

        @Override
        public void onSearchAction(String currentQuery) {
        }
    });
    searchView.setOnBindSuggestionCallback(new SearchSuggestionsAdapter.OnBindSuggestionCallback() {

        @Override
        public void onBindSuggestion(View suggestionView, ImageView leftIcon, TextView textView, SearchSuggestion item, int itemPosition) {
            Account accountSuggestion = ((Account) item);
            Picasso.with(MainActivity.this).load(accountSuggestion.avatar).placeholder(R.drawable.avatar_default).into(leftIcon);
            String searchStr = accountSuggestion.getDisplayName() + " " + accountSuggestion.username;
            final SpannableStringBuilder str = new SpannableStringBuilder(searchStr);
            str.setSpan(new StyleSpan(Typeface.BOLD), 0, accountSuggestion.getDisplayName().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(str);
            textView.setMaxLines(1);
            textView.setEllipsize(TextUtils.TruncateAt.END);
        }
    });
}
Also used : Account(com.keylesspalace.tusky.entity.Account) Call(retrofit2.Call) SearchSuggestion(com.arlib.floatingsearchview.suggestions.model.SearchSuggestion) Intent(android.content.Intent) FloatingSearchView(com.arlib.floatingsearchview.FloatingSearchView) ImageView(android.widget.ImageView) BindView(butterknife.BindView) View(android.view.View) FloatingSearchView(com.arlib.floatingsearchview.FloatingSearchView) TextView(android.widget.TextView) SearchSuggestionsAdapter(com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter) Response(retrofit2.Response) Callback(retrofit2.Callback) StyleSpan(android.text.style.StyleSpan) TextView(android.widget.TextView) ImageView(android.widget.ImageView) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

SearchSuggestion (com.arlib.floatingsearchview.suggestions.model.SearchSuggestion)3 SearchSuggestionsAdapter (com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter)2 Intent (android.content.Intent)1 ViewPropertyAnimatorUpdateListener (android.support.v4.view.ViewPropertyAnimatorUpdateListener)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 StyleSpan (android.text.style.StyleSpan)1 GestureDetector (android.view.GestureDetector)1 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 BindView (butterknife.BindView)1 FloatingSearchView (com.arlib.floatingsearchview.FloatingSearchView)1 GestureDetectorListenerAdapter (com.arlib.floatingsearchview.util.adapter.GestureDetectorListenerAdapter)1 OnItemTouchListenerAdapter (com.arlib.floatingsearchview.util.adapter.OnItemTouchListenerAdapter)1 Account (com.keylesspalace.tusky.entity.Account)1 Call (retrofit2.Call)1 Callback (retrofit2.Callback)1