From 4adff555b947f6a3652de6b0cd19d905924792bd Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 10 Jul 2021 11:17:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 2 +- .../app/ui/book/read/ReadBookActivity.kt | 11 ++- .../legado/app/ui/book/read/TextActionMenu.kt | 83 +++++++++++++++---- app/src/main/res/layout/popup_action_menu.xml | 47 +++++++++-- .../main/res/menu/content_select_action.xml | 12 ++- build.gradle | 2 + 6 files changed, 123 insertions(+), 34 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d359250e0..4542baf8e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -123,7 +123,7 @@ dependencies { //kotlin implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" //协程 - def coroutines_version = '1.5.0' + def coroutines_version = '1.5.1' implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index d0f138e1f..ce1f7e710 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -105,12 +105,7 @@ class ReadBookActivity : ReadBookBaseActivity(), } private var menu: Menu? = null private val textActionMenu: TextActionMenu by lazy { - TextActionMenu(this, this).apply { - contentView.measure( - View.MeasureSpec.UNSPECIFIED, - View.MeasureSpec.UNSPECIFIED - ) - } + TextActionMenu(this, this) } override val scope: CoroutineScope get() = this @@ -474,6 +469,10 @@ class ReadBookActivity : ReadBookBaseActivity(), * 显示文本操作菜单 */ override fun showTextActionMenu() = binding.run { + textActionMenu.contentView.measure( + View.MeasureSpec.UNSPECIFIED, + View.MeasureSpec.UNSPECIFIED + ) val popupHeight = textActionMenu.contentView.measuredHeight val x = textMenuPosition.x.toInt() var y = textMenuPosition.y.toInt() - popupHeight diff --git a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt index 906fc8ebc..75f8e30b0 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt @@ -17,25 +17,24 @@ import androidx.annotation.RequiresApi import androidx.appcompat.view.SupportMenuInflater import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.view.menu.MenuItemImpl +import androidx.core.view.isVisible import io.legado.app.R +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.databinding.ItemTextBinding import io.legado.app.databinding.PopupActionMenuBinding import io.legado.app.service.BaseReadAloudService -import io.legado.app.utils.isAbsUrl -import io.legado.app.utils.sendToClip -import io.legado.app.utils.share -import io.legado.app.utils.toastOnUi -import splitties.views.onClick +import io.legado.app.utils.* import java.util.* @SuppressLint("RestrictedApi") class TextActionMenu(private val context: Context, private val callBack: CallBack) : PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT), TextToSpeech.OnInitListener { - private val binding = PopupActionMenuBinding.inflate(LayoutInflater.from(context)) - private val menu = MenuBuilder(context) - private val moreMenu = MenuBuilder(context) + private val adapter = Adapter(context) + private val visibleMenuItems: List + private val moreMenuItems: List private val ttsListener by lazy { TTSUtteranceListener() } @@ -48,23 +47,71 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac isOutsideTouchable = false isFocusable = false - SupportMenuInflater(context).inflate(R.menu.content_select_action, menu) + val myMenu = MenuBuilder(context) + val otherMenu = MenuBuilder(context) + SupportMenuInflater(context).inflate(R.menu.content_select_action, myMenu) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - onInitializeMenu(moreMenu) + onInitializeMenu(otherMenu) + } + visibleMenuItems = myMenu.visibleItems.subList(0, 5) + moreMenuItems = + myMenu.visibleItems.subList(5, myMenu.visibleItems.lastIndex) + otherMenu.visibleItems + initRecyclerView() + setOnDismissListener { + binding.ivMenuMore.setImageResource(R.drawable.ic_more_vert) + binding.recyclerViewMore.gone() + adapter.setItems(visibleMenuItems) + binding.recyclerView.visible() + } + } + + private fun initRecyclerView() = with(binding) { + recyclerView.adapter = adapter + recyclerViewMore.adapter = adapter + adapter.setItems(visibleMenuItems) + ivMenuMore.visible() + ivMenuMore.setOnClickListener { + if (recyclerView.isVisible) { + ivMenuMore.setImageResource(R.drawable.ic_arrow_back) + adapter.setItems(moreMenuItems) + recyclerView.gone() + recyclerViewMore.visible() + } else { + ivMenuMore.setImageResource(R.drawable.ic_more_vert) + recyclerViewMore.gone() + adapter.setItems(visibleMenuItems) + recyclerView.visible() + } + } + } + + inner class Adapter(context: Context) : + RecyclerAdapter(context) { + + override fun getViewBinding(parent: ViewGroup): ItemTextBinding { + return ItemTextBinding.inflate(inflater, parent, false) } - val menuItems = menu.visibleItems + moreMenu.visibleItems - menuItems.forEach { - val textView = ItemTextBinding.inflate(LayoutInflater.from(context)).root.apply { - tag = it - text = it.title - onClick { + + override fun convert( + holder: ItemViewHolder, + binding: ItemTextBinding, + item: MenuItemImpl, + payloads: MutableList + ) { + with(binding) { + textView.text = item.title + } + } + + override fun registerListener(holder: ItemViewHolder, binding: ItemTextBinding) { + holder.itemView.setOnClickListener { + getItem(holder.layoutPosition)?.let { if (!callBack.onMenuItemSelected(it.itemId)) { onMenuItemSelected(it) } - callBack.onMenuActionFinally() } + callBack.onMenuActionFinally() } - binding.root.addView(textView) } } diff --git a/app/src/main/res/layout/popup_action_menu.xml b/app/src/main/res/layout/popup_action_menu.xml index 74b43c301..ab9a681b8 100644 --- a/app/src/main/res/layout/popup_action_menu.xml +++ b/app/src/main/res/layout/popup_action_menu.xml @@ -1,9 +1,46 @@ - \ No newline at end of file + android:orientation="vertical"> + + + + + + + + + + + + + diff --git a/app/src/main/res/menu/content_select_action.xml b/app/src/main/res/menu/content_select_action.xml index 63a4bb184..dda5324dd 100644 --- a/app/src/main/res/menu/content_select_action.xml +++ b/app/src/main/res/menu/content_select_action.xml @@ -1,5 +1,6 @@ - + + android:title="@string/search_content" + app:showAsAction="never" /> + android:title="@string/browser" + app:showAsAction="never" /> + android:title="@string/share" + app:showAsAction="never" /> \ No newline at end of file diff --git a/build.gradle b/build.gradle index 3663614be..4e78684d5 100644 --- a/build.gradle +++ b/build.gradle @@ -4,6 +4,7 @@ buildscript { ext.kotlin_version = '1.5.20' repositories { google() + mavenCentral() maven { url 'https://maven.aliyun.com/repository/public' } maven { url 'https://maven.aliyun.com/repository/gradle-plugin' } maven { url 'https://plugins.gradle.org/m2/' } @@ -18,6 +19,7 @@ buildscript { allprojects { repositories { google() + mavenCentral() maven { url 'https://maven.aliyun.com/repository/public' } maven { url 'https://jitpack.io' } }