Search in sources :

Example 1 with TextWatcherAdapter

use of com.arlib.floatingsearchview.util.adapter.TextWatcherAdapter in project floatingsearchview by arimorty.

the class FloatingSearchView method setupQueryBar.

private void setupQueryBar() {
    mSearchInput.setTextColor(mSearchInputTextColor);
    mSearchInput.setHintTextColor(mSearchInputHintColor);
    if (!isInEditMode() && mHostActivity != null) {
        mHostActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }
    ViewTreeObserver vto = mQuerySection.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            Util.removeGlobalLayoutObserver(mQuerySection, this);
            inflateOverflowMenu(mMenuId);
        }
    });
    mMenuView.setMenuCallback(new MenuBuilder.Callback() {

        @Override
        public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
            if (mActionMenuItemListener != null) {
                mActionMenuItemListener.onActionMenuItemSelected(item);
            }
            // todo check if we should care about this return or not
            return false;
        }

        @Override
        public void onMenuModeChange(MenuBuilder menu) {
        }
    });
    mMenuView.setOnVisibleWidthChanged(new MenuView.OnVisibleWidthChangedListener() {

        @Override
        public void onItemsMenuVisibleWidthChanged(int newVisibleWidth) {
            handleOnVisibleMenuItemsWidthChanged(newVisibleWidth);
        }
    });
    mMenuView.setActionIconColor(mActionMenuItemColor);
    mMenuView.setOverflowColor(mOverflowIconColor);
    mClearButton.setVisibility(View.INVISIBLE);
    mClearButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mSearchInput.setText("");
            if (mOnClearSearchActionListener != null) {
                mOnClearSearchActionListener.onClearSearchClicked();
            }
        }
    });
    mSearchInput.addTextChangedListener(new TextWatcherAdapter() {

        public void onTextChanged(final CharSequence s, int start, int before, int count) {
            if (mSkipTextChangeEvent || !mIsFocused) {
                mSkipTextChangeEvent = false;
            } else {
                if (mSearchInput.getText().toString().length() != 0 && mClearButton.getVisibility() == View.INVISIBLE) {
                    mClearButton.setAlpha(0.0f);
                    mClearButton.setVisibility(View.VISIBLE);
                    ViewCompat.animate(mClearButton).alpha(1.0f).setDuration(CLEAR_BTN_FADE_ANIM_DURATION).start();
                } else if (mSearchInput.getText().toString().length() == 0) {
                    mClearButton.setVisibility(View.INVISIBLE);
                }
                if (mQueryListener != null && mIsFocused && !mOldQuery.equals(mSearchInput.getText().toString())) {
                    mQueryListener.onSearchTextChanged(mOldQuery, mSearchInput.getText().toString());
                }
            }
            mOldQuery = mSearchInput.getText().toString();
        }
    });
    mSearchInput.setOnFocusChangeListener(new TextView.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (mSkipQueryFocusChangeEvent) {
                mSkipQueryFocusChangeEvent = false;
            } else if (hasFocus != mIsFocused) {
                setSearchFocusedInternal(hasFocus);
            }
        }
    });
    mSearchInput.setOnKeyboardDismissedListener(new SearchInputView.OnKeyboardDismissedListener() {

        @Override
        public void onKeyboardDismissed() {
            if (mCloseSearchOnSofteKeyboardDismiss) {
                setSearchFocusedInternal(false);
            }
        }
    });
    mSearchInput.setOnSearchKeyListener(new SearchInputView.OnKeyboardSearchKeyClickListener() {

        @Override
        public void onSearchKeyClicked() {
            if (mSearchListener != null) {
                mSearchListener.onSearchAction(getQuery());
            }
            mSkipTextChangeEvent = true;
            mSkipTextChangeEvent = true;
            if (mIsTitleSet) {
                setSearchBarTitle(getQuery());
            } else {
                setSearchText(getQuery());
            }
            setSearchFocusedInternal(false);
        }
    });
    mLeftAction.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isSearchBarFocused()) {
                setSearchFocusedInternal(false);
            } else {
                switch(mLeftActionMode) {
                    case LEFT_ACTION_MODE_SHOW_HAMBURGER:
                        if (mLeftMenuClickListener != null) {
                            mLeftMenuClickListener.onClick(mLeftAction);
                        } else {
                            toggleLeftMenu();
                        }
                        break;
                    case LEFT_ACTION_MODE_SHOW_SEARCH:
                        setSearchFocusedInternal(true);
                        break;
                    case LEFT_ACTION_MODE_SHOW_HOME:
                        if (mOnHomeActionClickListener != null) {
                            mOnHomeActionClickListener.onHomeClicked();
                        }
                        break;
                    case LEFT_ACTION_MODE_NO_LEFT_ACTION:
                        // do nothing
                        break;
                }
            }
        }
    });
    refreshLeftIcon();
}
Also used : TextWatcherAdapter(com.arlib.floatingsearchview.util.adapter.TextWatcherAdapter) MenuItem(android.view.MenuItem) MenuBuilder(android.support.v7.view.menu.MenuBuilder) ImageView(android.widget.ImageView) MenuView(com.arlib.floatingsearchview.util.view.MenuView) View(android.view.View) CardView(android.support.v7.widget.CardView) SearchInputView(com.arlib.floatingsearchview.util.view.SearchInputView) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) MenuView(com.arlib.floatingsearchview.util.view.MenuView) TextView(android.widget.TextView) SearchInputView(com.arlib.floatingsearchview.util.view.SearchInputView) ViewTreeObserver(android.view.ViewTreeObserver)

Aggregations

MenuBuilder (android.support.v7.view.menu.MenuBuilder)1 CardView (android.support.v7.widget.CardView)1 RecyclerView (android.support.v7.widget.RecyclerView)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewTreeObserver (android.view.ViewTreeObserver)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 TextWatcherAdapter (com.arlib.floatingsearchview.util.adapter.TextWatcherAdapter)1 MenuView (com.arlib.floatingsearchview.util.view.MenuView)1 SearchInputView (com.arlib.floatingsearchview.util.view.SearchInputView)1