From 66a2503b23583aa6833deb678f74fe6dc93ea976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Catalan=20Ban=CC=83uls?= Date: Sat, 12 Dec 2015 18:02:25 +0100 Subject: [PATCH 1/6] Renames some shitty code --- .../materialsearchview/SearchAdapter.java | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/library/src/main/java/com/miguelcatalan/materialsearchview/SearchAdapter.java b/library/src/main/java/com/miguelcatalan/materialsearchview/SearchAdapter.java index c3728e1..df7dd96 100644 --- a/library/src/main/java/com/miguelcatalan/materialsearchview/SearchAdapter.java +++ b/library/src/main/java/com/miguelcatalan/materialsearchview/SearchAdapter.java @@ -1,7 +1,6 @@ package com.miguelcatalan.materialsearchview; import android.content.Context; -import android.graphics.Color; import android.graphics.drawable.Drawable; import android.text.TextUtils; import android.view.LayoutInflater; @@ -17,28 +16,27 @@ import java.util.List; /** + * Suggestions Adapter. + * * @author Miguel Catalan Bañuls */ public class SearchAdapter extends BaseAdapter implements Filterable { private ArrayList data; - - private String[] typeAheadData; - + private String[] suggestions; private Drawable suggestionIcon; + private LayoutInflater inflater; - LayoutInflater inflater; - - public SearchAdapter(Context context, String[] typeAheadData) { + public SearchAdapter(Context context, String[] suggestions) { inflater = LayoutInflater.from(context); data = new ArrayList<>(); - this.typeAheadData = typeAheadData; + this.suggestions = suggestions; } - public SearchAdapter(Context context, String[] typeAheadData, Drawable suggestionIcon) { + public SearchAdapter(Context context, String[] suggestions, Drawable suggestionIcon) { inflater = LayoutInflater.from(context); data = new ArrayList<>(); - this.typeAheadData = typeAheadData; + this.suggestions = suggestions; this.suggestionIcon = suggestionIcon; } @@ -49,12 +47,13 @@ public Filter getFilter() { protected FilterResults performFiltering(CharSequence constraint) { FilterResults filterResults = new FilterResults(); if (!TextUtils.isEmpty(constraint)) { + // Retrieve the autocomplete results. List searchData = new ArrayList<>(); - for (String str : typeAheadData) { - if (str.toLowerCase().startsWith(constraint.toString().toLowerCase())) { - searchData.add(str); + for (String string : suggestions) { + if (string.toLowerCase().startsWith(constraint.toString().toLowerCase())) { + searchData.add(string); } } @@ -93,29 +92,30 @@ public long getItemId(int position) { @Override public View getView(int position, View convertView, ViewGroup parent) { - MyViewHolder mViewHolder; + + SuggestionsViewHolder viewHolder; if (convertView == null) { convertView = inflater.inflate(R.layout.suggest_item, parent, false); - mViewHolder = new MyViewHolder(convertView); - convertView.setTag(mViewHolder); + viewHolder = new SuggestionsViewHolder(convertView); + convertView.setTag(viewHolder); } else { - mViewHolder = (MyViewHolder) convertView.getTag(); + viewHolder = (SuggestionsViewHolder) convertView.getTag(); } String currentListData = (String) getItem(position); - mViewHolder.textView.setText(currentListData); - + viewHolder.textView.setText(currentListData); return convertView; } - private class MyViewHolder { + private class SuggestionsViewHolder { + TextView textView; ImageView imageView; - public MyViewHolder(View convertView) { + public SuggestionsViewHolder(View convertView) { textView = (TextView) convertView.findViewById(R.id.suggestion_text); if (suggestionIcon != null) { imageView = (ImageView) convertView.findViewById(R.id.suggestion_icon); From 37d6a54a4ed86b6502fe043b36e298357616d9c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Catalan=20Ban=CC=83uls?= Date: Sat, 12 Dec 2015 18:16:14 +0100 Subject: [PATCH 2/6] Adds custom animation duration modifiers --- .../materialsearchview/MaterialSearchView.java | 14 ++++++++++++-- .../materialsearchview/utils/AnimationUtil.java | 6 +++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java b/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java index 9345b68..f15c2f3 100644 --- a/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java +++ b/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java @@ -46,7 +46,7 @@ public class MaterialSearchView extends FrameLayout implements Filter.FilterList private MenuItem mMenuItem; private boolean mIsSearchOpen = false; - + private int mAnimationDuration; private boolean mClearingFocus; //Views @@ -161,6 +161,7 @@ private void initiateView() { initSearchView(); mSuggestionsListView.setVisibility(GONE); + setAnimationDuration(AnimationUtil.ANIMATION_DURATION_MEDIUM); } private void initSearchView() { @@ -473,6 +474,15 @@ public boolean isSearchOpen() { return mIsSearchOpen; } + /** + * Sets animation duration. ONLY FOR PRE-LOLLIPOP!! + * + * @param duration duration of the animation + */ + public void setAnimationDuration(int duration) { + mAnimationDuration = duration; + } + /** * Open Search View. This will animate the showing of the view. */ @@ -532,7 +542,7 @@ public boolean onAnimationCancel(View view) { AnimationUtil.reveal(mSearchTopBar, animationListener); } else { - AnimationUtil.fadeInView(mSearchLayout, AnimationUtil.ANIMATION_DURATION_MEDIUM, animationListener); + AnimationUtil.fadeInView(mSearchLayout, mAnimationDuration, animationListener); } } diff --git a/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java b/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java index d18be6e..2355261 100644 --- a/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java +++ b/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java @@ -15,9 +15,9 @@ */ public class AnimationUtil { - public static final int ANIMATION_DURATION_SHORT = 150; - public static final int ANIMATION_DURATION_MEDIUM = 400; - public static final int ANIMATION_DURATION_LONG = 800; + public static int ANIMATION_DURATION_SHORT = 150; + public static int ANIMATION_DURATION_MEDIUM = 400; + public static int ANIMATION_DURATION_LONG = 800; public interface AnimationListener { /** From 90ae5ceba858ee50746de057854822a9ebd4b120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Catalan=20Ban=CC=83uls?= Date: Sat, 12 Dec 2015 18:18:33 +0100 Subject: [PATCH 3/6] Some code clean --- .../materialsearchview/MaterialSearchView.java | 7 ++----- .../materialsearchview/utils/AnimationUtil.java | 11 ----------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java b/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java index f15c2f3..2e4d7e0 100644 --- a/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java +++ b/library/src/main/java/com/miguelcatalan/materialsearchview/MaterialSearchView.java @@ -491,9 +491,9 @@ public void showSearch() { } /** - * Open Search View. if animate is true, Animate the showing of the view. + * Open Search View. If animate is true, Animate the showing of the view. * - * @param animate + * @param animate true for animate */ public void showSearch(boolean animate) { if (isSearchOpen()) { @@ -613,11 +613,9 @@ public void clearFocus() { @Override public Parcelable onSaveInstanceState() { - //begin boilerplate code that allows parent classes to save state Parcelable superState = super.onSaveInstanceState(); mSavedState = new SavedState(superState); - //end mSavedState.query = mUserQuery != null ? mUserQuery.toString() : null; mSavedState.isSearchOpen = this.mIsSearchOpen; @@ -626,7 +624,6 @@ public Parcelable onSaveInstanceState() { @Override public void onRestoreInstanceState(Parcelable state) { - //begin boilerplate code so parent classes can restore state if (!(state instanceof SavedState)) { super.onRestoreInstanceState(state); return; diff --git a/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java b/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java index 2355261..e6f1135 100644 --- a/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java +++ b/library/src/main/java/com/miguelcatalan/materialsearchview/utils/AnimationUtil.java @@ -57,7 +57,6 @@ public static void fadeInView(View view, int duration, final AnimationListener l @Override public void onAnimationStart(View view) { if (!listener.onAnimationStart(view)) { - //execute Parent MEthod view.setDrawingCacheEnabled(true); } } @@ -65,16 +64,12 @@ public void onAnimationStart(View view) { @Override public void onAnimationEnd(View view) { if (!listener.onAnimationEnd(view)) { - //execute Parent MEthod view.setDrawingCacheEnabled(false); } } @Override public void onAnimationCancel(View view) { - if (!listener.onAnimationCancel(view)) { - //execute Parent MEthod - } } }; } @@ -127,7 +122,6 @@ public static void fadeOutView(View view, int duration, final AnimationListener @Override public void onAnimationStart(View view) { if (listener == null || !listener.onAnimationStart(view)) { - //execute Parent MEthod view.setDrawingCacheEnabled(true); } } @@ -135,18 +129,13 @@ public void onAnimationStart(View view) { @Override public void onAnimationEnd(View view) { if (listener == null || !listener.onAnimationEnd(view)) { - //execute Parent MEthod view.setVisibility(View.GONE); - //view.setAlpha(1f); view.setDrawingCacheEnabled(false); } } @Override public void onAnimationCancel(View view) { - if (listener == null || !listener.onAnimationCancel(view)) { - //execute Parent MEthod - } } }); } From 938f456e0a4350a539ae03a3e06dc3cee701d258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Catalan=20Ban=CC=83uls?= Date: Sun, 20 Dec 2015 19:41:48 +0100 Subject: [PATCH 4/6] Add example of how to implement AppBarLayout --- library/build.gradle | 3 +- sample/src/main/AndroidManifest.xml | 7 +- .../sample/DummyFragment.java | 29 +++++ .../sample/MainActivity.java | 6 + .../sample/StickyActivity.java | 2 +- .../sample/TabActivity.java | 112 ++++++++++++++++++ .../sample/ViewPagerAdapter.java | 40 +++++++ .../main/res/layout/activity_appbarlayout.xml | 50 ++++++++ sample/src/main/res/layout/activity_main.xml | 7 ++ sample/src/main/res/layout/fragment_dummy.xml | 12 ++ sample/src/main/res/values/strings.xml | 4 +- 11 files changed, 266 insertions(+), 6 deletions(-) create mode 100644 sample/src/main/java/com/miguelcatalan/materialsearchview/sample/DummyFragment.java create mode 100644 sample/src/main/java/com/miguelcatalan/materialsearchview/sample/TabActivity.java create mode 100644 sample/src/main/java/com/miguelcatalan/materialsearchview/sample/ViewPagerAdapter.java create mode 100644 sample/src/main/res/layout/activity_appbarlayout.xml create mode 100644 sample/src/main/res/layout/fragment_dummy.xml diff --git a/library/build.gradle b/library/build.gradle index 5d54e1b..dfda1d5 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -30,7 +30,8 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.1.0' + compile 'com.android.support:appcompat-v7:23.1.1' + compile 'com.android.support:design:23.1.1' } publish { diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index ecee02e..6c0b2c7 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -30,8 +30,11 @@ + android:label="@string/title_activity_sticky"> + + diff --git a/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/DummyFragment.java b/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/DummyFragment.java new file mode 100644 index 0000000..e722923 --- /dev/null +++ b/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/DummyFragment.java @@ -0,0 +1,29 @@ +package com.miguelcatalan.materialsearchview.sample; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * @author Miguel Catalan Bañuls + */ +public class DummyFragment extends Fragment { + + public DummyFragment() { + // Required empty public constructor + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_dummy, container, false); + } +} \ No newline at end of file diff --git a/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/MainActivity.java b/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/MainActivity.java index 5770a41..085adf2 100644 --- a/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/MainActivity.java +++ b/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/MainActivity.java @@ -12,6 +12,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe private Button themedButton; private Button voiceButton; private Button stickyButton; + private Button tabButton; @Override protected void onCreate(Bundle savedInstanceState) { @@ -26,6 +27,8 @@ protected void onCreate(Bundle savedInstanceState) { voiceButton.setOnClickListener(this); stickyButton = (Button) findViewById(R.id.button_sticky); stickyButton.setOnClickListener(this); + tabButton = (Button) findViewById(R.id.button_tab); + tabButton.setOnClickListener(this); } @Override @@ -44,6 +47,9 @@ public void onClick(View view) { case R.id.button_sticky: intent = new Intent(this, StickyActivity.class); break; + case R.id.button_tab: + intent = new Intent(this, TabActivity.class); + break; } if (intent != null) { startActivity(intent); diff --git a/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/StickyActivity.java b/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/StickyActivity.java index dd55c7b..1259b19 100644 --- a/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/StickyActivity.java +++ b/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/StickyActivity.java @@ -21,7 +21,7 @@ public class StickyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_default); + setContentView(R.layout.activity_sticky); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); diff --git a/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/TabActivity.java b/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/TabActivity.java new file mode 100644 index 0000000..0212236 --- /dev/null +++ b/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/TabActivity.java @@ -0,0 +1,112 @@ +package com.miguelcatalan.materialsearchview.sample; + +import android.content.Intent; +import android.os.Bundle; +import android.speech.RecognizerIntent; +import android.support.design.widget.Snackbar; +import android.support.design.widget.TabLayout; +import android.support.v4.view.ViewPager; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; +import android.text.TextUtils; +import android.view.Menu; +import android.view.MenuItem; + +import com.miguelcatalan.materialsearchview.MaterialSearchView; + +import java.util.ArrayList; + +public class TabActivity extends AppCompatActivity { + + private MaterialSearchView searchView; + private ViewPager viewPager; + private TabLayout tabLayout; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_appbarlayout); + + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + + viewPager = (ViewPager) findViewById(R.id.viewpager); + setupViewPager(viewPager); + + tabLayout = (TabLayout) findViewById(R.id.tabs); + tabLayout.setupWithViewPager(viewPager); + + searchView = (MaterialSearchView) findViewById(R.id.search_view); + searchView.setVoiceSearch(false); + searchView.setCursorDrawable(R.drawable.custom_cursor); + searchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions)); + searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + Snackbar.make(findViewById(R.id.container), "Query: " + query, Snackbar.LENGTH_LONG) + .show(); + return false; + } + + @Override + public boolean onQueryTextChange(String newText) { + //Do some magic + return false; + } + }); + + searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() { + @Override + public void onSearchViewShown() { + //Do some magic + } + + @Override + public void onSearchViewClosed() { + //Do some magic + } + }); + } + + private void setupViewPager(ViewPager viewPager) { + ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); + adapter.addFragment(new DummyFragment(), "ONE"); + adapter.addFragment(new DummyFragment(), "TWO"); + viewPager.setAdapter(adapter); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu_main, menu); + + MenuItem item = menu.findItem(R.id.action_search); + searchView.setMenuItem(item); + + return true; + } + + @Override + public void onBackPressed() { + if (searchView.isSearchOpen()) { + searchView.closeSearch(); + } else { + super.onBackPressed(); + } + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == MaterialSearchView.REQUEST_VOICE && resultCode == RESULT_OK) { + ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); + if (matches != null && matches.size() > 0) { + String searchWrd = matches.get(0); + if (!TextUtils.isEmpty(searchWrd)) { + searchView.setQuery(searchWrd, false); + } + } + + return; + } + super.onActivityResult(requestCode, resultCode, data); + } +} diff --git a/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/ViewPagerAdapter.java b/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/ViewPagerAdapter.java new file mode 100644 index 0000000..2deb6f6 --- /dev/null +++ b/sample/src/main/java/com/miguelcatalan/materialsearchview/sample/ViewPagerAdapter.java @@ -0,0 +1,40 @@ +package com.miguelcatalan.materialsearchview.sample; + +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Miguel Catalan Bañuls + */ +class ViewPagerAdapter extends FragmentPagerAdapter { + private final List mFragmentList = new ArrayList<>(); + private final List mFragmentTitleList = new ArrayList<>(); + + public ViewPagerAdapter(FragmentManager manager) { + super(manager); + } + + @Override + public Fragment getItem(int position) { + return mFragmentList.get(position); + } + + @Override + public int getCount() { + return mFragmentList.size(); + } + + public void addFragment(Fragment fragment, String title) { + mFragmentList.add(fragment); + mFragmentTitleList.add(title); + } + + @Override + public CharSequence getPageTitle(int position) { + return mFragmentTitleList.get(position); + } +} \ No newline at end of file diff --git a/sample/src/main/res/layout/activity_appbarlayout.xml b/sample/src/main/res/layout/activity_appbarlayout.xml new file mode 100644 index 0000000..101e60d --- /dev/null +++ b/sample/src/main/res/layout/activity_appbarlayout.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 92a0706..de6e207 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -40,6 +40,13 @@ android:layout_height="wrap_content" android:text="Sticky" android:textColor="@color/theme_secondary_text" /> + +