pull/36/head
parent
5c3d50657a
commit
358f955071
@ -1,20 +1,108 @@ |
|||||||
package io.legado.app.ui.rss.source.edit |
package io.legado.app.ui.rss.source.edit |
||||||
|
|
||||||
|
import android.graphics.Rect |
||||||
import android.os.Bundle |
import android.os.Bundle |
||||||
|
import android.view.Gravity |
||||||
|
import android.view.Menu |
||||||
|
import android.view.ViewTreeObserver |
||||||
|
import android.widget.EditText |
||||||
|
import android.widget.PopupWindow |
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager |
||||||
import io.legado.app.R |
import io.legado.app.R |
||||||
import io.legado.app.base.VMBaseActivity |
import io.legado.app.base.VMBaseActivity |
||||||
|
import io.legado.app.constant.AppConst |
||||||
|
import io.legado.app.lib.theme.ATH |
||||||
|
import io.legado.app.ui.widget.KeyboardToolPop |
||||||
import io.legado.app.utils.getViewModel |
import io.legado.app.utils.getViewModel |
||||||
|
import kotlinx.android.synthetic.main.activity_book_source_edit.* |
||||||
|
import org.jetbrains.anko.displayMetrics |
||||||
|
import kotlin.math.abs |
||||||
|
|
||||||
class RssSourceEditActivity : |
class RssSourceEditActivity : |
||||||
VMBaseActivity<RssSourceEditViewModel>(R.layout.activity_rss_source_edit, false) { |
VMBaseActivity<RssSourceEditViewModel>(R.layout.activity_rss_source_edit, false), |
||||||
|
KeyboardToolPop.CallBack { |
||||||
|
|
||||||
|
private var mSoftKeyboardTool: PopupWindow? = null |
||||||
|
private var mIsSoftKeyBoardShowing = false |
||||||
|
|
||||||
|
private lateinit var adapter: RssSourceEditAdapter |
||||||
|
|
||||||
override val viewModel: RssSourceEditViewModel |
override val viewModel: RssSourceEditViewModel |
||||||
get() = getViewModel(RssSourceEditViewModel::class.java) |
get() = getViewModel(RssSourceEditViewModel::class.java) |
||||||
|
|
||||||
|
|
||||||
override fun onActivityCreated(savedInstanceState: Bundle?) { |
override fun onActivityCreated(savedInstanceState: Bundle?) { |
||||||
|
initView() |
||||||
|
} |
||||||
|
|
||||||
|
override fun onDestroy() { |
||||||
|
super.onDestroy() |
||||||
|
mSoftKeyboardTool?.dismiss() |
||||||
|
} |
||||||
|
|
||||||
|
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { |
||||||
|
menuInflater.inflate(R.menu.source_edit, menu) |
||||||
|
return super.onCompatCreateOptionsMenu(menu) |
||||||
|
} |
||||||
|
|
||||||
|
private fun initView() { |
||||||
|
ATH.applyEdgeEffectColor(recycler_view) |
||||||
|
mSoftKeyboardTool = KeyboardToolPop(this, AppConst.keyboardToolChars, this) |
||||||
|
window.decorView.viewTreeObserver.addOnGlobalLayoutListener(KeyboardOnGlobalChangeListener()) |
||||||
|
recycler_view.layoutManager = LinearLayoutManager(this) |
||||||
|
recycler_view.adapter = adapter |
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
|
override fun sendText(text: String) { |
||||||
|
if (text.isBlank()) return |
||||||
|
val view = window.decorView.findFocus() |
||||||
|
if (view is EditText) { |
||||||
|
val start = view.selectionStart |
||||||
|
val end = view.selectionEnd |
||||||
|
val edit = view.editableText//获取EditText的文字 |
||||||
|
if (start < 0 || start >= edit.length) { |
||||||
|
edit.append(text) |
||||||
|
} else { |
||||||
|
edit.replace(start, end, text)//光标所在位置插入文字 |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private fun showKeyboardTopPopupWindow() { |
||||||
|
mSoftKeyboardTool?.isShowing?.let { if (it) return } |
||||||
|
if (!isFinishing) { |
||||||
|
mSoftKeyboardTool?.showAtLocation(ll_content, Gravity.BOTTOM, 0, 0) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private fun closePopupWindow() { |
||||||
|
mSoftKeyboardTool?.let { |
||||||
|
if (it.isShowing) { |
||||||
|
it.dismiss() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private inner class KeyboardOnGlobalChangeListener : ViewTreeObserver.OnGlobalLayoutListener { |
||||||
|
override fun onGlobalLayout() { |
||||||
|
val rect = Rect() |
||||||
|
// 获取当前页面窗口的显示范围 |
||||||
|
window.decorView.getWindowVisibleDisplayFrame(rect) |
||||||
|
val screenHeight = this@RssSourceEditActivity.displayMetrics.heightPixels |
||||||
|
val keyboardHeight = screenHeight - rect.bottom // 输入法的高度 |
||||||
|
val preShowing = mIsSoftKeyBoardShowing |
||||||
|
if (abs(keyboardHeight) > screenHeight / 5) { |
||||||
|
mIsSoftKeyBoardShowing = true // 超过屏幕五分之一则表示弹出了输入法 |
||||||
|
recycler_view.setPadding(0, 0, 0, 100) |
||||||
|
showKeyboardTopPopupWindow() |
||||||
|
} else { |
||||||
|
mIsSoftKeyBoardShowing = false |
||||||
|
recycler_view.setPadding(0, 0, 0, 0) |
||||||
|
if (preShowing) { |
||||||
|
closePopupWindow() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
} |
} |
Loading…
Reference in new issue