示例#1
0
  /**
   * Set the text for the corresponding views.
   *
   * @param blocks list of all blocks to be set
   * @param subreddit
   */
  public void setViews(List<String> blocks, String subreddit) {
    removeAllViews();

    Context context = getContext();

    if (!blocks.isEmpty()) {
      setVisibility(View.VISIBLE);
    }

    for (String block : blocks) {
      if (block.startsWith("<table>")) {
        HorizontalScrollView scrollView = new HorizontalScrollView(context);
        scrollView.setScrollbarFadingEnabled(false);
        TableLayout table = formatTable(block, subreddit);
        scrollView.setLayoutParams(MARGIN_PARAMS);
        table.setPaddingRelative(0, 0, 0, Reddit.pxToDp(10, context));
        scrollView.addView(table);
        addView(scrollView);
      } else if (block.startsWith("<pre>")) {
        HorizontalScrollView scrollView = new HorizontalScrollView(context);
        scrollView.setScrollbarFadingEnabled(false);
        SpoilerRobotoTextView newTextView = new SpoilerRobotoTextView(context);
        newTextView.setTextHtml(block, subreddit);
        setStyle(newTextView, subreddit);
        scrollView.setLayoutParams(MARGIN_PARAMS);
        newTextView.setPaddingRelative(0, 0, 0, Reddit.pxToDp(10, context));
        scrollView.addView(newTextView);
        addView(scrollView);
      } else {
        SpoilerRobotoTextView newTextView = new SpoilerRobotoTextView(context);
        newTextView.setTextHtml(block, subreddit);
        setStyle(newTextView, subreddit);
        newTextView.setLayoutParams(MARGIN_PARAMS);
        addView(newTextView);
      }
    }
  }
示例#2
0
  private TableLayout formatTable(String text, String subreddit) {
    TableRow.LayoutParams rowParams =
        new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);

    Context context = getContext();
    TableLayout table = new TableLayout(context);
    TableLayout.LayoutParams params =
        new TableLayout.LayoutParams(
            TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
    table.setLayoutParams(params);

    final String tableStart = "<table>";
    final String tableEnd = "</table>";
    final String tableHeadStart = "<thead>";
    final String tableHeadEnd = "</thead>";
    final String tableRowStart = "<tr>";
    final String tableRowEnd = "</tr>";
    final String tableColumnStart = "<td>";
    final String tableColumnEnd = "</td>";
    final String tableHeaderStart = "<th>";
    final String tableHeaderEnd = "</th>";

    int i = 0;
    int columnStart = 0;
    int columnEnd;
    TableRow row = null;
    while (i < text.length()) {
      if (text.charAt(i) != '<') { // quick check otherwise it falls through to else
        i += 1;
      } else if (text.subSequence(i, i + tableStart.length()).toString().equals(tableStart)) {
        i += tableStart.length();
      } else if (text.subSequence(i, i + tableHeadStart.length())
          .toString()
          .equals(tableHeadStart)) {
        i += tableHeadStart.length();
      } else if (text.subSequence(i, i + tableRowStart.length()).toString().equals(tableRowStart)) {
        row = new TableRow(context);
        row.setLayoutParams(rowParams);
        i += tableRowStart.length();
      } else if (text.subSequence(i, i + tableRowEnd.length()).toString().equals(tableRowEnd)) {
        table.addView(row);
        i += tableRowEnd.length();
      } else if (text.subSequence(i, i + tableEnd.length()).toString().equals(tableEnd)) {
        i += tableEnd.length();
      } else if (text.subSequence(i, i + tableHeadEnd.length()).toString().equals(tableHeadEnd)) {
        i += tableHeadEnd.length();
      } else if (text.subSequence(i, i + tableColumnStart.length())
              .toString()
              .equals(tableColumnStart)
          || text.subSequence(i, i + tableHeaderStart.length())
              .toString()
              .equals(tableHeaderStart)) {
        i += tableColumnStart.length();
        columnStart = i;
      } else if (text.subSequence(i, i + tableColumnEnd.length()).toString().equals(tableColumnEnd)
          || text.subSequence(i, i + tableHeaderEnd.length()).toString().equals(tableHeaderEnd)) {
        columnEnd = i;

        SpoilerRobotoTextView textView = new SpoilerRobotoTextView(context);
        textView.setTextHtml(text.subSequence(columnStart, columnEnd), subreddit);
        setStyle(textView, subreddit);
        textView.setLayoutParams(COLUMN_PARAMS);

        row.addView(textView);

        columnStart = 0;
        i += tableColumnEnd.length();
      } else {
        i += 1;
      }
    }

    return table;
  }