diff --git a/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt index 4e33bdd51..09fd9454a 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt @@ -18,6 +18,7 @@ object ChapterProvider { content: String, chapterSize: Int ): TextChapter { val textPages = arrayListOf() + val pageLines = arrayListOf() val pageLengths = arrayListOf() var surplusText = content var pageIndex = 0 @@ -35,10 +36,12 @@ object ChapterProvider { } } textView.text = spannableStringBuilder - val lastCharNum = textView.getCharNum() + val lastLine = textView.getLineNum() + val lastCharNum = textView.getCharNum(lastLine) if (lastCharNum == 0) { break } else { + pageLines.add(lastLine) pageLengths.add(lastCharNum) textPages.add( TextPage( @@ -64,6 +67,7 @@ object ChapterProvider { bookChapter.title, bookChapter.url, textPages, + pageLines, pageLengths, chapterSize ) diff --git a/app/src/main/java/io/legado/app/ui/widget/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/widget/page/ContentTextView.kt index 0e7a7bea8..485895497 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/ContentTextView.kt @@ -16,8 +16,8 @@ class ContentTextView : AppCompatTextView { /** * 获取当前页总字数 */ - fun getCharNum(): Int { - return layout?.getLineEnd(getLineNum()) ?: 0 + fun getCharNum(lineNum: Int = getLineNum()): Int { + return layout?.getLineEnd(lineNum) ?: 0 } /** diff --git a/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt index fcec1bd5a..9383adb15 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt @@ -13,7 +13,14 @@ import io.legado.app.constant.AppConst.TIME_FORMAT import io.legado.app.help.ImageLoader import io.legado.app.help.ReadBookConfig import io.legado.app.utils.* -import kotlinx.android.synthetic.main.view_book_page.view.* +import kotlinx.android.synthetic.main.view_book_page.view.content_text_view +import kotlinx.android.synthetic.main.view_book_page.view.page_panel +import kotlinx.android.synthetic.main.view_book_page.view.top_bar +import kotlinx.android.synthetic.main.view_book_page.view.tv_bottom_left +import kotlinx.android.synthetic.main.view_book_page.view.tv_bottom_right +import kotlinx.android.synthetic.main.view_book_page.view.tv_top_left +import kotlinx.android.synthetic.main.view_book_page.view.tv_top_right +import kotlinx.android.synthetic.main.view_book_page_scroll.view.* import org.jetbrains.anko.matchParent import java.util.* @@ -111,4 +118,12 @@ class ContentView : FrameLayout { fun contentTextView(): ContentTextView? { return content_text_view } + + fun scrollTo(pos: Int?) { + if (pos != null) { + page_scroll_view?.post { + page_scroll_view?.scrollTo(0, content_text_view.layout.getLineTop(pos)) + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt index 77cb77bd6..7bf865314 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt @@ -34,10 +34,8 @@ class PageView(context: Context, attrs: AttributeSet) : addView(curPage) upBg() setWillNotDraw(false) - + pageFactory = TextPageFactory.create(this) upPageAnim() - this.pageFactory = TextPageFactory.create(this) - upContent() } override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { @@ -88,6 +86,7 @@ class PageView(context: Context, attrs: AttributeSet) : 2 -> ScrollPageDelegate(this) else -> NoAnimPageDelegate(this) } + upContent() } fun upContent() { @@ -96,6 +95,11 @@ class PageView(context: Context, attrs: AttributeSet) : curPage?.setContent(it.currentPage()) nextPage?.setContent(it.nextPage()) } + callback?.let { + if (isScrollDelegate()) { + curPage?.scrollTo(it.textChapter()?.getStartLine(it.durChapterPos())) + } + } } fun moveToPrevPage(noAnim: Boolean = true) { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt b/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt index d9eff7009..72377e3cd 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt @@ -7,6 +7,7 @@ data class TextChapter( val title: String, val url: String, val pages: List, + val pageLines: List, val pageLengths: List, val chaptersSize: Int ) { @@ -64,5 +65,16 @@ data class TextChapter( } return stringBuilder.toString() } + + fun getStartLine(pageIndex: Int): Int { + if (pageLines.size > pageIndex) { + var lines = 0 + for (index: Int in 0 until pageIndex) { + lines += pageLines[index] + 1 + } + return lines + } + return 0 + } }