Java Code Examples for com.facebook.litho.LithoView#layout()

The following examples show how to use com.facebook.litho.LithoView#layout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComponentTestHelper.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * Mount a component tree into a component view.
 *
 * @param lithoView The view to mount the component tree into
 * @param componentTree The component tree to mount
 * @param widthSpec The width spec used to measure the resulting view
 * @param heightSpec The height spec used to measure the resulting view
 * @return A LithoView with the component tree mounted in it.
 */
public static LithoView mountComponent(
    LithoView lithoView, ComponentTree componentTree, int widthSpec, int heightSpec) {
  final boolean addParent = lithoView.getParent() == null;
  final ViewGroup parent =
      new ViewGroup(lithoView.getContext()) {
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {}
      };

  if (addParent) {
    parent.addView(lithoView);
  }

  lithoView.setComponentTree(componentTree);

  try {
    Whitebox.invokeMethod(lithoView, "onAttach");
  } catch (Exception e) {
    throw new RuntimeException(e);
  }

  lithoView.measure(widthSpec, heightSpec);

  if (addParent) {
    parent.layout(0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight());
  }

  lithoView.layout(0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight());

  return lithoView;
}
 
Example 2
Source File: AnimationTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void
    animationTransitionsExtension_reUsingLithoViewWithDifferentComponentTrees_shouldNotCrash() {
  final boolean useExtensionsWithMountDelegate =
      ComponentsConfiguration.useExtensionsWithMountDelegate;
  ComponentsConfiguration.useExtensionsWithMountDelegate = true;

  ComponentContext componentContext = new ComponentContext(RuntimeEnvironment.application);

  mLithoViewRule.setRoot(getNonAnimatingComponent());
  // We measure and layout this non animating component to initialize the transition extension.
  mLithoViewRule.measure().layout();

  // We need an other litho view where we are going to measure and layout an other similar tree
  // (the real difference here is that the root components are not animating)
  LithoView lithoView = new LithoView(componentContext);
  ComponentTree nonAnimatingComponentTree = ComponentTree.create(componentContext).build();
  nonAnimatingComponentTree.setRoot(getNonAnimatingComponent());
  lithoView.setComponentTree(nonAnimatingComponentTree);
  lithoView.measure(LithoViewRule.DEFAULT_WIDTH_SPEC, LithoViewRule.DEFAULT_HEIGHT_SPEC);
  lithoView.layout(0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight());

  // Now we need a new component tree that will hold a component tree that holds an animating root
  // component.
  ComponentTree animatingComponentTree = ComponentTree.create(componentContext).build();
  animatingComponentTree.setRoot(getAnimatingXPropertyComponent());

  mLithoViewRule.useComponentTree(animatingComponentTree);
  // We measure this component tree so we initialize the mRootTransition in the extension, but we
  // end up not running a layout here.
  mLithoViewRule.measure();

  // Finally we set a new animating component tree to the initial litho view and run measure and
  // layout.
  mLithoViewRule.useComponentTree(nonAnimatingComponentTree);
  mLithoViewRule.measure().layout();
  // Should not crash.
  ComponentsConfiguration.useExtensionsWithMountDelegate = useExtensionsWithMountDelegate;
}
 
Example 3
Source File: LithoViewRule.java    From litho with Apache License 2.0 4 votes vote down vote up
public LithoViewRule layout() {
  final LithoView lithoView = getLithoView();
  lithoView.layout(0, 0, lithoView.getMeasuredWidth(), lithoView.getMeasuredHeight());
  return this;
}