Saltar al contenido
CrossFly es 100% open sourceSíguenos en GitHub
CrossFlyCrossFly
Empezar
Todos los ejemplos
RecetasListas113 líneas · 1 archivos

Listing: cards or table

A card answers "what is this one?"; a row answers "how do these compare?". Which one the reader needs changes during the day, so Listing lets them switch.

Qué muestra esto

The same items as cards or as a table, with the reader choosing — and a frozen first column.

Ejecutarlo en tu máquina

Cada ejemplo es una Window normal, así que el lanzador solo necesita su nombre.

gradlew lwjgl3:run --args="classname dev.cf.recipe.R05Listing"
Editarlo

Java se compila antes de ejecutarse, así que el navegador no puede reconstruir una edición por su cuenta: el código que ves aquí se convirtió en JavaScript con TeaVM antes de publicarse. Copia el archivo a un proyecto y funciona sin cambios en las cuatro plataformas; el comando de arriba es toda la preparación.

Ejecutándose aquíCrossFly 0.1.32
Iniciando la aplicación…

Compilado a JavaScript por TeaVM: es el mismo código, no un vídeo de él. · Todos los ejemplos

Qué usa

Tipos del framework que toca este ejemplo. Los enlazados tienen página de referencia.

CardComponentDataGridGridLayoutListingStylesVerticalLayoutWidgets

El código

Completo, sin recortes. Es el archivo, no un fragmento.

Crudo
dev/cf/recipe/R05Listing.java
package dev.cf.recipe;

import dev.cf.Tones;

import dev.crossfly.ui.*;
import dev.crossfly.ui.color.*;
import dev.crossfly.ui.graphics.*;
import dev.crossfly.ui.layout.*;
import dev.crossfly.ui.style.*;

import static dev.crossfly.ui.Widgets.*;

/**
 * The same items as cards or as a table, with the reader choosing — and a frozen first column.
 */
public class R05Listing extends Recipe
{
   // Brazilian company and city names, kept as sample data: proper names of a Brazilian client list
   // (iniciais() below also relies on "Casa do Norte" having a preposition in the middle).
   private static final String[] NOME ={ "Aurora Distribuidora", "Primavera Norte", "Vale Sul",
      "Casa do Norte", "Mercantil Boa Vista", "Atacado Serra", "Comercial Ipê" };
   private static final String[] CIDADE = { "Campinas", "Sumaré", "Indaiatuba", "Jundiaí",
      "Valinhos", "Vinhedo", "Itu" };
   private static final int[] VALOR = { 12400, 8300, 21750, 4120, 15990, 6480, 9310 };

   public String title()
   {
      return "Listing: cards or table";
   }

   public String note()
   {
      return "A card answers \"what is this one?\"; a row answers \"how do these compare?\". Which"
         + " one the reader needs changes during the day, so Listing lets them switch.";
   }

   public void build(Component body) throws Exception
   {
      Listing lista = new Listing().cardWidth(20f)
         .columns(1, 3)
         .column("nome", "Client", 13f)
         .column("cidade", "City", 9f)
         .column("valor", "Amount", 8f, Position.END)
         // The frozen column does not leave the view when the table scrolls horizontally — it is
         // what tells whose row you are reading out at the far right.
         .freeze(Position.START, "nome")
         .items(new Listing.Items()
         {
            public int count()
            {
               return NOME.length;
            }

            public Component card(int i) throws Exception
            {
               // Tile on the left, the three lines on the right. A column of name-over-city-over-
               // value is three greys with nothing for the eye to land on between rows; the tile
               // is one line of code and it is what makes the list scannable.
               Card c = new Card();
               c.setLayout(new GridLayout(Position.START, Position.CENTER));

               Component texto = new Component();
               texto.setLayout(new VerticalLayout(Position.START, Position.START));
               texto.add(LAY_AUTO, label(NOME[i], 1.2f, true, Styles.textColor, Position.START));
               texto.add(LAY_AUTO, label(CIDADE[i], 1f, false, Styles.captionColor,
                  Position.START));
               texto.add(LAY_AUTO, outgap(label(valor(i), 1.1f, true, Tones.accent(),
                  Position.START), 0, .15f, 0, 0));

               c.add(LAY_AUTO, 1f, Tones.tile(iniciais(i), Tones.cycle(i)),
                  outgap(texto, .6f, 0, 0, 0));
               return c;
            }

            public void row(DataGrid.Row r, int i) throws Exception
            {
               r.cell(0, label(NOME[i], 1f, true, Styles.textColor, Position.START));
               r.cell(1, label(CIDADE[i], 1f, false, Styles.captionColor, Position.START));
               // The amount is what the table exists to compare, so it is the one coloured cell.
               r.cell(2, label(valor(i), 1f, true, Tones.accent(), Position.END));
            }
         });
      lista.testTag("recipe.listing");

      // The switch has to be placed by the SCREEN. Listing offers modeButton() and never draws it
      // itself, because a listing usually sits under a toolbar that already has a count and a sort
      // control, and a bar of its own would be a second one. Leave this line out and the recipe
      // shows cards with no way to reach the table — which is what it did until somebody looked.
      Component barra = new Component();
      barra.setLayout(new GridLayout(Position.START, Position.CENTER));
      barra.add(1f, 0f, label("7 clients", 1f, false, Styles.captionColor, Position.START),
         lista.modeButton());
      body.add(LAY_AUTO, outgap(barra, 0, 0, 0, .5f));

      body.add(LAY_FILL, lista);
   }

   /** The first letter of each of the first two words — "Aurora Distribuidora" becomes AD. */
   private static String iniciais(int i)
   {
      String[] p = NOME[i].split(" ");
      // Upper case both, or "Casa do Norte" answers "Cd" — the second word is a preposition and
      // the tile ends up shouting the one letter that carries no information.
      return p.length > 1 ? ("" + p[0].charAt(0) + p[p.length - 1].charAt(0)).toUpperCase()
         : p[0].substring(0, 2).toUpperCase();
   }

   private static String valor(int i)
   {
      return "R$ " + VALOR[i] / 100 + "," + (VALOR[i] % 100 < 10 ? "0" : "") + VALOR[i] % 100;
   }
}
Para una IA

Versiones de esta página legibles por máquina, para que un modelo lea el estante en vez de adivinar la API.

Este ejemplo, en JSONEste ejemplo, en Markdown
AnteriorBottom sheet with a fixed footerSiguienteChoosing from a list