|
@@ -0,0 +1,228 @@
|
|
|
|
|
+package droidmentor.searchviewsample;
|
|
|
|
|
+
|
|
|
|
|
+import android.animation.Animator;
|
|
|
|
|
+import android.animation.AnimatorListenerAdapter;
|
|
|
|
|
+import android.graphics.Color;
|
|
|
|
|
+import android.os.Build;
|
|
|
|
|
+import android.os.Bundle;
|
|
|
|
|
+import android.support.annotation.RequiresApi;
|
|
|
|
|
+import android.support.v4.view.MenuItemCompat;
|
|
|
|
|
+import android.support.v7.app.AppCompatActivity;
|
|
|
|
|
+import android.support.v7.widget.SearchView;
|
|
|
|
|
+import android.support.v7.widget.Toolbar;
|
|
|
|
|
+import android.text.InputFilter;
|
|
|
|
|
+import android.text.InputType;
|
|
|
|
|
+import android.util.Log;
|
|
|
|
|
+import android.view.Menu;
|
|
|
|
|
+import android.view.MenuItem;
|
|
|
|
|
+import android.view.View;
|
|
|
|
|
+import android.view.ViewAnimationUtils;
|
|
|
|
|
+import android.widget.AutoCompleteTextView;
|
|
|
|
|
+import android.widget.EditText;
|
|
|
|
|
+import android.widget.ImageView;
|
|
|
|
|
+import android.widget.TextView;
|
|
|
|
|
+import android.widget.Toast;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+
|
|
|
|
|
+public class SearchViewExampleActivity extends AppCompatActivity {
|
|
|
|
|
+
|
|
|
|
|
+ Toolbar toolbar, tb_search;
|
|
|
|
|
+ Menu menu_search;
|
|
|
|
|
+ MenuItem item_search;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
|
|
+ setContentView(R.layout.activity_main);
|
|
|
|
|
+ toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
|
|
|
+ setSupportActionBar(toolbar);
|
|
|
|
|
+ setSearchtollbar();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onCreateOptionsMenu(final Menu menu) {
|
|
|
|
|
+ getMenuInflater().inflate(R.menu.menu_home, menu);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
+ // Handle item selection
|
|
|
|
|
+ switch (item.getItemId()) {
|
|
|
|
|
+ case R.id.action_status:
|
|
|
|
|
+ Toast.makeText(this, "Home Status Click", Toast.LENGTH_SHORT).show();
|
|
|
|
|
+ return true;
|
|
|
|
|
+ case R.id.action_search:
|
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
|
+ circleReveal(R.id.tb_search, 1, true, true);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ tb_search.setVisibility(View.VISIBLE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ item_search.expandActionView();
|
|
|
|
|
+ return true;
|
|
|
|
|
+ case R.id.action_settings:
|
|
|
|
|
+ Toast.makeText(this, "Home Settings Click", Toast.LENGTH_SHORT).show();
|
|
|
|
|
+ return true;
|
|
|
|
|
+ default:
|
|
|
|
|
+ return super.onOptionsItemSelected(item);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setSearchtollbar() {
|
|
|
|
|
+ tb_search = (Toolbar) findViewById(R.id.tb_search);
|
|
|
|
|
+ if (tb_search != null) {
|
|
|
|
|
+ tb_search.inflateMenu(R.menu.menu_search);
|
|
|
|
|
+ menu_search = tb_search.getMenu();
|
|
|
|
|
+
|
|
|
|
|
+ tb_search.setNavigationOnClickListener(new View.OnClickListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onClick(View v) {
|
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
|
+ circleReveal(R.id.tb_search, 1, true, false);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ tb_search.setVisibility(View.GONE);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ item_search = menu_search.findItem(R.id.action_filter_search);
|
|
|
|
|
+
|
|
|
|
|
+ MenuItemCompat.setOnActionExpandListener(item_search, new MenuItemCompat.OnActionExpandListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onMenuItemActionCollapse(MenuItem item) {
|
|
|
|
|
+ // Do something when collapsed
|
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
|
+ circleReveal(R.id.tb_search, 1, true, false);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ tb_search.setVisibility(View.GONE);
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onMenuItemActionExpand(MenuItem item) {
|
|
|
|
|
+ // Do something when expanded
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ initSearchView();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Log.d("toolbar", "setSearchtollbar: NULL");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void initSearchView() {
|
|
|
|
|
+ final SearchView searchView =
|
|
|
|
|
+ (SearchView) menu_search.findItem(R.id.action_filter_search).getActionView();
|
|
|
|
|
+
|
|
|
|
|
+ // Enable/Disable Submit button in the keyboard
|
|
|
|
|
+
|
|
|
|
|
+ searchView.setSubmitButtonEnabled(false);
|
|
|
|
|
+
|
|
|
|
|
+ // Change search close button image
|
|
|
|
|
+
|
|
|
|
|
+ ImageView closeButton = (ImageView) searchView.findViewById(R.id.search_close_btn);
|
|
|
|
|
+ closeButton.setImageResource(R.drawable.ic_close);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // set hint and the text colors
|
|
|
|
|
+
|
|
|
|
|
+ EditText txtSearch = ((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text));
|
|
|
|
|
+ txtSearch.setHint("请输入网点编号");
|
|
|
|
|
+ txtSearch.setMaxLines(1);
|
|
|
|
|
+ // 只能输入数字
|
|
|
|
|
+ txtSearch.setInputType(InputType.TYPE_CLASS_NUMBER);
|
|
|
|
|
+ //限制输入10位
|
|
|
|
|
+ txtSearch.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
|
|
|
|
|
+ txtSearch.setHintTextColor(Color.DKGRAY);
|
|
|
|
|
+ txtSearch.setTextColor(getResources().getColor(R.color.colorPrimary));
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // set the cursor
|
|
|
|
|
+
|
|
|
|
|
+ AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
|
|
|
|
|
+ try {
|
|
|
|
|
+ Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
|
|
|
|
|
+ mCursorDrawableRes.setAccessible(true);
|
|
|
|
|
+ mCursorDrawableRes.set(searchTextView, R.drawable.search_cursor); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onQueryTextSubmit(String query) {
|
|
|
|
|
+ callSearch(query);
|
|
|
|
|
+ searchView.clearFocus();
|
|
|
|
|
+ Log.i("query", "" + query);
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onQueryTextChange(String newText) {
|
|
|
|
|
+ callSearch(newText);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void callSearch(String query) {
|
|
|
|
|
+ //Do searching
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
|
|
|
+ public void circleReveal(int viewID, int posFromRight, boolean containsOverflow, final boolean isShow) {
|
|
|
|
|
+ final View myView = findViewById(viewID);
|
|
|
|
|
+
|
|
|
|
|
+ int width = myView.getWidth();
|
|
|
|
|
+
|
|
|
|
|
+ if (posFromRight > 0) {
|
|
|
|
|
+ width -= (posFromRight * getResources().getDimensionPixelSize(R.dimen.abc_action_button_min_width_material)) - (getResources().getDimensionPixelSize(R.dimen.abc_action_button_min_width_material) / 2);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (containsOverflow) {
|
|
|
|
|
+ width -= getResources().getDimensionPixelSize(R.dimen.abc_action_button_min_width_overflow_material);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ int cx = width;
|
|
|
|
|
+ int cy = myView.getHeight() / 2;
|
|
|
|
|
+
|
|
|
|
|
+ Animator anim;
|
|
|
|
|
+ if (isShow) {
|
|
|
|
|
+ anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, (float) width);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, (float) width, 0);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ anim.setDuration((long) 220);
|
|
|
|
|
+
|
|
|
|
|
+ // make the view invisible when the animation is done
|
|
|
|
|
+ anim.addListener(new AnimatorListenerAdapter() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onAnimationEnd(Animator animation) {
|
|
|
|
|
+ if (!isShow) {
|
|
|
|
|
+ super.onAnimationEnd(animation);
|
|
|
|
|
+ myView.setVisibility(View.INVISIBLE);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // make the view visible and start the animation
|
|
|
|
|
+ if (isShow) {
|
|
|
|
|
+ myView.setVisibility(View.VISIBLE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // start the animation
|
|
|
|
|
+ anim.start();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+}
|