Skip to content

Commit

Permalink
fix: initialize the keyboardView.layoutManager every time in the liqu…
Browse files Browse the repository at this point in the history
…id keyboard
  • Loading branch information
shitlime committed Aug 2, 2023
1 parent d718ceb commit 0f9cc4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
4 changes: 0 additions & 4 deletions app/src/main/java/com/osfans/trime/ime/core/Trime.java
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,6 @@ public void selectLiquidKeyboard(final int tabIndex) {
symbolInput.getLayoutParams().height = mainInput.getHeight();
symbolInput.setVisibility(View.VISIBLE);

// 检测横屏/竖屏 初始化flexbox
final int orientation = getResources().getConfiguration().orientation;
liquidKeyboard.initFlexbox(orientation==Configuration.ORIENTATION_LANDSCAPE);

symbolKeyboardType = liquidKeyboard.select(tabIndex);
tabView.updateTabWidth();
if (inputRootBinding != null) {
Expand Down
45 changes: 21 additions & 24 deletions app/src/main/java/com/osfans/trime/ime/symbol/LiquidKeyboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ class LiquidKeyboard(private val context: Context) : ClipboardHelper.OnClipboard
private lateinit var keyboardView: RecyclerView
private val symbolHistory = SymbolHistory(180)

private lateinit var flexbox: FlexboxLayoutManager

private val oneColumnStaggeredGrid: StaggeredGridLayoutManager by lazy {
return@lazy StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
}

fun setKeyboardView(view: RecyclerView) {
keyboardView = view
keyboardView.apply {
Expand All @@ -50,25 +44,28 @@ class LiquidKeyboard(private val context: Context) : ClipboardHelper.OnClipboard
}
}

// 及时更新layoutManager, 以防在旋转屏幕后打开液体键盘crash
/**
* 根据屏幕状态初始化 flexbox
* 使用FlexboxLayoutManager时调用此函数获取
*/
fun initFlexbox(isLandscape: Boolean) {
if (isLandscape) {
flexbox = FlexboxLayoutManager(context).apply {
flexDirection = FlexDirection.COLUMN // 主轴为垂直方向,起点在左端。
flexWrap = FlexWrap.WRAP // 按正常方向换行
justifyContent = JustifyContent.FLEX_START // 交叉轴的起点对齐
}
} else {
flexbox = FlexboxLayoutManager(context).apply {
flexDirection = FlexDirection.ROW // 主轴为水平方向,起点在左端。
flexWrap = FlexWrap.WRAP // 按正常方向换行
justifyContent = JustifyContent.FLEX_START // 交叉轴的起点对齐
}
private fun getFlexbox(): FlexboxLayoutManager {
return FlexboxLayoutManager(context).apply {
flexDirection = FlexDirection.ROW // 主轴为水平方向,起点在左端。
flexWrap = FlexWrap.WRAP // 按正常方向换行
justifyContent = JustifyContent.FLEX_START // 交叉轴的起点对齐
}
}

/**
* 使用StaggeredGridLayoutManager时调用此函数获取
*/
private fun getOneColumnStaggeredGrid(): StaggeredGridLayoutManager {
return StaggeredGridLayoutManager(
1,
StaggeredGridLayoutManager.VERTICAL,
)
}

fun select(i: Int): SymbolKeyboardType {
val tag = TabManager.getTag(i)
symbolHistory.load()
Expand Down Expand Up @@ -137,7 +134,7 @@ class LiquidKeyboard(private val context: Context) : ClipboardHelper.OnClipboard
}
}
keyboardView.apply {
layoutManager = flexbox
layoutManager = getFlexbox()
/*
Timber.d(
"configStylet() single_width=%s, keyHeight=%s, margin_x=%s, margin_top=%s",
Expand Down Expand Up @@ -239,7 +236,7 @@ class LiquidKeyboard(private val context: Context) : ClipboardHelper.OnClipboard
})
}
keyboardView.apply {
layoutManager = oneColumnStaggeredGrid
layoutManager = getOneColumnStaggeredGrid()
adapter = dbAdapter
// 调用ListView的setSelected(!ListView.isSelected())方法,这样就能及时刷新布局
isSelected = true
Expand Down Expand Up @@ -281,7 +278,7 @@ class LiquidKeyboard(private val context: Context) : ClipboardHelper.OnClipboard
}
// 设置布局管理器
keyboardView.apply {
layoutManager = flexbox
layoutManager = getFlexbox()
adapter = candidateAdapter
isSelected = true
}
Expand All @@ -298,7 +295,7 @@ class LiquidKeyboard(private val context: Context) : ClipboardHelper.OnClipboard
}
// 设置布局管理器
keyboardView.apply {
layoutManager = flexbox
layoutManager = getFlexbox()
adapter = candidateAdapter
keyboardView.isSelected = true
}
Expand Down

0 comments on commit 0f9cc4e

Please sign in to comment.