pull/1110/head
gedoor 3 years ago
parent 3da4e43656
commit 9180a4c40d
  1. 42
      app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt
  2. 7
      app/src/main/res/layout/popup_action_menu.xml

@ -18,8 +18,6 @@ import androidx.appcompat.view.SupportMenuInflater
import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.view.menu.MenuItemImpl import androidx.appcompat.view.menu.MenuItemImpl
import io.legado.app.R 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.ItemTextBinding
import io.legado.app.databinding.PopupActionMenuBinding import io.legado.app.databinding.PopupActionMenuBinding
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
@ -27,6 +25,7 @@ import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.sendToClip import io.legado.app.utils.sendToClip
import io.legado.app.utils.share import io.legado.app.utils.share
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import splitties.views.onClick
import java.util.* import java.util.*
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
@ -35,7 +34,6 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac
TextToSpeech.OnInitListener { TextToSpeech.OnInitListener {
private val binding = PopupActionMenuBinding.inflate(LayoutInflater.from(context)) private val binding = PopupActionMenuBinding.inflate(LayoutInflater.from(context))
private val adapter = Adapter(context)
private val menu = MenuBuilder(context) private val menu = MenuBuilder(context)
private val moreMenu = MenuBuilder(context) private val moreMenu = MenuBuilder(context)
private val ttsListener by lazy { private val ttsListener by lazy {
@ -50,45 +48,23 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac
isOutsideTouchable = false isOutsideTouchable = false
isFocusable = false isFocusable = false
initRecyclerView()
}
private fun initRecyclerView() = binding.run {
recyclerView.adapter = adapter
SupportMenuInflater(context).inflate(R.menu.content_select_action, menu) SupportMenuInflater(context).inflate(R.menu.content_select_action, menu)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
onInitializeMenu(moreMenu) onInitializeMenu(moreMenu)
} }
adapter.setItems(menu.visibleItems + moreMenu.visibleItems) val menuItems = menu.visibleItems + moreMenu.visibleItems
} menuItems.forEach {
val textView = ItemTextBinding.inflate(LayoutInflater.from(context)).root.apply {
inner class Adapter(context: Context) : tag = it
RecyclerAdapter<MenuItemImpl, ItemTextBinding>(context) { text = it.title
onClick {
override fun getViewBinding(parent: ViewGroup): ItemTextBinding {
return ItemTextBinding.inflate(inflater, parent, false)
}
override fun convert(
holder: ItemViewHolder,
binding: ItemTextBinding,
item: MenuItemImpl,
payloads: MutableList<Any>
) {
binding.run {
textView.text = item.title
}
}
override fun registerListener(holder: ItemViewHolder, binding: ItemTextBinding) {
holder.itemView.setOnClickListener {
getItem(holder.layoutPosition)?.let {
if (!callBack.onMenuItemSelected(it.itemId)) { if (!callBack.onMenuItemSelected(it.itemId)) {
onMenuItemSelected(it) onMenuItemSelected(it)
} }
callBack.onMenuActionFinally()
} }
callBack.onMenuActionFinally()
} }
binding.root.addView(textView)
} }
} }

@ -1,10 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" <com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/shape_card_view" android:background="@drawable/shape_card_view"
android:padding="5dp" android:padding="5dp"
android:orientation="horizontal" app:flexDirection="row"
app:layoutManager="com.google.android.flexbox.FlexboxLayoutManager" /> app:flexWrap="wrap" />
Loading…
Cancel
Save