feat: 优化代码

pull/159/head
kunfei 5 years ago
parent 14be9913f3
commit d0b0a9c749
  1. 5
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 51
      app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt
  3. 39
      app/src/main/res/layout/popup_action_menu.xml

@ -403,11 +403,10 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(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

@ -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
onInitializeMenu(moreMenu)
}
if (moreMenu.size() > 0) {
iv_menu_more.visible()
}
iv_menu_more.onClick {
popupMenu.show()
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()
}
}
}

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/shape_card_view"
android:padding="5dp"
android:orientation="horizontal">
@ -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" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_menu_more"
@ -21,7 +28,31 @@
android:tint="@color/tv_text_default"
android:visibility="gone"
android:contentDescription="@string/more_menu"
android:layout_gravity="center_vertical" />
android:layout_gravity="center_vertical"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/recycler_view_top" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/iv_menu_more"
app:layout_constraintRight_toRightOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/iv_menu_more"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Loading…
Cancel
Save