react-native-navigation的迁移库

SnackbarAndFabContainer.java 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.reactnativenavigation.views;
  2. import android.content.Context;
  3. import android.support.design.widget.CoordinatorLayout;
  4. import com.reactnativenavigation.events.Event;
  5. import com.reactnativenavigation.events.EventBus;
  6. import com.reactnativenavigation.events.FabSetEvent;
  7. import com.reactnativenavigation.events.ScreenChangedEvent;
  8. import com.reactnativenavigation.events.Subscriber;
  9. import com.reactnativenavigation.layouts.Layout;
  10. import com.reactnativenavigation.params.FabParams;
  11. import com.reactnativenavigation.params.SnackbarParams;
  12. public class SnackbarAndFabContainer extends CoordinatorLayout implements Snakbar.OnDismissListener, Subscriber{
  13. private Snakbar snakbar;
  14. private FloatingActionButtonCoordinator fabCoordinator;
  15. private Layout layout;
  16. public SnackbarAndFabContainer(Context context, Layout layout) {
  17. super(context);
  18. this.layout = layout;
  19. fabCoordinator = new FloatingActionButtonCoordinator(this);
  20. EventBus.instance.register(this);
  21. }
  22. public void showSnackbar(final String navigatorEventId, final SnackbarParams params) {
  23. snakbar = new Snakbar(this, navigatorEventId, params);
  24. snakbar.show();
  25. }
  26. @Override
  27. public void onDismiss(Snakbar snakbar) {
  28. if (this.snakbar == snakbar) {
  29. this.snakbar = null;
  30. }
  31. }
  32. public void destroy() {
  33. EventBus.instance.unregister(this);
  34. }
  35. @Override
  36. public void onEvent(Event event) {
  37. if (ScreenChangedEvent.TYPE.equals(event.getType())) {
  38. onScreenChange(((ScreenChangedEvent) event).fabParams);
  39. }
  40. if (FabSetEvent.TYPE.equals(event.getType())) {
  41. updateFab(((FabSetEvent) event).fabParams);
  42. }
  43. }
  44. private void onScreenChange(FabParams fabParams) {
  45. dismissSnackbar();
  46. updateFab(fabParams);
  47. }
  48. private void dismissSnackbar() {
  49. if (snakbar != null) {
  50. snakbar.dismiss();
  51. snakbar = null;
  52. }
  53. }
  54. private void updateFab(final FabParams fabParams) {
  55. fabCoordinator.remove(new Runnable() {
  56. @Override
  57. public void run() {
  58. if (fabParams != null) {
  59. if (layout.getCurrentScreen().getScreenInstanceId().equals(fabParams.screenInstanceId)) {
  60. fabCoordinator.add(fabParams);
  61. }
  62. }
  63. }
  64. });
  65. }
  66. }