react-native-navigation的迁移库

TitleBarSearchButton.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.reactnativenavigation.views;
  2. import android.app.Activity;
  3. import android.graphics.drawable.Drawable;
  4. import android.support.annotation.Nullable;
  5. import android.support.v4.view.MenuItemCompat;
  6. import android.support.v7.widget.SearchView;
  7. import android.support.v7.widget.Toolbar;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.EditText;
  13. import android.widget.ImageButton;
  14. import android.widget.ImageView;
  15. import com.facebook.react.bridge.Arguments;
  16. import com.facebook.react.bridge.WritableMap;
  17. import com.reactnativenavigation.NavigationApplication;
  18. import com.reactnativenavigation.R;
  19. import com.reactnativenavigation.params.TitleBarButtonParams;
  20. import com.reactnativenavigation.utils.ReflectionUtils;
  21. import com.reactnativenavigation.utils.ViewUtils;
  22. class TitleBarSearchButton extends TitleBarButton implements SearchView.OnQueryTextListener, View.OnFocusChangeListener, View.OnClickListener {
  23. static final String BUTTON_ID = "searchView";
  24. private SearchView searchView;
  25. TitleBarSearchButton(Menu menu, View parent, TitleBarButtonParams buttonParams, @Nullable String navigatorEventId) {
  26. super(menu, parent, buttonParams, navigatorEventId);
  27. }
  28. MenuItem addToMenu(int index) {
  29. ((Activity) parent.getContext()).getMenuInflater().inflate(R.menu.search_item, menu);
  30. MenuItem item = menu.findItem(R.id.toolbar_action_search);
  31. item.setOnMenuItemClickListener(this);
  32. if (buttonParams.icon != null) {
  33. item.setIcon(buttonParams.icon);
  34. }
  35. searchView = (SearchView) MenuItemCompat.getActionView(item);
  36. searchView.setQueryHint(buttonParams.hint);
  37. searchView.setOnQueryTextFocusChangeListener(this);
  38. searchView.setOnQueryTextListener(this);
  39. searchView.setOnSearchClickListener(this);
  40. setColor();
  41. return item;
  42. }
  43. private void setColor() {
  44. EditText searchEditText = ViewUtils.findChildByClass(searchView, EditText.class);
  45. if (searchEditText != null) {
  46. if (buttonParams.color.hasColor()) {
  47. searchEditText.setTextColor(buttonParams.color.getColor());
  48. searchEditText.setHintTextColor(buttonParams.color.getColor());
  49. }
  50. colorCloseButton(searchEditText);
  51. setImagePlateColor();
  52. }
  53. }
  54. private void colorCloseButton(EditText searchEditText) {
  55. ViewUtils.performOnChildren((ViewGroup) searchEditText.getParent(), new ViewUtils.PerformOnViewTask() {
  56. @Override
  57. public void runOnView(View view) {
  58. if (view instanceof ImageView) {
  59. if (buttonParams.color.hasColor()) {
  60. ((ImageView) view).setColorFilter(buttonParams.color.getColor());
  61. }
  62. }
  63. }
  64. });
  65. }
  66. private void setImagePlateColor() {
  67. if (buttonParams.color.hasColor()) {
  68. Object mSearchPlate = ReflectionUtils.getDeclaredField(searchView, "mSearchPlate");
  69. if (mSearchPlate != null) {
  70. Drawable background = ((View) mSearchPlate).getBackground();
  71. if (background != null) {
  72. ViewUtils.tintDrawable(background, buttonParams.color.getColor(), true);
  73. }
  74. }
  75. }
  76. }
  77. @Override
  78. public boolean onMenuItemClick(MenuItem item) {
  79. setupBackButtonAfterSearchViewIsExpended();
  80. return false;
  81. }
  82. private void setupBackButtonAfterSearchViewIsExpended() {
  83. ViewUtils.runOnPreDraw(searchView, new Runnable() {
  84. @Override
  85. public void run() {
  86. Object backButton = ViewUtils.findChildByClass((ViewGroup) searchView.getParent(), ImageButton.class);
  87. if (backButton != null) {
  88. setBackButtonClickListener((View) backButton);
  89. colorBackButton((ImageView) backButton);
  90. }
  91. }
  92. private void colorBackButton(ImageView backButton) {
  93. if (buttonParams.color.hasColor()) {
  94. ViewUtils.tintDrawable(backButton.getDrawable(), buttonParams.color.getColor(), true);
  95. }
  96. }
  97. private void setBackButtonClickListener(View backButton) {
  98. backButton.setOnClickListener(new View.OnClickListener() {
  99. @Override
  100. public void onClick(View v) {
  101. ((Toolbar) searchView.getParent()).collapseActionView();
  102. sendEvent("searchViewHidden");
  103. }
  104. });
  105. }
  106. });
  107. }
  108. @Override
  109. public boolean onQueryTextSubmit(String query) {
  110. WritableMap arguments = Arguments.createMap();
  111. arguments.putString("query", query);
  112. sendEvent("searchQuerySubmit", arguments);
  113. return false;
  114. }
  115. @Override
  116. public boolean onQueryTextChange(String newText) {
  117. WritableMap arguments = Arguments.createMap();
  118. arguments.putString("query", newText);
  119. sendEvent("searchQueryChange", arguments);
  120. return false;
  121. }
  122. @Override
  123. public void onFocusChange(View v, boolean hasFocus) {
  124. WritableMap arguments = Arguments.createMap();
  125. arguments.putBoolean("hasFocus", hasFocus);
  126. sendEvent("searchFocusChange", arguments);
  127. }
  128. @Override
  129. public void onClick(View v) {
  130. sendEvent("searchViewShown");
  131. }
  132. private void sendEvent(String eventId, WritableMap arguments) {
  133. NavigationApplication.instance.getEventEmitter().sendNavigatorEvent(eventId, navigatorEventId, arguments);
  134. }
  135. private void sendEvent(String eventId) {
  136. NavigationApplication.instance.getEventEmitter().sendNavigatorEvent(eventId, navigatorEventId);
  137. }
  138. }