kunfei 3 лет назад
Родитель
Сommit
b002413356

+ 1 - 1
app/src/main/AndroidManifest.xml

@@ -167,7 +167,7 @@
             android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout"
             android:launchMode="singleTask"
             android:windowSoftInputMode="adjustResize|stateHidden" />
-        <!-- 书源编辑 -->
+        <!-- 替换规则编辑 -->
         <activity
             android:name=".ui.replace.edit.ReplaceEditActivity"
             android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout"

+ 10 - 4
app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt

@@ -23,10 +23,7 @@ import io.legado.app.lib.theme.primaryColor
 import io.legado.app.lib.theme.primaryTextColor
 import io.legado.app.ui.book.explore.ExploreShowActivity
 import io.legado.app.ui.book.source.edit.BookSourceEditActivity
-import io.legado.app.utils.applyTint
-import io.legado.app.utils.cnCompare
-import io.legado.app.utils.setEdgeEffectColor
-import io.legado.app.utils.startActivity
+import io.legado.app.utils.*
 import io.legado.app.utils.viewbindingdelegate.viewBinding
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Job
@@ -51,6 +48,7 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explo
     private val groups = linkedSetOf<String>()
     private var exploreFlowJob: Job? = null
     private var groupsMenu: SubMenu? = null
+    private val softKeyBoardChangeListener = SoftKeyBoardChangeListener()
 
     override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
         setSupportToolbar(binding.titleBar.toolbar)
@@ -58,6 +56,14 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(R.layout.fragment_explo
         initRecyclerView()
         initGroupData()
         upExploreData()
+        softKeyBoardChangeListener.attach(requireActivity().window) {
+            binding.root.setPaddingBottom(softKeyBoardChangeListener.keyBoardHeight)
+        }
+    }
+
+    override fun onDestroy() {
+        super.onDestroy()
+        softKeyBoardChangeListener.unAttach()
     }
 
     override fun onCompatCreateOptionsMenu(menu: Menu) {

+ 47 - 0
app/src/main/java/io/legado/app/utils/SoftKeyBoardChangeListener.kt

@@ -0,0 +1,47 @@
+package io.legado.app.utils
+
+import android.graphics.Rect
+import android.view.ViewTreeObserver
+import android.view.Window
+import splitties.systemservices.windowManager
+import kotlin.math.abs
+
+class SoftKeyBoardChangeListener : ViewTreeObserver.OnGlobalLayoutListener {
+
+    private var window: Window? = null
+    private var softKeyBoardChangeCallback: ((Boolean) -> Unit)? = null
+    var keyBoardShowing = false
+        private set
+    var keyBoardHeight = 0
+
+    fun attach(window: Window, callback: ((Boolean) -> Unit)) {
+        this.window = window
+        window.decorView.viewTreeObserver.addOnGlobalLayoutListener(this)
+        softKeyBoardChangeCallback = callback
+    }
+
+    fun unAttach() {
+        window?.decorView?.viewTreeObserver?.removeOnGlobalLayoutListener(this)
+        window = null
+    }
+
+    override fun onGlobalLayout() {
+        window?.let {
+            val rect = Rect()
+            // 获取当前页面窗口的显示范围
+            it.decorView.getWindowVisibleDisplayFrame(rect)
+            val screenHeight = windowManager.windowSize.heightPixels
+            val keyboardHeight = screenHeight - rect.bottom // 输入法的高度
+            if (abs(keyboardHeight) > screenHeight / 5) {
+                keyBoardShowing = true // 超过屏幕五分之一则表示弹出了输入法
+                this.keyBoardHeight = abs(keyboardHeight)
+                softKeyBoardChangeCallback?.invoke(true)
+            } else {
+                keyBoardShowing = false
+                this.keyBoardHeight = 0
+                softKeyBoardChangeCallback?.invoke(false)
+            }
+        }
+    }
+
+}

+ 4 - 0
app/src/main/java/io/legado/app/utils/ViewExtensions.kt

@@ -140,6 +140,10 @@ fun View.screenshot(): Bitmap? {
     }
 }
 
+fun View.setPaddingBottom(bottom: Int) {
+    setPadding(paddingLeft, paddingTop, paddingRight, bottom)
+}
+
 fun SeekBar.progressAdd(int: Int) {
     progress += int
 }