react-native-navigation的迁移库

Snakbar.java 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.reactnativenavigation.views;
  2. import android.support.design.widget.Snackbar;
  3. import android.view.View;
  4. import com.reactnativenavigation.NavigationApplication;
  5. import com.reactnativenavigation.params.SnackbarParams;
  6. class Snakbar {
  7. private final OnDismissListener parent;
  8. private final String navigatorEventId;
  9. private final SnackbarParams params;
  10. private Snackbar snackbar;
  11. interface OnDismissListener {
  12. void onDismiss(Snakbar snakbar);
  13. }
  14. public void show() {
  15. snackbar.show();
  16. }
  17. void dismiss() {
  18. snackbar.dismiss();
  19. }
  20. public View getView() {
  21. return snackbar.getView();
  22. }
  23. Snakbar(OnDismissListener parent, String navigatorEventId, SnackbarParams params) {
  24. this.parent = parent;
  25. this.navigatorEventId = navigatorEventId;
  26. this.params = params;
  27. create();
  28. }
  29. private void create() {
  30. snackbar = Snackbar.make((View) parent, params.text, params.duration);
  31. setAction(navigatorEventId, params, snackbar);
  32. setStyle(snackbar, params);
  33. setOnDismissListener();
  34. }
  35. private void setAction(final String navigatorEventId, final SnackbarParams params, Snackbar snackbar) {
  36. if (params.eventId != null) {
  37. snackbar.setAction(params.buttonText, new View.OnClickListener() {
  38. @Override
  39. public void onClick(View v) {
  40. NavigationApplication.instance.getEventEmitter().sendNavigatorEvent(params.eventId, navigatorEventId);
  41. }
  42. });
  43. }
  44. }
  45. private void setStyle(Snackbar snackbar, SnackbarParams params) {
  46. if (params.buttonColor.hasColor()) {
  47. snackbar.setActionTextColor(params.buttonColor.getColor());
  48. }
  49. }
  50. private void setOnDismissListener() {
  51. snackbar.getView().addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
  52. @Override
  53. public void onViewAttachedToWindow(View v) {
  54. }
  55. @Override
  56. public void onViewDetachedFromWindow(View v) {
  57. parent.onDismiss(Snakbar.this);
  58. }
  59. });
  60. }
  61. }