[Modify]首页tab持久化保存
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
package com.yizhuan.erban.home.fragment
|
package com.yizhuan.erban.home.fragment
|
||||||
|
|
||||||
|
import android.text.TextUtils
|
||||||
import android.view.Gravity
|
import android.view.Gravity
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.activityViewModels
|
import androidx.fragment.app.activityViewModels
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.reflect.TypeToken
|
||||||
import com.netease.nim.uikit.common.util.sys.ScreenUtil
|
import com.netease.nim.uikit.common.util.sys.ScreenUtil
|
||||||
import com.yizhuan.erban.R
|
import com.yizhuan.erban.R
|
||||||
import com.yizhuan.erban.application.IReportConstants
|
import com.yizhuan.erban.application.IReportConstants
|
||||||
@@ -22,9 +25,9 @@ import com.yizhuan.erban.ui.widget.magicindicator.buildins.commonnavigator.Commo
|
|||||||
import com.yizhuan.xchat_android_core.DemoCache
|
import com.yizhuan.xchat_android_core.DemoCache
|
||||||
import com.yizhuan.xchat_android_core.UriProvider
|
import com.yizhuan.xchat_android_core.UriProvider
|
||||||
import com.yizhuan.xchat_android_core.home.bean.HomeTagInfo
|
import com.yizhuan.xchat_android_core.home.bean.HomeTagInfo
|
||||||
import com.yizhuan.xchat_android_core.statistic.StatisticManager
|
import com.yizhuan.xchat_android_library.common.SpConstants
|
||||||
import com.yizhuan.xchat_android_core.statistic.protocol.StatisticsProtocol
|
|
||||||
import com.yizhuan.xchat_android_library.common.util.Logger
|
import com.yizhuan.xchat_android_library.common.util.Logger
|
||||||
|
import com.yizhuan.xchat_android_library.common.util.SPUtils
|
||||||
import com.yizhuan.xchat_android_library.utils.ResUtil
|
import com.yizhuan.xchat_android_library.utils.ResUtil
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,8 +36,8 @@ import com.yizhuan.xchat_android_library.utils.ResUtil
|
|||||||
class HomeFragment : BaseViewBindingFragment<FragmentHomeBinding>(), View.OnClickListener,
|
class HomeFragment : BaseViewBindingFragment<FragmentHomeBinding>(), View.OnClickListener,
|
||||||
PartyMagicIndicatorAdapter.OnItemSelectListener {
|
PartyMagicIndicatorAdapter.OnItemSelectListener {
|
||||||
|
|
||||||
private val mFragments: ArrayList<Fragment> = ArrayList()
|
private val mFragments = mutableListOf<Fragment>()
|
||||||
private val mTabInfoList: ArrayList<HomeTagInfo> = ArrayList()
|
private val mTabInfoList = mutableListOf<HomeTagInfo>()
|
||||||
private val homeViewModel: HomeViewModel by activityViewModels()
|
private val homeViewModel: HomeViewModel by activityViewModels()
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@@ -97,15 +100,22 @@ class HomeFragment : BaseViewBindingFragment<FragmentHomeBinding>(), View.OnClic
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onGetHomeTagSuccess(info: List<HomeTagInfo>) {
|
private fun onGetHomeTagSuccess(tagInfo: List<HomeTagInfo>) {
|
||||||
var changed = false
|
var changed = false
|
||||||
mTabInfoList.filter { it.id != 6 }
|
mTabInfoList.filter { it.id != 6 }
|
||||||
val oldTabInfoList = mTabInfoList.map { it.name }
|
val homeTabInfo = SPUtils.getString(SpConstants.TAB_INFO_LIST, "")
|
||||||
for (tagInfo in info) {
|
if (!TextUtils.isEmpty(homeTabInfo)) {
|
||||||
if (!oldTabInfoList.contains(tagInfo.name)) {
|
val homeTagInfoList: List<HomeTagInfo> =
|
||||||
|
Gson().fromJson(homeTabInfo, object : TypeToken<List<HomeTagInfo?>?>() {}.type)
|
||||||
|
if (homeTagInfoList.isNotEmpty()) {
|
||||||
|
if (homeTagInfoList != tagInfo) {
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
changed = true
|
changed = true
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
changed = true
|
||||||
}
|
}
|
||||||
//只有HomeTagInfo发生改变才刷新数据
|
//只有HomeTagInfo发生改变才刷新数据
|
||||||
if (changed) {
|
if (changed) {
|
||||||
@@ -120,17 +130,17 @@ class HomeFragment : BaseViewBindingFragment<FragmentHomeBinding>(), View.OnClic
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
mFragments.add(RecommendFragment.newInstance())
|
mFragments.add(RecommendFragment.newInstance())
|
||||||
for (i in info.indices) {
|
for (i in tagInfo.indices) {
|
||||||
//过滤对象是空和没有标签名同时也没有子标签的情况
|
//过滤对象是空和没有标签名同时也没有子标签的情况
|
||||||
var title: CharSequence?
|
var title: CharSequence?
|
||||||
var fragment: Fragment?
|
var fragment: Fragment?
|
||||||
var name = info[i].name
|
var name = tagInfo[i].name
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
name = name.trim { it <= ' ' }
|
name = name.trim { it <= ' ' }
|
||||||
}
|
}
|
||||||
title = name
|
title = name
|
||||||
mTabInfoList.add(info[i])
|
mTabInfoList.add(tagInfo[i])
|
||||||
val tabId: Int = info[i].id
|
val tabId: Int = tagInfo[i].id
|
||||||
fragment = when (title) {
|
fragment = when (title) {
|
||||||
ResUtil.getString(R.string.home_fragment_partyfragment_01) -> {
|
ResUtil.getString(R.string.home_fragment_partyfragment_01) -> {
|
||||||
RoomLikeFragment.newInstance()
|
RoomLikeFragment.newInstance()
|
||||||
@@ -144,6 +154,12 @@ class HomeFragment : BaseViewBindingFragment<FragmentHomeBinding>(), View.OnClic
|
|||||||
}
|
}
|
||||||
mFragments.add(fragment)
|
mFragments.add(fragment)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SPUtils.putString(
|
||||||
|
SpConstants.TAB_INFO_LIST,
|
||||||
|
Gson().toJson(mTabInfoList.filter { it.id != 6 })
|
||||||
|
)
|
||||||
|
|
||||||
val commonNavigator = CommonNavigator(context)
|
val commonNavigator = CommonNavigator(context)
|
||||||
commonNavigator.setTitleWrapContent(false)
|
commonNavigator.setTitleWrapContent(false)
|
||||||
commonNavigator.titleMargin = ScreenUtil.dip2px(2.0F)
|
commonNavigator.titleMargin = ScreenUtil.dip2px(2.0F)
|
||||||
@@ -170,6 +186,37 @@ class HomeFragment : BaseViewBindingFragment<FragmentHomeBinding>(), View.OnClic
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
mFragments.add(RecommendFragment.newInstance())
|
mFragments.add(RecommendFragment.newInstance())
|
||||||
|
val homeTabInfo = SPUtils.getString(SpConstants.TAB_INFO_LIST, "")
|
||||||
|
if (!TextUtils.isEmpty(homeTabInfo)) {
|
||||||
|
val homeTagInfoList: List<HomeTagInfo> =
|
||||||
|
Gson().fromJson(homeTabInfo, object : TypeToken<List<HomeTagInfo?>?>() {}.type)
|
||||||
|
if (homeTagInfoList.isNotEmpty()) {
|
||||||
|
for (i in homeTagInfoList.indices) {
|
||||||
|
//过滤对象是空和没有标签名同时也没有子标签的情况
|
||||||
|
var title: CharSequence?
|
||||||
|
var fragment: Fragment?
|
||||||
|
var name = homeTagInfoList[i].name
|
||||||
|
if (name != null) {
|
||||||
|
name = name.trim { it <= ' ' }
|
||||||
|
}
|
||||||
|
title = name
|
||||||
|
mTabInfoList.add(homeTagInfoList[i])
|
||||||
|
val tabId: Int = homeTagInfoList[i].id
|
||||||
|
fragment = when (title) {
|
||||||
|
ResUtil.getString(R.string.home_fragment_partyfragment_01) -> {
|
||||||
|
RoomLikeFragment.newInstance()
|
||||||
|
}
|
||||||
|
ResUtil.getString(R.string.home_fragment_partyfragment_02) -> {
|
||||||
|
RoomGameFragment.newInstance()
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
RoomCommonFragment.newInstance(tabId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mFragments.add(fragment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
val commonNavigator = CommonNavigator(context)
|
val commonNavigator = CommonNavigator(context)
|
||||||
commonNavigator.setTitleWrapContent(false)
|
commonNavigator.setTitleWrapContent(false)
|
||||||
commonNavigator.titleMargin = ScreenUtil.dip2px(2.0F)
|
commonNavigator.titleMargin = ScreenUtil.dip2px(2.0F)
|
||||||
@@ -192,4 +239,10 @@ class HomeFragment : BaseViewBindingFragment<FragmentHomeBinding>(), View.OnClic
|
|||||||
ReportManager.get().reportEvent(IReportConstants.TAB_HOMEPAGE_CLICK, map)
|
ReportManager.get().reportEvent(IReportConstants.TAB_HOMEPAGE_CLICK, map)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroyView() {
|
||||||
|
super.onDestroyView()
|
||||||
|
mTabInfoList.clear()
|
||||||
|
mFragments.clear()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -104,8 +104,8 @@ class RoomLikeFragment : BaseViewBindingFragment<FragmentRoomLikeBinding>() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onLazyLoad() {
|
||||||
super.onResume()
|
super.onLazyLoad()
|
||||||
loadData(true)
|
loadData(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ class RoomLikeFragment : BaseViewBindingFragment<FragmentRoomLikeBinding>() {
|
|||||||
|
|
||||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||||
fun onRefreshHomeDataEvent(event: RefreshHomeDataEvent?) {
|
fun onRefreshHomeDataEvent(event: RefreshHomeDataEvent?) {
|
||||||
if (isResumed) loadData(true)
|
// if (isResumed) loadData(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -0,0 +1,5 @@
|
|||||||
|
package com.yizhuan.xchat_android_library.common
|
||||||
|
|
||||||
|
object SpConstants {
|
||||||
|
const val TAB_INFO_LIST = "tab_info_list"
|
||||||
|
}
|
Reference in New Issue
Block a user