fix(home): 修复手表数据展示逻辑并优化首页手表模块
- WatchItemFragment 按 position 细化数据展示分支,补全各字段空值判断 - floatToInt() 改用 roundToInt() 避免数值截断误差 - HomeWatchView 新增 ViewPager2 5s 自动轮播,回首页禁用滑动动画,销毁时防内存泄漏 - 首页手表卡片空状态样式调整(高度 45→163dp,字号 12→15sp) - 首页头像图片由 jpg 替换为 png Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
53f33b35e0
commit
dcb53f344a
@@ -10,6 +10,7 @@ import com.xjjk.healthyclients.base.BaseVMBFragment
|
||||
import com.xjjk.healthyclients.bean.home.HomeWatchAllDataBean
|
||||
import com.xjjk.healthyclients.databinding.FragmentItemWatchBinding
|
||||
import com.xjjk.healthyclients.event.WatchDataChangeEvent
|
||||
import com.xjjk.healthyclients.superfuntion.floatToInt
|
||||
import com.xjjk.healthyclients.superfuntion.orEmptyDefault
|
||||
import com.xjjk.healthyclients.ui.viewmodel.HomeViewModel
|
||||
|
||||
@@ -35,8 +36,8 @@ class WatchItemFragment :
|
||||
|
||||
if (position != 4) {
|
||||
mBinding.watchType.text = "" + mBinding.watchType.getText() + getWatchTypeText(position)
|
||||
mBinding.watchLeftText.text = getWatchTypeText(position) +"范围"
|
||||
mBinding.watchRightText.text = "平均"+getWatchTypeText(position)
|
||||
mBinding.watchLeftText.text = getWatchTypeText(position) + "范围"
|
||||
mBinding.watchRightText.text = "平均" + getWatchTypeText(position)
|
||||
|
||||
} else {
|
||||
mBinding.watchType.text = "睡眠总时长"
|
||||
@@ -79,31 +80,71 @@ class WatchItemFragment :
|
||||
// 采集时间
|
||||
mBinding.watchTime.text = bean?.latestTime.orEmptyDefault()
|
||||
|
||||
if (position != 4) {
|
||||
if (position == 1 || position == 2 || position == 0) {
|
||||
// 最新值 + 单位
|
||||
if (TextUtils.isEmpty(bean?.latest)) {
|
||||
mBinding.watchValue.text = "--"
|
||||
} else {
|
||||
mBinding.watchValue.text = "${bean?.latest.floatToInt()}"
|
||||
}
|
||||
// 范围:min ~ max
|
||||
if (TextUtils.isEmpty(bean?.min)) {
|
||||
mBinding.watchLeftValue.text = "--"
|
||||
} else {
|
||||
mBinding.watchLeftValue.text =
|
||||
"${bean?.min.floatToInt()}-${bean?.max.floatToInt()}"
|
||||
}
|
||||
// 平均值
|
||||
|
||||
if (TextUtils.isEmpty(bean?.avg)) {
|
||||
mBinding.watchRightValue.text = "--"
|
||||
} else {
|
||||
mBinding.watchRightValue.text = "${bean?.avg.floatToInt()}"
|
||||
}
|
||||
|
||||
} else if (position == 3) {
|
||||
// 最新值 + 单位
|
||||
mBinding.watchValue.text = bean?.latest.orEmptyDefault()
|
||||
// 范围:min ~ max
|
||||
if (TextUtils.isEmpty(bean?.min)) {
|
||||
mBinding.watchLeftValue.text = "--"
|
||||
} else {
|
||||
mBinding.watchLeftValue.text = "${bean?.min.orEmptyDefault()}-${bean?.max.orEmptyDefault()}"
|
||||
mBinding.watchLeftValue.text =
|
||||
"${bean?.min.orEmptyDefault()}-${bean?.max.orEmptyDefault()}"
|
||||
}
|
||||
// 平均值
|
||||
mBinding.watchRightValue.text = bean?.avg.orEmptyDefault()
|
||||
} else {
|
||||
|
||||
} else if (position == 4) {
|
||||
|
||||
//睡眠数据单独处理
|
||||
if (bean == null) {
|
||||
mBinding.watchValue.text = "--"
|
||||
mBinding.watchLeftValue.text = "--"
|
||||
mBinding.watchRightValue.text = "--"
|
||||
|
||||
} else {
|
||||
mBinding.watchValue.text =
|
||||
DateUtil.timeDiff(bean.latest!!.toLong() * 1000 * 60).orEmptyDefault()
|
||||
mBinding.watchLeftValue.text =
|
||||
DateUtil.timeDiff(bean.max!!.toLong() * 1000 * 60).orEmptyDefault()
|
||||
mBinding.watchRightValue.text =
|
||||
DateUtil.timeDiff(bean.min!!.toLong() * 1000 * 60).orEmptyDefault()
|
||||
if (TextUtils.isEmpty(bean.latest)) {
|
||||
mBinding.watchValue.text = "--"
|
||||
} else {
|
||||
mBinding.watchValue.text =
|
||||
DateUtil.timeDiff(bean.latest!!.floatToInt().toLong() * 1000 * 60)
|
||||
.orEmptyDefault()
|
||||
}
|
||||
if (TextUtils.isEmpty(bean.max)) {
|
||||
mBinding.watchLeftValue.text = "--"
|
||||
} else {
|
||||
mBinding.watchLeftValue.text =
|
||||
DateUtil.timeDiff(bean.max!!.floatToInt().toLong() * 1000 * 60)
|
||||
.orEmptyDefault()
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(bean.min)) {
|
||||
mBinding.watchRightValue.text = "--"
|
||||
} else {
|
||||
mBinding.watchRightValue.text =
|
||||
DateUtil.timeDiff(bean.min!!.floatToInt().toLong() * 1000 * 60)
|
||||
.orEmptyDefault()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ fun String?.floatToInt(i: Int = 0): Int {
|
||||
i
|
||||
} else {
|
||||
if (this!!.toFloatOrNull() != null) {
|
||||
this.toFloatOrNull()!!.toInt()
|
||||
this.toFloatOrNull()!!.roundToInt()
|
||||
} else {
|
||||
i
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.xjjk.healthyclients.view
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
@@ -22,23 +24,49 @@ import org.greenrobot.eventbus.EventBus
|
||||
class HomeWatchView(context: Context?, attrs: AttributeSet?) :
|
||||
RelativeLayout(context, attrs, 0), ViewPagerAdapter.OnCreateFragmentListener {
|
||||
|
||||
companion object {
|
||||
/** 自动轮播间隔时间(毫秒) */
|
||||
private const val AUTO_PLAY_INTERVAL = 5000L
|
||||
}
|
||||
|
||||
var mContext: Context? = context
|
||||
var mBinding: LayoutHomeItemWatchBinding = DataBindingUtil.inflate(
|
||||
LayoutInflater.from(context),
|
||||
R.layout.layout_home_item_watch, this, true
|
||||
)
|
||||
|
||||
/** 自动轮播 Handler */
|
||||
private val autoPlayHandler = Handler(Looper.getMainLooper())
|
||||
|
||||
/** 自动轮播 Runnable,每隔5s循环滚动到下一页 */
|
||||
private val autoPlayRunnable = object : Runnable {
|
||||
override fun run() {
|
||||
val itemCount = mBinding.viewPager.adapter?.itemCount ?: 0
|
||||
if (itemCount > 0) {
|
||||
val nextItem = (mBinding.viewPager.currentItem + 1) % itemCount
|
||||
if (nextItem == 0) {
|
||||
mBinding.viewPager.setCurrentItem(nextItem, false)
|
||||
} else {
|
||||
mBinding.viewPager.setCurrentItem(nextItem, true)
|
||||
}
|
||||
}
|
||||
autoPlayHandler.postDelayed(this, AUTO_PLAY_INTERVAL)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun init(fragment: Fragment) {
|
||||
val arrayOf = arrayOf("心脑血管", "癌症", "慢呼", "糖尿病", "亚健康")
|
||||
mBinding.watchTabLayout.onCreateTab(arrayOf, 1) {tab->
|
||||
if (tab?.position != 0) {
|
||||
mBinding.emptyText.visibility = View.VISIBLE
|
||||
mBinding.emptyText.text = "开发中"
|
||||
mBinding.emptyText.text = "紧急开发中"
|
||||
mBinding.viewPager.visibility = View.GONE
|
||||
stopAutoPlay()
|
||||
} else {
|
||||
mBinding.viewPager.visibility = View.VISIBLE
|
||||
mBinding.emptyText.visibility = View.GONE
|
||||
startAutoPlay()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -48,6 +76,24 @@ class HomeWatchView(context: Context?, attrs: AttributeSet?) :
|
||||
tabList.add(TabItemBean())
|
||||
}
|
||||
mBinding.viewPager.init(fragment, tabList.toTypedArray(), this, 5)
|
||||
startAutoPlay()
|
||||
}
|
||||
|
||||
/** 启动自动轮播 */
|
||||
fun startAutoPlay() {
|
||||
autoPlayHandler.removeCallbacks(autoPlayRunnable)
|
||||
autoPlayHandler.postDelayed(autoPlayRunnable, AUTO_PLAY_INTERVAL)
|
||||
}
|
||||
|
||||
/** 停止自动轮播 */
|
||||
fun stopAutoPlay() {
|
||||
autoPlayHandler.removeCallbacks(autoPlayRunnable)
|
||||
}
|
||||
|
||||
override fun onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow()
|
||||
// View 移除时停止轮播,防止内存泄漏
|
||||
stopAutoPlay()
|
||||
}
|
||||
|
||||
fun setWatchInfo(watchInfo: WatchInfoBean?) {
|
||||
@@ -78,4 +124,4 @@ class HomeWatchView(context: Context?, attrs: AttributeSet?) :
|
||||
return WatchItemFragment().newInstance(position)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 306 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 83 KiB |
@@ -35,11 +35,11 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginTop="12dp"
|
||||
android:layout_marginRight="36dp"
|
||||
android:ellipsize="end"
|
||||
android:textColor="#14BEBE"
|
||||
android:textSize="13sp"
|
||||
android:textSize="12sp"
|
||||
tools:text="监测工具编码:4PMBB25A28100553" />
|
||||
|
||||
<ImageView
|
||||
@@ -91,14 +91,14 @@
|
||||
<TextView
|
||||
android:id="@+id/empty_text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="45dp"
|
||||
android:layout_height="163dp"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginTop="55dp"
|
||||
android:background="@color/white"
|
||||
android:gravity="center"
|
||||
android:text="暂未登录"
|
||||
android:textColor="#77849E"
|
||||
android:textSize="12sp" />
|
||||
android:textSize="15sp" />
|
||||
|
||||
</com.allen.library.shape.ShapeRelativeLayout>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user