diff --git a/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt b/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt index 6c43b1dee..fabb55a06 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt @@ -1,7 +1,7 @@ package io.legado.app.ui.widget.page interface DataSource { - fun isScroll(): Boolean + fun isScrollDelegate(): Boolean fun pageIndex(): Int 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 85d7be3d1..77cb77bd6 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 @@ -11,8 +11,10 @@ import io.legado.app.ui.widget.page.delegate.* import io.legado.app.utils.activity import io.legado.app.utils.getPrefInt -class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs), - PageDelegate.PageInterface { +class PageView(context: Context, attrs: AttributeSet) : + FrameLayout(context, attrs), + PageDelegate.PageInterface, + DataSource { private var callback: CallBack? = null private var pageDelegate: PageDelegate? = null @@ -34,62 +36,8 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att setWillNotDraw(false) upPageAnim() - - setPageFactory(TextPageFactory.create(object : DataSource { - override fun isScroll(): Boolean { - return pageDelegate is ScrollPageDelegate - } - - override fun pageIndex(): Int { - return callback?.durChapterPos() ?: 0 - } - - override fun setPageIndex(pageIndex: Int) { - callback?.setPageIndex(pageIndex) - } - - override fun getChapterPosition(): Int { - return callback?.durChapterIndex() ?: 0 - } - - override fun getChapter(position: Int): TextChapter? { - return callback?.textChapter(position) - } - - override fun getCurrentChapter(): TextChapter? { - return callback?.textChapter(0) - } - - override fun getNextChapter(): TextChapter? { - return callback?.textChapter(1) - } - - override fun getPreviousChapter(): TextChapter? { - return callback?.textChapter(-1) - } - - override fun hasNextChapter(): Boolean { - callback?.let { - return it.durChapterIndex() < it.chapterSize() - 1 - } - return false - } - - override fun hasPrevChapter(): Boolean { - callback?.let { - return it.durChapterIndex() > 0 - } - return false - } - - override fun moveToNextChapter() { - callback?.moveToNextChapter() - } - - override fun moveToPrevChapter() { - callback?.moveToPrevChapter() - } - })) + this.pageFactory = TextPageFactory.create(this) + upContent() } override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { @@ -132,14 +80,8 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att upContent() } - private fun setPageFactory(factory: TextPageFactory) { - this.pageFactory = factory - - //可做成异步回调 - upContent() - } - fun upPageAnim() { + pageDelegate = null pageDelegate = when (context.getPrefInt("pageAnim")) { 0 -> CoverPageDelegate(this) 1 -> SlidePageDelegate(this) @@ -207,6 +149,60 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att callback?.clickCenter() } + override fun isScrollDelegate(): Boolean { + return pageDelegate is ScrollPageDelegate + } + + override fun pageIndex(): Int { + return callback?.durChapterPos() ?: 0 + } + + override fun setPageIndex(pageIndex: Int) { + callback?.setPageIndex(pageIndex) + } + + override fun getChapterPosition(): Int { + return callback?.durChapterIndex() ?: 0 + } + + override fun getChapter(position: Int): TextChapter? { + return callback?.textChapter(position) + } + + override fun getCurrentChapter(): TextChapter? { + return callback?.textChapter(0) + } + + override fun getNextChapter(): TextChapter? { + return callback?.textChapter(1) + } + + override fun getPreviousChapter(): TextChapter? { + return callback?.textChapter(-1) + } + + override fun hasNextChapter(): Boolean { + callback?.let { + return it.durChapterIndex() < it.chapterSize() - 1 + } + return false + } + + override fun hasPrevChapter(): Boolean { + callback?.let { + return it.durChapterIndex() > 0 + } + return false + } + + override fun moveToNextChapter() { + callback?.moveToNextChapter() + } + + override fun moveToPrevChapter() { + callback?.moveToPrevChapter() + } + interface CallBack { fun chapterSize(): Int fun durChapterIndex(): Int diff --git a/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt b/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt index b0eee5632..6882dcae0 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt @@ -62,7 +62,7 @@ class TextPageFactory private constructor(dataSource: DataSource) : } override fun currentPage(): TextPage? = dataSource.pageIndex().let { index -> - return if (dataSource.isScroll()) { + return if (dataSource.isScrollDelegate()) { dataSource.getCurrentChapter()?.scrollPage() } else { dataSource.getCurrentChapter()?.page(index) @@ -70,7 +70,7 @@ class TextPageFactory private constructor(dataSource: DataSource) : } override fun nextPage(): TextPage? = dataSource.pageIndex().let { index -> - if (dataSource.isScroll()) { + if (dataSource.isScrollDelegate()) { return dataSource.getNextChapter()?.scrollPage() ?: TextPage(index = index + 1, title = "index:${index + 1}") } @@ -85,7 +85,7 @@ class TextPageFactory private constructor(dataSource: DataSource) : } override fun previousPage(): TextPage? = dataSource.pageIndex().let { index -> - if (dataSource.isScroll()) { + if (dataSource.isScrollDelegate()) { return dataSource.getPreviousChapter()?.scrollPage() ?: TextPage(index = index + 1, title = "index:${index + 1}") } diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt index ce4d4beab..00dc408e0 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt @@ -38,9 +38,19 @@ abstract class PageDelegate(protected val pageView: PageView) { protected var viewWidth: Int = pageView.width protected var viewHeight: Int = pageView.height - private val scroller: Scroller by lazy { Scroller(pageView.context, FastOutLinearInInterpolator()) } + private val scroller: Scroller by lazy { + Scroller( + pageView.context, + FastOutLinearInInterpolator() + ) + } - private val detector: GestureDetector by lazy { GestureDetector(pageView.context, GestureListener()) } + private val detector: GestureDetector by lazy { + GestureDetector( + pageView.context, + GestureListener() + ) + } private var isMoved = false private var noNext = true @@ -77,7 +87,13 @@ abstract class PageDelegate(protected val pageView: PageView) { } protected fun startScroll(startX: Int, startY: Int, dx: Int, dy: Int) { - scroller.startScroll(startX, startY, dx, dy, getDuration((if(dx !=0) dx else dy).toFloat())) + scroller.startScroll( + startX, + startY, + dx, + dy, + getDuration((if (dx != 0) dx else dy).toFloat()) + ) isRunning = true isStarted = true invalidate() @@ -243,8 +259,18 @@ abstract class PageDelegate(protected val pageView: PageView) { isLongPress = true } - override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float): Boolean { - curPage?.contentTextView()?.setTextIsSelectable(false) + override fun onScroll( + e1: MotionEvent, + e2: MotionEvent, + distanceX: Float, + distanceY: Float + ): Boolean { + if (pageView.isScrollDelegate()) { + curPage?.dispatchTouchEvent(e2) + return true + } else { + curPage?.contentTextView()?.setTextIsSelectable(false) + } if (!isMoved && abs(distanceX) > abs(distanceY)) { if (distanceX < 0) { //上一页的参数配置 diff --git a/app/src/main/res/layout/view_book_page.xml b/app/src/main/res/layout/view_book_page.xml index eb27614ad..59784756f 100644 --- a/app/src/main/res/layout/view_book_page.xml +++ b/app/src/main/res/layout/view_book_page.xml @@ -32,8 +32,7 @@ android:id="@+id/content_text_view" android:layout_width="match_parent" android:layout_height="0dp" - android:layout_weight="1" - android:textIsSelectable="true" /> + android:layout_weight="1" />