Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
A
appzxhy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wanglei
appzxhy
Commits
4cc10ff9
Commit
4cc10ff9
authored
Jul 11, 2025
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[AI重构]AI重构tab
parent
47214079
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
265 additions
and
57 deletions
+265
-57
HomeTabUIBean.kt
...src/main/java/com/simplecleaner/app/bean/HomeTabUIBean.kt
+26
-2
MainActivity.kt
...c/main/java/com/simplecleaner/app/ui/main/MainActivity.kt
+54
-55
TabManager.kt
...src/main/java/com/simplecleaner/app/ui/main/TabManager.kt
+185
-0
No files found.
app/src/main/java/com/simplecleaner/app/bean/HomeTabUIBean.kt
View file @
4cc10ff9
package
com.simplecleaner.app.bean
/**
* 首页Tab配置Bean
* @param icon Tab图标资源ID
* @param tab Tab显示文本
* @param showRedDot 是否显示红点
* @param badgeCount 徽章数量(可选)
* @param isEnabled 是否启用该Tab
*/
data class
HomeTabUIBean
(
val
icon
:
Int
,
val
tab
:
String
)
\ No newline at end of file
val
tab
:
String
,
val
showRedDot
:
Boolean
=
false
,
val
badgeCount
:
Int
=
0
,
val
isEnabled
:
Boolean
=
true
)
{
companion
object
{
/**
* 创建默认的Tab配置
*/
fun
createDefault
(
icon
:
Int
,
tab
:
String
,
showRedDot
:
Boolean
=
false
):
HomeTabUIBean
{
return
HomeTabUIBean
(
icon
=
icon
,
tab
=
tab
,
showRedDot
=
showRedDot
)
}
}
}
\ No newline at end of file
app/src/main/java/com/simplecleaner/app/ui/main/MainActivity.kt
View file @
4cc10ff9
package
com.simplecleaner.app.ui.main
import
android.view.LayoutInflater
import
android.view.View
import
android.widget.TextView
import
androidx.constraintlayout.widget.ConstraintLayout
import
androidx.core.content.ContextCompat
import
androidx.core.view.ViewCompat
import
androidx.core.view.WindowInsetsCompat
import
androidx.fragment.app.Fragment
import
androidx.lifecycle.ViewModelProvider
import
androidx.lifecycle.lifecycleScope
import
androidx.viewpager2.adapter.FragmentStateAdapter
import
androidx.viewpager2.widget.ViewPager2
import
com.simplecleaner.app.R
import
com.simplecleaner.app.SpConstObject.mainPageCount
import
com.simplecleaner.app.base.BaseActivity
...
...
@@ -20,7 +16,6 @@ import com.simplecleaner.app.bean.HomeTabUIBean
import
com.simplecleaner.app.bean.config.AdConfigBean
import
com.simplecleaner.app.business.ads.AdsMgr
import
com.simplecleaner.app.databinding.ActivityMainBinding
import
com.simplecleaner.app.databinding.ItemHomeTabBinding
import
com.simplecleaner.app.ui.dialog.AppExitDialog
import
com.simplecleaner.app.ui.dialog.NotificationTurnOnDialog
import
com.simplecleaner.app.ui.dialog.RateDialog
...
...
@@ -29,11 +24,8 @@ import com.simplecleaner.app.ui.dialog.permissionStorageJump
import
com.simplecleaner.app.ui.set.SettingsFragment
import
com.simplecleaner.app.utils.PermissionUtils.requestNotificationPermission
import
com.simplecleaner.app.utils.PermissionUtils.requestStoragePermission
import
com.google.android.material.tabs.TabLayout
import
com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import
kotlinx.coroutines.flow.collectLatest
import
kotlinx.coroutines.launch
import
androidx.core.graphics.toColorInt
import
com.simplecleaner.app.base.jumpAction
import
com.simplecleaner.app.bean.FeatureBean.Companion.BATTERY_INFO
import
com.simplecleaner.app.ui.batteryinfo.BatteryReceiver
...
...
@@ -55,6 +47,26 @@ class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::infl
}
}
// Tab管理器
private
val
tabManager
by
lazy
{
TabManager
(
this
,
binding
.
tabLayout
,
binding
.
viewPager2
,
fragmentList
)
}
// 定义Tab数据,便于维护和扩展
private
val
tabDataList
by
lazy
{
listOf
(
HomeTabUIBean
.
createDefault
(
icon
=
R
.
drawable
.
tab_home_selector
,
tab
=
getString
(
R
.
string
.
home
)
),
HomeTabUIBean
.
createDefault
(
icon
=
R
.
drawable
.
tab_settings_selector
,
tab
=
getString
(
R
.
string
.
settings
),
showRedDot
=
true
)
)
}
override
fun
useDefaultImmersive
()
{
ViewCompat
.
setOnApplyWindowInsetsListener
(
binding
.
root
)
{
v
,
insets
->
...
...
@@ -130,65 +142,48 @@ class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::infl
}
private
fun
initTab
()
{
arrayOf
(
HomeTabUIBean
(
R
.
drawable
.
tab_home_selector
,
getString
(
R
.
string
.
home
)),
HomeTabUIBean
(
R
.
drawable
.
tab_settings_selector
,
getString
(
R
.
string
.
settings
)),
).
forEachIndexed
{
index
,
homeTab
->
val
tab
=
binding
.
tabLayout
.
newTab
()
val
view
=
LayoutInflater
.
from
(
this
).
inflate
(
R
.
layout
.
item_home_tab
,
null
)
val
tabBinding
=
ItemHomeTabBinding
.
bind
(
view
)
tabBinding
.
ivIcon
.
setImageResource
(
homeTab
.
icon
)
tabBinding
.
tvTab
.
text
=
homeTab
.
tab
// tabBinding.tvTab.setTextColor(ContextCompat.getColor(this, R.color.color_tab_home))
tab
.
customView
=
tabBinding
.
root
tab
.
id
=
index
if
(
index
==
1
)
{
tabBinding
.
vDian
.
visibility
=
View
.
VISIBLE
}
else
{
tabBinding
.
vDian
.
visibility
=
View
.
GONE
}
binding
.
tabLayout
.
addTab
(
tab
)
// 使用TabManager初始化Tab
tabManager
.
initTabs
(
tabDataList
)
// 设置Tab重新选择监听器
tabManager
.
setOnTabReselectedListener
{
tabIndex
->
handleTabReselected
(
tabIndex
)
}
binding
.
tabLayout
.
addOnTabSelectedListener
(
object
:
OnTabSelectedListener
{
override
fun
onTabSelected
(
tab
:
TabLayout
.
Tab
?)
{
tab
?.
let
{
if
(
binding
.
viewPager2
.
currentItem
==
tab
.
id
)
return
binding
.
viewPager2
.
setCurrentItem
(
tab
.
id
,
false
)
}
}
}
override
fun
onTabUnselected
(
tab
:
TabLayout
.
Tab
?)
{
tab
?.
setUnselected
()
/**
* 处理Tab重新选择事件
*/
private
fun
handleTabReselected
(
tabIndex
:
Int
)
{
when
(
tabIndex
)
{
TAB_INDEX_HOME
->
{
// 首页重新选择,可以刷新数据
// (fragmentList[TAB_INDEX_HOME] as? HomeFragment)?.refreshData()
}
override
fun
onTabReselected
(
tab
:
TabLayout
.
Tab
?)
{
TAB_INDEX_SETTINGS
->
{
// 设置页面重新选择,可以刷新设置状态
// (fragmentList[TAB_INDEX_SETTINGS] as? SettingsFragment)?.refreshSettings()
}
}
)
}
}
fun
TabLayout
.
Tab
.
setSelected
()
{
val
textView
=
customView
?.
findViewById
<
TextView
>(
R
.
id
.
tv_tab
)
textView
?.
setTextColor
(
ContextCompat
.
getColor
(
this
@MainActivity
,
R
.
color
.
colorPrimary
)
)
/**
* 更新Tab红点状态
*/
fun
updateTabRedDot
(
tabIndex
:
Int
,
showRedDot
:
Boolean
)
{
tabManager
.
updateTabRedDot
(
tabIndex
,
showRedDot
)
}
fun
TabLayout
.
Tab
.
setUnselected
()
{
val
textView
=
customView
?.
findViewById
<
TextView
>(
R
.
id
.
tv_tab
)
textView
?.
setTextColor
(
"#A6A6A6"
.
toColorInt
())
/**
* 设置Tab启用状态
*/
fun
setTabEnabled
(
tabIndex
:
Int
,
enabled
:
Boolean
)
{
tabManager
.
setTabEnabled
(
tabIndex
,
enabled
)
}
private
fun
initViewPager2
()
{
binding
.
viewPager2
.
adapter
=
viewPagerAdapter
binding
.
viewPager2
.
registerOnPageChangeCallback
(
object
:
ViewPager2
.
OnPageChangeCallback
()
{
override
fun
onPageSelected
(
position
:
Int
)
{
val
defaultTab
=
binding
.
tabLayout
.
getTabAt
(
position
)
defaultTab
?.
select
()
defaultTab
?.
setSelected
()
}
})
// ViewPager监听器现在由TabManager处理
}
...
...
@@ -212,4 +207,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::infl
}
companion
object
{
private
const
val
TAB_INDEX_HOME
=
0
private
const
val
TAB_INDEX_SETTINGS
=
1
}
}
\ No newline at end of file
app/src/main/java/com/simplecleaner/app/ui/main/TabManager.kt
0 → 100644
View file @
4cc10ff9
package
com.simplecleaner.app.ui.main
import
android.content.Context
import
android.view.LayoutInflater
import
android.view.View
import
android.widget.TextView
import
androidx.core.content.ContextCompat
import
androidx.core.graphics.toColorInt
import
androidx.fragment.app.Fragment
import
androidx.viewpager2.widget.ViewPager2
import
com.google.android.material.tabs.TabLayout
import
com.simplecleaner.app.R
import
com.simplecleaner.app.bean.HomeTabUIBean
import
com.simplecleaner.app.databinding.ItemHomeTabBinding
/**
* Tab管理器,负责管理Tab的创建、配置和交互
*/
class
TabManager
(
private
val
context
:
Context
,
private
val
tabLayout
:
TabLayout
,
private
val
viewPager2
:
ViewPager2
,
private
val
fragmentList
:
List
<
Fragment
>
)
{
private
val
tabDataList
=
mutableListOf
<
HomeTabUIBean
>()
private
var
onTabReselectedListener
:
((
Int
)
->
Unit
)?
=
null
/**
* 初始化Tab
*/
fun
initTabs
(
tabConfigs
:
List
<
HomeTabUIBean
>)
{
tabDataList
.
clear
()
tabDataList
.
addAll
(
tabConfigs
)
setupTabData
()
setupTabListener
()
setupViewPagerListener
()
}
/**
* 设置Tab数据
*/
private
fun
setupTabData
()
{
tabDataList
.
forEachIndexed
{
index
,
tabData
->
val
tab
=
createTab
(
tabData
,
index
)
tabLayout
.
addTab
(
tab
)
}
}
/**
* 创建单个Tab
*/
private
fun
createTab
(
tabData
:
HomeTabUIBean
,
index
:
Int
):
TabLayout
.
Tab
{
val
tab
=
tabLayout
.
newTab
()
val
tabView
=
createTabView
(
tabData
,
index
)
tab
.
customView
=
tabView
tab
.
id
=
index
return
tab
}
/**
* 创建Tab视图
*/
private
fun
createTabView
(
tabData
:
HomeTabUIBean
,
index
:
Int
):
View
{
val
view
=
LayoutInflater
.
from
(
context
).
inflate
(
R
.
layout
.
item_home_tab
,
null
)
val
tabBinding
=
ItemHomeTabBinding
.
bind
(
view
)
// 设置图标和文本
tabBinding
.
ivIcon
.
setImageResource
(
tabData
.
icon
)
tabBinding
.
tvTab
.
text
=
tabData
.
tab
// 设置红点显示逻辑
tabBinding
.
vDian
.
visibility
=
if
(
tabData
.
showRedDot
)
View
.
VISIBLE
else
View
.
GONE
// 设置Tab启用状态
view
.
isEnabled
=
tabData
.
isEnabled
if
(!
tabData
.
isEnabled
)
{
view
.
alpha
=
0.5f
}
return
tabBinding
.
root
}
/**
* 设置Tab选择监听器
*/
private
fun
setupTabListener
()
{
tabLayout
.
addOnTabSelectedListener
(
object
:
TabLayout
.
OnTabSelectedListener
{
override
fun
onTabSelected
(
tab
:
TabLayout
.
Tab
?)
{
tab
?.
let
{
selectedTab
->
// 检查Tab是否启用
val
tabData
=
tabDataList
.
getOrNull
(
selectedTab
.
id
)
if
(
tabData
?.
isEnabled
==
false
)
{
// Tab被禁用,不执行切换
return
@let
}
if
(
viewPager2
.
currentItem
!=
selectedTab
.
id
)
{
viewPager2
.
setCurrentItem
(
selectedTab
.
id
,
false
)
}
}
}
override
fun
onTabUnselected
(
tab
:
TabLayout
.
Tab
?)
{
tab
?.
setUnselected
()
}
override
fun
onTabReselected
(
tab
:
TabLayout
.
Tab
?)
{
onTabReselectedListener
?.
invoke
(
tab
?.
id
?:
-
1
)
}
})
}
/**
* 设置ViewPager监听器
*/
private
fun
setupViewPagerListener
()
{
viewPager2
.
registerOnPageChangeCallback
(
object
:
ViewPager2
.
OnPageChangeCallback
()
{
override
fun
onPageSelected
(
position
:
Int
)
{
val
defaultTab
=
tabLayout
.
getTabAt
(
position
)
defaultTab
?.
select
()
defaultTab
?.
setSelected
()
}
})
}
/**
* 设置Tab重新选择监听器
*/
fun
setOnTabReselectedListener
(
listener
:
(
Int
)
->
Unit
)
{
onTabReselectedListener
=
listener
}
/**
* 更新Tab红点状态
*/
fun
updateTabRedDot
(
tabIndex
:
Int
,
showRedDot
:
Boolean
)
{
if
(
tabIndex
in
tabDataList
.
indices
)
{
val
tab
=
tabLayout
.
getTabAt
(
tabIndex
)
tab
?.
let
{
tabLayout
->
val
tabView
=
tabLayout
.
customView
val
redDot
=
tabView
?.
findViewById
<
View
>(
R
.
id
.
vDian
)
redDot
?.
visibility
=
if
(
showRedDot
)
View
.
VISIBLE
else
View
.
GONE
}
}
}
/**
* 设置Tab启用状态
*/
fun
setTabEnabled
(
tabIndex
:
Int
,
enabled
:
Boolean
)
{
if
(
tabIndex
in
tabDataList
.
indices
)
{
val
tab
=
tabLayout
.
getTabAt
(
tabIndex
)
tab
?.
let
{
tabLayout
->
val
tabView
=
tabLayout
.
customView
tabView
?.
isEnabled
=
enabled
tabView
?.
alpha
=
if
(
enabled
)
1.0f
else
0.5f
}
}
}
/**
* 获取Tab数据列表
*/
fun
getTabDataList
():
List
<
HomeTabUIBean
>
=
tabDataList
.
toList
()
/**
* Tab选中状态设置
*/
fun
TabLayout
.
Tab
.
setSelected
()
{
val
textView
=
customView
?.
findViewById
<
TextView
>(
R
.
id
.
tv_tab
)
textView
?.
setTextColor
(
ContextCompat
.
getColor
(
context
,
R
.
color
.
colorPrimary
)
)
}
/**
* Tab未选中状态设置
*/
fun
TabLayout
.
Tab
.
setUnselected
()
{
val
textView
=
customView
?.
findViewById
<
TextView
>(
R
.
id
.
tv_tab
)
textView
?.
setTextColor
(
"#A6A6A6"
.
toColorInt
())
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment