/// <summary> /// Scrolls the specified widget to the specified coordinates, except /// constrains the X scrolling position to the horizontal regions of /// the text that will be visible after scrolling to the specified /// Y position. /// </summary> /// <remarks> /// Scrolls the specified widget to the specified coordinates, except /// constrains the X scrolling position to the horizontal regions of /// the text that will be visible after scrolling to the specified /// Y position. /// </remarks> public static void scrollTo(android.widget.TextView widget, android.text.Layout layout , int x, int y) { int verticalPadding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom( ); int top = layout.getLineForVertical(y); int bottom = layout.getLineForVertical(y + widget.getHeight() - verticalPadding); int left = int.MaxValue; int right = 0; android.text.Layout.Alignment?a = layout.getParagraphAlignment(top); bool ltr = layout.getParagraphDirection(top) > 0; { for (int i = top; i <= bottom; i++) { left = (int)System.Math.Min(left, layout.getLineLeft(i)); right = (int)System.Math.Max(right, layout.getLineRight(i)); } } int hoizontalPadding = widget.getTotalPaddingLeft() + widget.getTotalPaddingRight (); int availableWidth = widget.getWidth() - hoizontalPadding; int actualWidth = right - left; if (actualWidth < availableWidth) { if (a == android.text.Layout.Alignment.ALIGN_CENTER) { x = left - ((availableWidth - actualWidth) / 2); } else { if ((ltr && (a == android.text.Layout.Alignment.ALIGN_OPPOSITE)) || (a == android.text.Layout.Alignment .ALIGN_RIGHT)) { // align_opposite does NOT mean align_right, we need the paragraph // direction to resolve it to left or right x = left - (availableWidth - actualWidth); } else { x = left; } } } else { x = System.Math.Min(x, right - availableWidth); x = System.Math.Max(x, left); } widget.scrollTo(x, y); }
/// <summary>Performs a scroll page up action.</summary> /// <remarks> /// Performs a scroll page up action. /// Scrolls up by one page. /// </remarks> /// <param name="widget">The text view.</param> /// <param name="buffer">The text buffer.</param> /// <returns>True if the event was handled.</returns> /// <hide></hide> protected internal virtual bool scrollPageUp(android.widget.TextView widget, android.text.Spannable buffer) { android.text.Layout layout = widget.getLayout(); int top_1 = widget.getScrollY() - getInnerHeight(widget); int topLine = layout.getLineForVertical(top_1); if (topLine >= 0) { android.text.method.Touch.scrollTo(widget, layout, widget.getScrollX(), layout.getLineTop (topLine)); return true; } return false; }
/// <summary>Performs a scroll page up action.</summary> /// <remarks> /// Performs a scroll page up action. /// Scrolls down by one page. /// </remarks> /// <param name="widget">The text view.</param> /// <param name="buffer">The text buffer.</param> /// <returns>True if the event was handled.</returns> /// <hide></hide> protected internal virtual bool scrollPageDown(android.widget.TextView widget, android.text.Spannable buffer) { android.text.Layout layout = widget.getLayout(); int innerHeight = getInnerHeight(widget); int bottom_1 = widget.getScrollY() + innerHeight + innerHeight; int bottomLine = layout.getLineForVertical(bottom_1); if (bottomLine <= layout.getLineCount() - 1) { android.text.method.Touch.scrollTo(widget, layout, widget.getScrollX(), layout.getLineTop (bottomLine + 1) - innerHeight); return true; } return false; }
/// <summary>Performs a scroll up action.</summary> /// <remarks> /// Performs a scroll up action. /// Scrolls up by the specified number of lines. /// </remarks> /// <param name="widget">The text view.</param> /// <param name="buffer">The text buffer.</param> /// <param name="amount">The number of lines to scroll by. Must be at least 1.</param> /// <returns>True if the event was handled.</returns> /// <hide></hide> protected internal virtual bool scrollUp(android.widget.TextView widget, android.text.Spannable buffer, int amount) { android.text.Layout layout = widget.getLayout(); int top_1 = widget.getScrollY(); int topLine = layout.getLineForVertical(top_1); if (layout.getLineTop(topLine) == top_1) { // If the top line is partially visible, bring it all the way // into view; otherwise, bring the previous line into view. topLine -= 1; } if (topLine >= 0) { topLine = System.Math.Max(topLine - amount + 1, 0); android.text.method.Touch.scrollTo(widget, layout, widget.getScrollX(), layout.getLineTop (topLine)); return true; } return false; }
/// <summary>Performs a scroll down action.</summary> /// <remarks> /// Performs a scroll down action. /// Scrolls down by the specified number of lines. /// </remarks> /// <param name="widget">The text view.</param> /// <param name="buffer">The text buffer.</param> /// <param name="amount">The number of lines to scroll by. Must be at least 1.</param> /// <returns>True if the event was handled.</returns> /// <hide></hide> protected internal virtual bool scrollDown(android.widget.TextView widget, android.text.Spannable buffer, int amount) { android.text.Layout layout = widget.getLayout(); int innerHeight = getInnerHeight(widget); int bottom_1 = widget.getScrollY() + innerHeight; int bottomLine = layout.getLineForVertical(bottom_1); if (layout.getLineTop(bottomLine + 1) < bottom_1 + 1) { // Less than a pixel of this line is out of view, // so we must have tried to make it entirely in view // and now want the next line to be in view instead. bottomLine += 1; } int limit = layout.getLineCount() - 1; if (bottomLine <= limit) { bottomLine = System.Math.Min(bottomLine + amount - 1, limit); android.text.method.Touch.scrollTo(widget, layout, widget.getScrollX(), layout.getLineTop (bottomLine + 1) - innerHeight); return true; } return false; }