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 6808baf83..8cd1915cc 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 @@ -403,11 +403,10 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo */ override fun showTextActionMenu() { textActionMenu ?: let { - textActionMenu = TextActionMenu(this, this).apply { - contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED) - } + textActionMenu = TextActionMenu(this, this) } textActionMenu?.let { popup -> + popup.contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED) val popupHeight = popup.contentView.measuredHeight val x = text_menu_position.x.toInt() var y = text_menu_position.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 9d7046f6e..55c1855e4 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 @@ -15,13 +15,11 @@ import androidx.annotation.RequiresApi import androidx.appcompat.view.SupportMenuInflater import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.view.menu.MenuItemImpl -import androidx.appcompat.widget.PopupMenu -import androidx.core.view.size -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView +import androidx.core.view.isVisible import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.utils.gone import io.legado.app.utils.isAbsUrl import io.legado.app.utils.sendToClip import io.legado.app.utils.visible @@ -35,6 +33,10 @@ import org.jetbrains.anko.toast class TextActionMenu(private val context: Context, private val callBack: CallBack) : PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) { + private val adapter = Adapter(context) + private val menu = MenuBuilder(context) + private val moreMenu = MenuBuilder(context) + init { @SuppressLint("InflateParams") contentView = LayoutInflater.from(context).inflate(R.layout.popup_action_menu, null) @@ -44,33 +46,40 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac isFocusable = false initRecyclerView() + setOnDismissListener { + contentView.apply { + iv_menu_more.setImageResource(R.drawable.ic_more_vert) + recycler_view_top.gone() + recycler_view_bottom.gone() + adapter.setItems(menu.visibleItems) + recycler_view.visible() + } + } } private fun initRecyclerView() = with(contentView) { - val adapter = Adapter(context) - recycler_view.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false) recycler_view.adapter = adapter - val menu = MenuBuilder(context) SupportMenuInflater(context).inflate(R.menu.content_select_action, menu) adapter.setItems(menu.visibleItems) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - val popupMenu = PopupMenu(context, iv_menu_more) - onInitializeMenu(popupMenu.menu) - if (popupMenu.menu.size > 0) { - iv_menu_more.visible() - popupMenu.setOnMenuItemClickListener { item -> - item.intent?.let { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - it.putExtra(Intent.EXTRA_PROCESS_TEXT, callBack.selectedText) - context.startActivity(it) - } - } - this@TextActionMenu.dismiss() - true - } - } - iv_menu_more.onClick { - popupMenu.show() + onInitializeMenu(moreMenu) + } + if (moreMenu.size() > 0) { + iv_menu_more.visible() + } + iv_menu_more.onClick { + if (recycler_view.isVisible) { + iv_menu_more.setImageResource(R.drawable.ic_arrow_back) + recycler_view_top.adapter = adapter + adapter.setItems(moreMenu.visibleItems) + recycler_view.gone() + recycler_view_top.visible() + } else { + iv_menu_more.setImageResource(R.drawable.ic_more_vert) + recycler_view_top.gone() + recycler_view_bottom.gone() + adapter.setItems(menu.visibleItems) + recycler_view.visible() } } } diff --git a/app/src/main/res/layout/popup_action_menu.xml b/app/src/main/res/layout/popup_action_menu.xml index 98cceb33c..bf55912fd 100644 --- a/app/src/main/res/layout/popup_action_menu.xml +++ b/app/src/main/res/layout/popup_action_menu.xml @@ -1,7 +1,8 @@ - @@ -10,7 +11,13 @@ android:id="@+id/recycler_view" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_gravity="center" /> + android:layout_gravity="center" + android:orientation="horizontal" + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + app:layout_constraintLeft_toLeftOf="parent" + app:layout_constraintTop_toTopOf="@+id/iv_menu_more" + app:layout_constraintRight_toLeftOf="@+id/iv_menu_more" + app:layout_constraintBottom_toBottomOf="@+id/iv_menu_more" /> + android:layout_gravity="center_vertical" + app:layout_constraintRight_toRightOf="parent" + app:layout_constraintTop_toBottomOf="@+id/recycler_view_top" /> - + + + + +