Square Coordinators

repository·master·Indexed 20 days ago

https://github.com/square/coordinators

A lightweight Android library providing a lifecycle management pattern for UI components. It allows developers to attach and detach logic from Views using the Coordinator class and its attach and detach methods, with support for manual binding via Coordinators.bind or automatic binding via Coordinators.installBinder.

Tokens
584
Snippets
3
Records
3
Agent score
22%

What's inside Coordinators

  1. Implement a Coordinator to manage lifecycle

    master

    A Coordinator provides a simple lifecycle for managing components (like 'MVWhatever' patterns) on Android. To use it, extend the Coordinator class and override the attach and detach methods:

    • attach(View view): Use this to attach listeners, load state, or perform initialization when the view becomes active.
    • detach(View view): Use this to unbind listeners, save state, or perform cleanup when the view is removed.
    class ExampleCoordinator extends Coordinator {
    
      @Override public void attach(View view) {
        // Attach listeners, load state, whatever.
      }
    
      @Override public void detach(View view) {
        // Unbind listeners, save state, go nuts.
      }
    }
  2. Bind a Coordinator to a View

    master

    To connect a Coordinator to an Android View, use the Coordinators.bind method. This requires a CoordinatorProvider, which is a functional interface (or lambda) that takes a View and returns a Coordinator (can be @Nullable).

    Use Coordinators.bind(view, coordinatorProvider) to bind a specific view, or Coordinators.installBinder(viewGroup, coordinatorProvider) to automatically bind any child view added to a ViewGroup using the provided provider.

    // Create a factory for your Coordinators.
    CoordinatorProvider coordinatorProvider; // @Nullable (View) -> Coordinator
    
    // Bind a Coordinator to a View.
    Coordinators.bind(view, coordinatorProvider);
    
    // Bind a Coordinator to any child View added to a group.
    Coordinators.installBinder(viewGroup, coordinatorProvider);