From 66380182227511102e6ba2dcce8e04aaf309d18a Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 3 Mar 2022 22:10:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E7=AB=A0=E6=8D=A2=E6=BA=90=E5=92=8C?= =?UTF-8?q?=E6=9B=B4=E5=A4=9A=E5=88=B7=E6=96=B0=E6=94=BE=E5=88=B0=E9=95=BF?= =?UTF-8?q?=E6=8C=89=E9=87=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/book/read/ReadBookActivity.kt | 4 ++-- .../java/io/legado/app/utils/MenuExtensions.kt | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) 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 3d08943ef..62b2ce59b 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 @@ -197,7 +197,7 @@ class ReadBookActivity : BaseReadBookActivity(), override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.book_read, menu) - menu.findItem(R.id.menu_change_source)?.setOnLongClickListener(menu) { + menu.iconItemOnLongClick(R.id.menu_change_source) { val changeSourceMenu = changeSourceMenu ?: PopupMenu(this, it).apply { inflate(R.menu.book_read_change_source) this.menu.applyOpenTint(this@ReadBookActivity) @@ -206,7 +206,7 @@ class ReadBookActivity : BaseReadBookActivity(), } changeSourceMenu.show() } - menu.findItem(R.id.menu_refresh)?.setOnLongClickListener(menu) { + menu.iconItemOnLongClick(R.id.menu_refresh) { val refreshMenu = refreshMenu ?: PopupMenu(this, it).apply { inflate(R.menu.book_read_refresh) this.menu.applyOpenTint(this@ReadBookActivity) diff --git a/app/src/main/java/io/legado/app/utils/MenuExtensions.kt b/app/src/main/java/io/legado/app/utils/MenuExtensions.kt index f43253821..188bb23f4 100644 --- a/app/src/main/java/io/legado/app/utils/MenuExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/MenuExtensions.kt @@ -61,9 +61,12 @@ fun Menu.applyOpenTint(context: Context) { } } -fun MenuItem.setOnLongClickListener(menu: Menu, function: (view: View) -> (Unit)) { - setActionView(R.layout.view_action_button) - actionView.findViewById(R.id.item).setImageDrawable(icon) - actionView.setOnLongClickListener { function.invoke(actionView); true } - actionView.setOnClickListener { menu.performIdentifierAction(itemId, 0) } -} \ No newline at end of file +fun Menu.iconItemOnLongClick(id: Int, function: (view: View) -> Unit) { + findItem(id)?.let { item -> + item.setActionView(R.layout.view_action_button) + item.actionView.contentDescription = item.title + item.actionView.findViewById(R.id.item).setImageDrawable(item.icon) + item.actionView.setOnLongClickListener { function.invoke(item.actionView); true } + item.actionView.setOnClickListener { performIdentifierAction(id, 0) } + } +}