我有一段长文本和一个固定大小的textView。如何分页显示文本?用户将以以下方式与程序交互:向左或向右滑动以切换到上一页或下一页。
目前,我创建了一个PageManager来处理这个任务。但是它的功能非常有限。处理文本的核心代码如下:
while (true) {
newLineIndex = TextUtils.indexOf(content, '\n', processIndex);
if (newLineIndex == -1) {// till the end of content
charCount = paint.breakText(content, processIndex, content.length(), false, leftLines * lineWidth, width);
} else {
charCount = paint.breakText(content, processIndex, ++newLineIndex, false, leftLines * lineWidth, width);
}
leftLines = (int) ((leftLines * lineWidth - width[0]) / lineWidth);
processIndex += charCount;
if (leftLines < 1 || processIndex >= content.length()) {
page = new Page();
page.endIndex = processIndex;
page.startIndex = pageBaseLine;
page.content = content.subSequence(page.startIndex, page.endIndex);
result.add(page);
pageBaseLine = processIndex;
leftLines = lineNumber;
}
if (processIndex >= content.length()) {
break;
}
}
限制是页面可能会截断文本,例如:
|A lon|
|g wor|
|d man|
// 一个长单词的人
或者由于换行导致的不正确的行:
// 页面管理器计算这个(2 行):
|a sentence with loooooooo|
|ooong word abcdefghijklmn|
//但实际上在文本视图中(3行):
|a sentence with |
|looooooooooong word |
|abcdefghijklmn |
所以最终的行数比计算的要多。所以我的页面管理器很愚蠢。有人能帮我吗?谢谢!