public final class

TileRenderer

extends java.lang.Object

 java.lang.Object

↳androidx.wear.tiles.renderer.TileRenderer

Gradle dependencies

compile group: 'androidx.wear', name: 'wear-tiles-renderer', version: '1.0.0-alpha01'

  • groupId: androidx.wear
  • artifactId: wear-tiles-renderer
  • version: 1.0.0-alpha01

Artifact androidx.wear:wear-tiles-renderer:1.0.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

Renderer for Wear Tiles.

This variant uses Android views to represent the contents of the Wear Tile.

Summary

Constructors
publicTileRenderer(Context uiContext, LayoutElementBuilders.Layout layout, int tilesTheme, ResourceBuilders.Resources resources, java.util.concurrent.Executor loadActionExecutor, TileRenderer.LoadActionListener loadActionListener)

Default constructor.

publicTileRenderer(Context uiContext, LayoutElementBuilders.Layout layout, ResourceBuilders.Resources resources, java.util.concurrent.Executor loadActionExecutor, TileRenderer.LoadActionListener loadActionListener)

Default constructor.

Methods
public Viewinflate(ViewGroup parent)

Inflates a Tile into parent.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public TileRenderer(Context uiContext, LayoutElementBuilders.Layout layout, ResourceBuilders.Resources resources, java.util.concurrent.Executor loadActionExecutor, TileRenderer.LoadActionListener loadActionListener)

Default constructor.

Parameters:

uiContext: A suitable for interacting with the UI.
layout: The portion of the Tile to render.
resources: The resources for the Tile.
loadActionListener: Listener for clicks that will cause the contents to be reloaded.

public TileRenderer(Context uiContext, LayoutElementBuilders.Layout layout, int tilesTheme, ResourceBuilders.Resources resources, java.util.concurrent.Executor loadActionExecutor, TileRenderer.LoadActionListener loadActionListener)

Default constructor.

Parameters:

uiContext: A suitable for interacting with the UI.
layout: The portion of the Tile to render.
tilesTheme: The theme to use for this Tile instance. This can be used to customise things like the default font family. Pass 0 to use the default theme.
resources: The resources for the Tile.
loadActionListener: Listener for clicks that will cause the contents to be reloaded.

Methods

public View inflate(ViewGroup parent)

Inflates a Tile into parent.

Parameters:

parent: The view to attach the tile into.

Returns:

The first child that was inflated. This may be null if the proto is empty the top-level LayoutElement has no inner set, or the top-level LayoutElement contains an unsupported inner type.

Source

/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.wear.tiles.renderer;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.wear.tiles.LayoutElementBuilders;
import androidx.wear.tiles.ResourceBuilders;
import androidx.wear.tiles.StateBuilders;
import androidx.wear.tiles.renderer.internal.StandardResourceResolvers;
import androidx.wear.tiles.renderer.internal.TileRendererInternal;

import java.util.concurrent.Executor;

/**
 * Renderer for Wear Tiles.
 *
 * <p>This variant uses Android views to represent the contents of the Wear Tile.
 */
public final class TileRenderer {
    /**
     * Listener for clicks on Clickable objects that have an Action to (re)load the contents of a
     * tile.
     */
    public interface LoadActionListener {

        /**
         * Called when a Clickable that has a LoadAction is clicked.
         *
         * @param nextState The state that the next tile should be in.
         */
        void onClick(@NonNull StateBuilders.State nextState);
    }

    private final TileRendererInternal mRenderer;

    /**
     * Default constructor.
     *
     * @param uiContext A {@link Context} suitable for interacting with the UI.
     * @param layout The portion of the Tile to render.
     * @param resources The resources for the Tile.
     * @param loadActionListener Listener for clicks that will cause the contents to be reloaded.
     */
    public TileRenderer(
            @NonNull Context uiContext,
            @NonNull LayoutElementBuilders.Layout layout,
            @NonNull ResourceBuilders.Resources resources,
            @NonNull Executor loadActionExecutor,
            @NonNull LoadActionListener loadActionListener) {
        this(
                uiContext,
                layout,
                /* tilesTheme= */ 0,
                resources,
                loadActionExecutor,
                loadActionListener);
    }

    /**
     * Default constructor.
     *
     * @param uiContext A {@link Context} suitable for interacting with the UI.
     * @param layout The portion of the Tile to render.
     * @param tilesTheme The theme to use for this Tile instance. This can be used to customise
     *     things like the default font family. Pass 0 to use the default theme.
     * @param resources The resources for the Tile.
     * @param loadActionListener Listener for clicks that will cause the contents to be reloaded.
     */
    public TileRenderer(
            @NonNull Context uiContext,
            @NonNull LayoutElementBuilders.Layout layout,
            @StyleRes int tilesTheme,
            @NonNull ResourceBuilders.Resources resources,
            @NonNull Executor loadActionExecutor,
            @NonNull LoadActionListener loadActionListener) {
        this.mRenderer =
                new TileRendererInternal(
                        uiContext,
                        layout.toProto(),
                        StandardResourceResolvers.forLocalApp(resources.toProto(), uiContext)
                                .build(),
                        tilesTheme,
                        loadActionExecutor,
                        (s) -> loadActionListener.onClick(StateBuilders.State.fromProto(s)));
    }

    /**
     * Inflates a Tile into {@code parent}.
     *
     * @param parent The view to attach the tile into.
     * @return The first child that was inflated. This may be null if the proto is empty the
     *     top-level LayoutElement has no inner set, or the top-level LayoutElement contains an
     *     unsupported inner type.
     */
    @Nullable
    public View inflate(@NonNull ViewGroup parent) {
        return mRenderer.inflate(parent);
    }
}