Java tutorial
package com.arlib.floatingsearchviewdemo; /** * Copyright (C) 2015 Ari C. * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.graphics.Color; import android.os.Bundle; import android.support.v4.content.res.ResourcesCompat; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.Html; import android.util.Log; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.arlib.floatingsearchview.FloatingSearchView; import com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter; import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion; import com.arlib.floatingsearchview.util.Util; import com.arlib.floatingsearchviewdemo.adapter.SearchResultsListAdapter; import com.arlib.floatingsearchviewdemo.data.ColorSuggestion; import com.arlib.floatingsearchviewdemo.data.ColorWrapper; import com.arlib.floatingsearchviewdemo.data.DataHelper; import java.util.List; public class MainActivity extends AppCompatActivity { private final String TAG = "MainActivity"; public static final long FIND_SUGGESTION_SIMULATED_DELAY = 250; private FloatingSearchView mSearchView; private RecyclerView mSearchResultsList; private SearchResultsListAdapter mSearchResultsAdapter; private DrawerLayout mDrawerLayout; private boolean mIsDarkSearchTheme = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSearchView = (FloatingSearchView) findViewById(R.id.floating_search_view); mSearchResultsList = (RecyclerView) findViewById(R.id.search_results_list); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); setupFloatingSearch(); setupResultsList(); setupDrawer(); } private void setupFloatingSearch() { mSearchView.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() { @Override public void onSearchTextChanged(String oldQuery, final String newQuery) { if (!oldQuery.equals("") && newQuery.equals("")) { mSearchView.clearSuggestions(); } else { //this shows the top left circular progress //you can call it where ever you want, but //it makes sense to do it when loading something in //the background. mSearchView.showProgress(); //simulates a query call to a data source //with a new query. DataHelper.findSuggestions(MainActivity.this, newQuery, 5, FIND_SUGGESTION_SIMULATED_DELAY, new DataHelper.OnFindSuggestionsListener() { @Override public void onResults(List<ColorSuggestion> results) { //this will swap the data and //render the collapse/expand animations as necessary mSearchView.swapSuggestions(results); //let the users know that the background //process has completed mSearchView.hideProgress(); } }); } Log.d(TAG, "onSearchTextChanged()"); } }); mSearchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() { @Override public void onSuggestionClicked(SearchSuggestion searchSuggestion) { ColorSuggestion colorSuggestion = (ColorSuggestion) searchSuggestion; DataHelper.findColors(MainActivity.this, colorSuggestion.getBody(), new DataHelper.OnFindColorsListener() { @Override public void onResults(List<ColorWrapper> results) { mSearchResultsAdapter.swapData(results); } }); Log.d(TAG, "onSuggestionClicked()"); } @Override public void onSearchAction(String query) { DataHelper.findColors(MainActivity.this, query, new DataHelper.OnFindColorsListener() { @Override public void onResults(List<ColorWrapper> results) { mSearchResultsAdapter.swapData(results); } }); Log.d(TAG, "onSearchAction()"); } }); mSearchView.setOnFocusChangeListener(new FloatingSearchView.OnFocusChangeListener() { @Override public void onFocus() { mSearchView.clearQuery(); //show suggestions when search bar gains focus (typically history suggestions) mSearchView.swapSuggestions(DataHelper.getHistory(MainActivity.this, 3)); Log.d(TAG, "onFocus()"); } @Override public void onFocusCleared() { Log.d(TAG, "onFocusCleared()"); } }); //handle menu clicks the same way as you would //in a regular activity mSearchView.setOnMenuItemClickListener(new FloatingSearchView.OnMenuItemClickListener() { @Override public void onActionMenuItemSelected(MenuItem item) { if (item.getItemId() == R.id.action_change_colors) { mIsDarkSearchTheme = true; //demonstrate setting colors for items mSearchView.setBackgroundColor(Color.parseColor("#787878")); mSearchView.setViewTextColor(Color.parseColor("#e9e9e9")); mSearchView.setHintTextColor(Color.parseColor("#e9e9e9")); mSearchView.setActionMenuOverflowColor(Color.parseColor("#e9e9e9")); mSearchView.setMenuItemIconColor(Color.parseColor("#e9e9e9")); mSearchView.setLeftActionIconColor(Color.parseColor("#e9e9e9")); mSearchView.setClearBtnColor(Color.parseColor("#e9e9e9")); mSearchView.setDividerColor(Color.parseColor("#BEBEBE")); mSearchView.setLeftActionIconColor(Color.parseColor("#e9e9e9")); } else { //just print action Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show(); } } }); //use this listener to listen to menu clicks when app:floatingSearch_leftAction="showHamburger" mSearchView.setOnLeftMenuClickListener(new FloatingSearchView.OnLeftMenuClickListener() { @Override public void onMenuOpened() { Log.d(TAG, "onMenuOpened()"); mDrawerLayout.openDrawer(GravityCompat.START); } @Override public void onMenuClosed() { Log.d(TAG, "onMenuClosed()"); } }); //use this listener to listen to menu clicks when app:floatingSearch_leftAction="showHome" mSearchView.setOnHomeActionClickListener(new FloatingSearchView.OnHomeActionClickListener() { @Override public void onHomeClicked() { Log.d(TAG, "onHomeClicked()"); } }); /* * Here you have access to the left icon and the text of a given suggestion * item after as it is bound to the suggestion list. You can utilize this * callback to change some properties of the left icon and the text. For example, you * can load the left icon images using your favorite image loading library, or change text color. * * * Important: * Keep in mind that the suggestion list is a RecyclerView, so views are reused for different * items in the list. */ mSearchView.setOnBindSuggestionCallback(new SearchSuggestionsAdapter.OnBindSuggestionCallback() { @Override public void onBindSuggestion(View suggestionView, ImageView leftIcon, TextView textView, SearchSuggestion item, int itemPosition) { ColorSuggestion colorSuggestion = (ColorSuggestion) item; String textColor = mIsDarkSearchTheme ? "#ffffff" : "#000000"; String textLight = mIsDarkSearchTheme ? "#bfbfbf" : "#787878"; if (colorSuggestion.getIsHistory()) { leftIcon.setImageDrawable( ResourcesCompat.getDrawable(getResources(), R.drawable.ic_history_black_24dp, null)); Util.setIconColor(leftIcon, Color.parseColor(textColor)); leftIcon.setAlpha(.36f); } else { leftIcon.setAlpha(0.0f); leftIcon.setImageDrawable(null); } textView.setTextColor(Color.parseColor(textColor)); String text = colorSuggestion.getBody().replaceFirst(mSearchView.getQuery(), "<font color=\"" + textLight + "\">" + mSearchView.getQuery() + "</font>"); textView.setText(Html.fromHtml(text)); } }); //listen for when suggestion list expands/shrinks in order to move down/up the //search results list mSearchView.setOnSuggestionsListHeightChanged(new FloatingSearchView.OnSuggestionsListHeightChanged() { @Override public void onSuggestionsListHeightChanged(float newHeight) { mSearchResultsList.setTranslationY(newHeight); } }); } private void setupResultsList() { mSearchResultsAdapter = new SearchResultsListAdapter(); mSearchResultsList.setAdapter(mSearchResultsAdapter); mSearchResultsList.setLayoutManager(new LinearLayoutManager(this)); } private void setupDrawer() { mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(View drawerView, float slideOffset) { } @Override public void onDrawerOpened(View drawerView) { //since the drawer might have opened as a results of //a click on the left menu, we need to make sure //to close it right after the drawer opens, so that //it is closed when the drawer is closed. mSearchView.setLeftMenuOpen(false); } @Override public void onDrawerClosed(View drawerView) { } @Override public void onDrawerStateChanged(int newState) { } }); } @Override public void onBackPressed() { //if mSearchView.setSearchFocused(false) causes the focused search //to close, then we don't want to close the activity. if mSearchView.setSearchFocused(false) //returns false, we know that the search was already closed so the call didn't change the focus //state and it makes sense to call supper onBackPressed() and close the activity if (!mSearchView.setSearchFocused(false)) { super.onBackPressed(); } } }