字典显示错位 #86

This commit is contained in:
HapeLee
2025-10-13 13:16:12 +08:00
parent acef789a47
commit 96eb57d28c
2 changed files with 62 additions and 23 deletions
@@ -4,6 +4,7 @@ import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.view.View
import androidx.fragment.app.viewModels
import androidx.transition.TransitionManager
import com.google.android.material.tabs.TabLayout
import io.legato.kazusa.R
import io.legato.kazusa.base.BaseBottomSheetDialogFragment
@@ -40,36 +41,60 @@ class DictDialog() : BaseBottomSheetDialogFragment(R.layout.dialog_dict) {
return
}
setupTabs()
observeDicts()
}
private fun setupTabs() {
binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabReselected(tab: TabLayout.Tab) {
}
override fun onTabUnselected(tab: TabLayout.Tab) {
}
override fun onTabReselected(tab: TabLayout.Tab) = Unit
override fun onTabUnselected(tab: TabLayout.Tab) = Unit
override fun onTabSelected(tab: TabLayout.Tab) {
val dictRule = tab.tag as DictRule
binding.rotateLoading.visible()
viewModel.dict(dictRule, word!!) {
binding.rotateLoading.gone()
binding.tvDict.setHtml(it)
}
loadDict(dictRule)
}
})
viewModel.initData {
it.forEach {
}
private fun observeDicts() {
viewModel.initData { dictRules ->
if (dictRules.isEmpty()) {
showEmptyView("暂无可用词典")
return@initData
}
binding.emptyMessageView.gone()
dictRules.forEach {
binding.tabLayout.addTab(binding.tabLayout.newTab().apply {
text = it.name
tag = it
})
}
setupTabLayoutMode(it.size)
setupTabLayoutMode(dictRules.size)
binding.tabLayout.getTabAt(0)?.select()
}
}
private fun loadDict(dictRule: DictRule) {
binding.tvDict.gone()
binding.emptyMessageView.gone()
binding.rotateLoading.visible()
viewModel.dict(dictRule, word!!) { result ->
binding.rotateLoading.gone()
if (result.isBlank()) {
showEmptyView("没有查询到结果")
} else {
binding.tvDict.visible()
binding.tvDict.setHtml(result)
}
}
}
//根据已启用词典数动态选取布局
private fun setupTabLayoutMode(dictCount: Int) {
if (dictCount <= 4) {
binding.tabLayout.tabMode = TabLayout.MODE_FIXED
@@ -80,4 +105,10 @@ class DictDialog() : BaseBottomSheetDialogFragment(R.layout.dialog_dict) {
}
}
}
private fun showEmptyView(message: String) {
binding.tvDict.gone()
binding.rotateLoading.gone()
binding.emptyMessageView.visible()
binding.emptyMessageView.setMessage(message)
}
}
+14 -6
View File
@@ -1,14 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingHorizontal="16dp"
android:paddingBottom="16dp">
<LinearLayout
android:id="@+id/header_layout"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
@@ -46,6 +47,7 @@
style="@style/Widget.Material3.LoadingIndicator.Contained"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
@@ -56,17 +58,23 @@
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="64dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tab_layout">
<io.legato.kazusa.ui.widget.text.ScrollTextView
<TextView
android:id="@+id/tv_dict"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="wrap_content" />
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
<io.legato.kazusa.ui.widget.EmptyMessageView
android:id="@+id/empty_message_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:messageText="词典内容为空!"/>
</LinearLayout>