feat(activity): 新增 SingleFragmentActivity 通用 Fragment 容器页
This commit is contained in:
@@ -106,6 +106,10 @@
|
|||||||
android:theme="@style/Theme.DishMatch.NoSplash"
|
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||||
android:screenOrientation="portrait"
|
android:screenOrientation="portrait"
|
||||||
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
||||||
|
<activity
|
||||||
|
android:name="com.shuwei.dish.match.ui.SingleFragmentActivity"
|
||||||
|
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||||
|
tools:ignore="DiscouragedApi,LockedOrientationActivity" />
|
||||||
<activity
|
<activity
|
||||||
android:name="com.shuwei.dish.match.ui.FoodRecognizeActivity"
|
android:name="com.shuwei.dish.match.ui.FoodRecognizeActivity"
|
||||||
android:theme="@style/Theme.DishMatch.NoSplash"
|
android:theme="@style/Theme.DishMatch.NoSplash"
|
||||||
|
|||||||
@@ -126,6 +126,9 @@ open class BaseActivity : AppCompatActivity() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 返回 Fragment 容器的 View ID,供子类加载 Fragment 使用 */
|
||||||
|
protected fun getFragmentContainerId(): Int = R.id.flContainer
|
||||||
|
|
||||||
override fun setContentView(view: View?) {
|
override fun setContentView(view: View?) {
|
||||||
if (view == binding.root) {
|
if (view == binding.root) {
|
||||||
super.setContentView(view)
|
super.setContentView(view)
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
package com.shuwei.dish.match.ui
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
import com.shuwei.dish.match.base.BaseActivity
|
||||||
|
import com.shuwei.dish.match.ui.fragment.CollectFragment
|
||||||
|
import com.shuwei.dish.match.ui.fragment.DeviceConfigFragment
|
||||||
|
import com.shuwei.dish.match.ui.fragment.SeasoningConfigFragment
|
||||||
|
import com.shuwei.dish.match.utils.ext.gone
|
||||||
|
import com.shuwei.dish.match.utils.ext.visible
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用单 Fragment 容器 Activity
|
||||||
|
* 通过 [PageType] 决定加载哪个 Fragment,避免为每个配置/功能页面单独创建 Activity
|
||||||
|
*/
|
||||||
|
class SingleFragmentActivity : BaseActivity() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支持的页面类型枚举
|
||||||
|
* 新增页面时在此追加枚举值,并在 [createFragment] 中补充对应分支
|
||||||
|
*/
|
||||||
|
enum class PageType {
|
||||||
|
/** 菜品采集页 */
|
||||||
|
COLLECT,
|
||||||
|
/** 设备配置页 */
|
||||||
|
DEVICE_CONFIG,
|
||||||
|
/** 调料配置页 */
|
||||||
|
SEASONING_CONFIG,
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val EXTRA_PAGE_TYPE = "extra_page_type"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动 SingleFragmentActivity
|
||||||
|
* @param context 调用方 Context
|
||||||
|
* @param pageType 要显示的页面类型
|
||||||
|
*/
|
||||||
|
fun start(context: Context, pageType: PageType) {
|
||||||
|
Intent(context, SingleFragmentActivity::class.java).apply {
|
||||||
|
putExtra(EXTRA_PAGE_TYPE, pageType.name)
|
||||||
|
context.startActivity(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
setHeaderBackground()
|
||||||
|
|
||||||
|
// 解析页面类型,非法值直接关闭
|
||||||
|
val typeName = intent.getStringExtra(EXTRA_PAGE_TYPE)
|
||||||
|
val pageType = typeName?.let {
|
||||||
|
runCatching { PageType.valueOf(it) }.getOrNull()
|
||||||
|
}
|
||||||
|
if (pageType == null) {
|
||||||
|
finish()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 配置标题栏
|
||||||
|
setTitleBar(
|
||||||
|
titleBarAction = { it.visible() },
|
||||||
|
titleAction = { it.text = getTitleForPage(pageType) },
|
||||||
|
rightIconActon = { it.gone() }
|
||||||
|
)
|
||||||
|
|
||||||
|
// 首次创建时加载 Fragment,避免屏幕旋转重复添加
|
||||||
|
if (savedInstanceState == null) {
|
||||||
|
val fragment = createFragment(pageType)
|
||||||
|
supportFragmentManager.beginTransaction()
|
||||||
|
.replace(getFragmentContainerId(), fragment)
|
||||||
|
.commit()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据页面类型返回对应标题文本
|
||||||
|
*/
|
||||||
|
private fun getTitleForPage(pageType: PageType): String = when (pageType) {
|
||||||
|
PageType.COLLECT -> "菜品采集"
|
||||||
|
PageType.DEVICE_CONFIG -> "设备配置"
|
||||||
|
PageType.SEASONING_CONFIG -> "调料配置"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据页面类型创建对应 Fragment 实例
|
||||||
|
*/
|
||||||
|
private fun createFragment(pageType: PageType): Fragment = when (pageType) {
|
||||||
|
PageType.COLLECT -> CollectFragment()
|
||||||
|
PageType.DEVICE_CONFIG -> DeviceConfigFragment()
|
||||||
|
PageType.SEASONING_CONFIG -> SeasoningConfigFragment()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user