优化列表请求失败和没有网络的显示状态,优化渐变颜色展示

This commit is contained in:
2026-06-16 15:40:20 +08:00
parent b2c3ed9015
commit 631f9831c3
24 changed files with 149 additions and 32 deletions

View File

@@ -3,25 +3,18 @@ package com.ckkj.defense_device.adapter
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.ckkj.defense_device.R import com.ckkj.defense_device.R
import com.ckkj.defense_device.bean.DeviceBean
import com.ckkj.defense_device.databinding.ItemDeviceBinding import com.ckkj.defense_device.databinding.ItemDeviceBinding
data class DeviceUi(
val name: String,
val status: String,
val deviceId: String = ""
) {
val isOnline: Boolean
get() = status == "1"
}
class DeviceAdapter( class DeviceAdapter(
private val onItemClick: (DeviceUi) -> Unit = {} private val onItemClick: (DeviceBean) -> Unit = {}
) : RecyclerView.Adapter<DeviceAdapter.DeviceViewHolder>() { ) : RecyclerView.Adapter<DeviceAdapter.DeviceViewHolder>() {
private val items = mutableListOf<DeviceUi>() private val items = mutableListOf<DeviceBean>()
fun submitList(deviceList: List<DeviceUi>) { fun submitList(deviceList: List<DeviceBean>) {
items.clear() items.clear()
items.addAll(deviceList) items.addAll(deviceList)
notifyDataSetChanged() notifyDataSetChanged()
@@ -33,15 +26,25 @@ class DeviceAdapter(
} }
override fun onBindViewHolder(holder: DeviceViewHolder, position: Int) { override fun onBindViewHolder(holder: DeviceViewHolder, position: Int) {
holder.bind(items[position]) holder.bind(items[position], position)
} }
override fun getItemCount(): Int = items.size override fun getItemCount(): Int = items.size
private val defaultIcons = listOf(R.mipmap.img1, R.mipmap.img2, R.mipmap.img3)
inner class DeviceViewHolder(private val binding: ItemDeviceBinding) : inner class DeviceViewHolder(private val binding: ItemDeviceBinding) :
RecyclerView.ViewHolder(binding.root) { RecyclerView.ViewHolder(binding.root) {
fun bind(item: DeviceUi) { fun bind(item: DeviceBean, position: Int) {
if (item.deviceImg.isNullOrEmpty()) {
val iconResId = defaultIcons[position % defaultIcons.size]
binding.ivDeviceIcon.setImageResource(iconResId)
} else {
Glide.with(binding.root.context)
.load(item.deviceImg)
.into(binding.ivDeviceIcon)
}
binding.tvDeviceType.text = item.name binding.tvDeviceType.text = item.name
when (item.status) { when (item.status) {
"1" -> { "1" -> {

View File

@@ -27,4 +27,13 @@ data class DeviceBean(
val wifiPassword: String? = null,//WiFi密码 val wifiPassword: String? = null,//WiFi密码
val workMode: String? = null,//工作模式 0-自动 1-半自动 val workMode: String? = null,//工作模式 0-自动 1-半自动
val optionPassword: String? = null, //操作密码 val optionPassword: String? = null, //操作密码
) ) {
val deviceId: String
get() = id ?: ""
val name: String
get() = deviceName ?: ""
val isOnline: Boolean
get() = status == "1"
}

View File

@@ -23,7 +23,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.ckkj.defense_device.R import com.ckkj.defense_device.R
import com.ckkj.defense_device.adapter.DeviceAdapter import com.ckkj.defense_device.adapter.DeviceAdapter
import com.ckkj.defense_device.adapter.DeviceUi
import com.ckkj.defense_device.adapter.FamilyAdapter import com.ckkj.defense_device.adapter.FamilyAdapter
import com.ckkj.defense_device.bean.DeviceBean import com.ckkj.defense_device.bean.DeviceBean
import com.ckkj.defense_device.bean.FamilyBean import com.ckkj.defense_device.bean.FamilyBean
@@ -200,8 +199,8 @@ class HomeMenuFragment : Fragment() {
// 为 LinearLayout 设置渐变背景 // 为 LinearLayout 设置渐变背景
setGradientBackground( setGradientBackground(
view = binding.gradientLayout, view = binding.gradientLayout,
startColor = Color.parseColor("#EBDCED"), startColor = resources.getColor(R.color.gradientLayout1),
endColor = Color.parseColor("#CAF4FF") endColor = resources.getColor(R.color.gradientLayout2)
) )
binding.message.setOnClickListener { binding.message.setOnClickListener {
val intent = Intent(requireContext(), MessageActivity::class.java) val intent = Intent(requireContext(), MessageActivity::class.java)
@@ -404,7 +403,7 @@ class HomeMenuFragment : Fragment() {
} catch (e: Exception) { } catch (e: Exception) {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
binding.rvDeviceList.visibility = View.GONE binding.rvDeviceList.visibility = View.GONE
binding.layoutNoPermission.visibility = View.VISIBLE binding.layoutNoNetwork.visibility = View.VISIBLE
Toast.makeText(requireContext(), R.string.network_error, Toast.LENGTH_SHORT).show() Toast.makeText(requireContext(), R.string.network_error, Toast.LENGTH_SHORT).show()
isLoading = false isLoading = false
if (isLoadMore) { if (isLoadMore) {
@@ -450,20 +449,16 @@ class HomeMenuFragment : Fragment() {
} }
private fun updateDeviceList() { private fun updateDeviceList() {
val deviceUiList = deviceBeanList.map { if (deviceBeanList.isEmpty()) {
DeviceUi(it.deviceName ?: "", it.status ?: "", it.id ?: "")
}
if (deviceUiList.isEmpty()) {
deviceAdapter.submitList(listOf( deviceAdapter.submitList(listOf(
DeviceUi(getString(R.string.no_device), "0") DeviceBean(deviceName = getString(R.string.no_device), status = "0")
)) ))
} else { } else {
deviceAdapter.submitList(deviceUiList) deviceAdapter.submitList(deviceBeanList)
} }
} }
private fun onDeviceClicked(device: DeviceUi) { private fun onDeviceClicked(device: DeviceBean) {
if (device.isOnline) { if (device.isOnline) {
DeviceIdEvent.setDeviceId(device.deviceId) DeviceIdEvent.setDeviceId(device.deviceId)
val intent = Intent(requireContext(), LiveVideoActivity::class.java) val intent = Intent(requireContext(), LiveVideoActivity::class.java)

View File

@@ -125,7 +125,7 @@ class MessageActivity : AppCompatActivity() {
e.printStackTrace() e.printStackTrace()
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
if (allMessages.isEmpty()) { if (allMessages.isEmpty()) {
showEmptyState() noNetwork()
} }
} }
} finally { } finally {
@@ -152,6 +152,12 @@ class MessageActivity : AppCompatActivity() {
binding.emptyLayout.visibility = View.GONE binding.emptyLayout.visibility = View.GONE
} }
private fun noNetwork() {
binding.rvMessageList.visibility = View.GONE
binding.layoutNoNetwork.visibility = View.VISIBLE
binding.tvNoMoreData.visibility = View.GONE
}
private fun showNoMoreDataHint() { private fun showNoMoreDataHint() {
binding.tvNoMoreData.visibility = View.VISIBLE binding.tvNoMoreData.visibility = View.VISIBLE
} }

View File

@@ -3,6 +3,7 @@ package com.ckkj.defense_device.liveVideo
import android.os.Bundle import android.os.Bundle
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
import android.view.View
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
@@ -95,19 +96,33 @@ class AddLinkedUserActivity : AppCompatActivity() {
Toast.makeText(this@AddLinkedUserActivity, "暂无家庭成员数据", Toast.LENGTH_SHORT).show() Toast.makeText(this@AddLinkedUserActivity, "暂无家庭成员数据", Toast.LENGTH_SHORT).show()
} }
} else { } else {
hide()
Toast.makeText(this@AddLinkedUserActivity, "请求失败: ${response.msg}", Toast.LENGTH_SHORT).show() Toast.makeText(this@AddLinkedUserActivity, "请求失败: ${response.msg}", Toast.LENGTH_SHORT).show()
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
android.util.Log.e("AddLinkedUserActivity", "网络请求异常", e)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
binding.swipeRefresh.isRefreshing = false binding.swipeRefresh.isRefreshing = false
noNetwork()
Toast.makeText(this@AddLinkedUserActivity, "网络请求失败: ${e.message}", Toast.LENGTH_SHORT).show() Toast.makeText(this@AddLinkedUserActivity, "网络请求失败: ${e.message}", Toast.LENGTH_SHORT).show()
} }
} }
} }
} }
private fun hide() {
binding.searchLayout.visibility = View.GONE
binding.rvAddLinkedUser.visibility = View.GONE
binding.butting?.visibility = View.GONE
binding.layoutNoPermission?.visibility = View.VISIBLE
}
private fun noNetwork() {
binding.searchLayout.visibility = View.GONE
binding.rvAddLinkedUser.visibility = View.GONE
binding.butting?.visibility = View.GONE
binding.layoutNoNetwork?.visibility = View.VISIBLE
}
private fun updateFamilyMemberList(familyMembersList: List<FamilyMembersBean>) { private fun updateFamilyMemberList(familyMembersList: List<FamilyMembersBean>) {
fullUserItemList.clear() fullUserItemList.clear()
familyMembersList.forEach { member -> familyMembersList.forEach { member ->

View File

@@ -265,8 +265,8 @@ class LinkedUserActivity : AppCompatActivity() {
e.printStackTrace() e.printStackTrace()
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
binding.swipeRefresh.isRefreshing = false binding.swipeRefresh.isRefreshing = false
hide() noNetwork()
Toast.makeText(this@LinkedUserActivity, "网络请求失败", Toast.LENGTH_SHORT).show() Toast.makeText(this@LinkedUserActivity, R.string.network_request_failed, Toast.LENGTH_SHORT).show()
} }
} }
} }
@@ -277,6 +277,12 @@ class LinkedUserActivity : AppCompatActivity() {
binding.butting?.visibility = View.GONE binding.butting?.visibility = View.GONE
binding.layoutNoPermission?.visibility = View.VISIBLE binding.layoutNoPermission?.visibility = View.VISIBLE
} }
private fun noNetwork() {
binding.searchLayout.visibility = View.GONE
binding.rvLinkedUser.visibility = View.GONE
binding.butting?.visibility = View.GONE
binding.layoutNoNetwork?.visibility = View.VISIBLE
}
private fun updateDeviceUserList(deviceUserList: List<DeviceUserBean>) { private fun updateDeviceUserList(deviceUserList: List<DeviceUserBean>) {
fullUserList.clear() fullUserList.clear()

View File

@@ -88,8 +88,41 @@
android:paddingBottom="16dp"/> android:paddingBottom="16dp"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<!-- 空页面 -->
<LinearLayout
android:id="@+id/layoutNoPermission"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:layout_width="244dp"
android:layout_height="131dp"
android:src="@mipmap/default_page"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layoutNoNetwork"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingHorizontal="32dp"
android:background="@color/white"
android:visibility="gone">
<ImageView
android:layout_width="244dp"
android:layout_height="131dp"
android:src="@mipmap/no_network_img_name" />
</LinearLayout>
<!-- 底部按钮 --> <!-- 底部按钮 -->
<LinearLayout <LinearLayout
android:id="@+id/butting"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingHorizontal="16dp" android:paddingHorizontal="16dp"

View File

@@ -168,6 +168,22 @@
android:src="@mipmap/default_page" /> android:src="@mipmap/default_page" />
</LinearLayout> </LinearLayout>
<LinearLayout
android:id="@+id/layoutNoNetwork"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingHorizontal="32dp"
android:background="@color/white"
android:visibility="gone">
<ImageView
android:layout_width="244dp"
android:layout_height="131dp"
android:src="@mipmap/no_network_img_name" />
</LinearLayout>
</LinearLayout> </LinearLayout>
<View <View

View File

@@ -112,6 +112,22 @@
android:src="@mipmap/default_page"/> android:src="@mipmap/default_page"/>
</LinearLayout> </LinearLayout>
<LinearLayout
android:id="@+id/layoutNoNetwork"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingHorizontal="32dp"
android:background="@color/white"
android:visibility="gone">
<ImageView
android:layout_width="244dp"
android:layout_height="131dp"
android:src="@mipmap/no_network_img_name" />
</LinearLayout>
</FrameLayout> </FrameLayout>

View File

@@ -58,7 +58,23 @@
<ImageView <ImageView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:src="@mipmap/no_message" /> android:src="@mipmap/default_page_img_name" />
</LinearLayout>
<LinearLayout
android:id="@+id/layoutNoNetwork"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingHorizontal="32dp"
android:background="@color/white"
android:visibility="gone">
<ImageView
android:layout_width="244dp"
android:layout_height="131dp"
android:src="@mipmap/no_network_img_name" />
</LinearLayout> </LinearLayout>
<TextView <TextView

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

@@ -20,5 +20,7 @@
<color name="green_light">#E8F7F7</color> <color name="green_light">#E8F7F7</color>
<color name="gray_divider">#E5E5E5</color> <color name="gray_divider">#E5E5E5</color>
<color name="orange">#FF9800</color> <color name="orange">#FF9800</color>
<color name="gradientLayout1">#EBDCED</color>
<color name="gradientLayout2">#CAF4FF</color>
</resources> </resources>