wifi选择弹窗修改

This commit is contained in:
mxy6664
2026-08-03 16:01:53 +08:00
parent 638233279f
commit a05cebae3f
4 changed files with 21 additions and 45 deletions

View File

@@ -2,6 +2,7 @@ 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.DiffUtil
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.ckkj.defense_device.databinding.ItemWifiBinding import com.ckkj.defense_device.databinding.ItemWifiBinding
@@ -39,14 +40,25 @@ class WifiListAdapter(
override fun getItemCount(): Int = wifiList.size override fun getItemCount(): Int = wifiList.size
fun updateData(newList: List<String>) { fun updateData(newList: List<String>) {
val oldList = wifiList.toList()
val diffResult = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
override fun getOldListSize() = oldList.size
override fun getNewListSize() = newList.size
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
oldList[oldItemPosition] == newList[newItemPosition]
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
oldList[oldItemPosition] == newList[newItemPosition]
})
wifiList.clear() wifiList.clear()
wifiList.addAll(newList) wifiList.addAll(newList)
notifyDataSetChanged() diffResult.dispatchUpdatesTo(this)
} }
fun addData(newList: List<String>) { fun addData(newList: List<String>) {
val startPosition = wifiList.size val startPosition = wifiList.size
val newItems = newList.filter { !wifiList.contains(it) } val newItems = newList.filter { !wifiList.contains(it) }
if (newItems.isEmpty()) return
wifiList.addAll(newItems) wifiList.addAll(newItems)
notifyItemRangeInserted(startPosition, newItems.size) notifyItemRangeInserted(startPosition, newItems.size)
} }

View File

@@ -144,10 +144,6 @@ class AddFamilyActivity : AppCompatActivity() {
} }
dialogBinding.rvWifiList.adapter = wifiListAdapter dialogBinding.rvWifiList.adapter = wifiListAdapter
dialogBinding.swipeRefreshLayout.setOnRefreshListener {
refreshWifiList(dialogBinding)
}
dialogBinding.rvWifiList.addOnScrollListener(object : RecyclerView.OnScrollListener() { dialogBinding.rvWifiList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy) super.onScrolled(recyclerView, dx, dy)
@@ -168,16 +164,6 @@ class AddFamilyActivity : AppCompatActivity() {
return rect.height() return rect.height()
} }
private fun refreshWifiList(dialogBinding: DialogWifiListBinding) {
dialogBinding.swipeRefreshLayout.isRefreshing = true
handler.postDelayed({
wifiManager.startScan()
val newWifiList = getWifiList()
wifiListAdapter?.updateData(newWifiList)
dialogBinding.swipeRefreshLayout.isRefreshing = false
}, 1000)
}
private fun loadMoreWifiList() { private fun loadMoreWifiList() {
isLoadingMore = true isLoadingMore = true
handler.postDelayed({ handler.postDelayed({
@@ -204,12 +190,8 @@ class AddFamilyActivity : AppCompatActivity() {
} }
private fun updateWifiList() { private fun updateWifiList() {
popupWindow?.contentView?.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.rvWifiList)?.let { recyclerView -> popupWindow?.contentView?.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.rvWifiList)?.let {
val wifiList = getWifiList() wifiListAdapter?.updateData(getWifiList())
recyclerView.adapter = WifiListAdapter(wifiList) { wifiName ->
binding.etWifiName.setText(wifiName)
popupWindow?.dismiss()
}
} }
} }

View File

@@ -202,14 +202,6 @@ class WiFiManagementActivity : AppCompatActivity() {
} }
dialogBinding.rvWifiList.adapter = adapter dialogBinding.rvWifiList.adapter = adapter
// 添加下拉刷新功能
dialogBinding.swipeRefreshLayout.setOnRefreshListener {
wifiManager.startScan()
// 延迟1秒后停止刷新动画
android.os.Handler().postDelayed({
dialogBinding.swipeRefreshLayout.isRefreshing = false
}, 1000)
}
} }
private fun updateBottomButtonVisibility() { private fun updateBottomButtonVisibility() {
@@ -229,9 +221,6 @@ class WiFiManagementActivity : AppCompatActivity() {
} }
private fun updateWifiList() { private fun updateWifiList() {
popupWindow?.contentView?.findViewById<androidx.swiperefreshlayout.widget.SwipeRefreshLayout>(R.id.swipeRefreshLayout)?.let { swipeRefreshLayout ->
swipeRefreshLayout.isRefreshing = false
}
popupWindow?.contentView?.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.rvWifiList)?.let { recyclerView -> popupWindow?.contentView?.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.rvWifiList)?.let { recyclerView ->
val wifiList = getWifiList() val wifiList = getWifiList()
recyclerView.adapter = WifiListAdapter(wifiList) { wifiName -> recyclerView.adapter = WifiListAdapter(wifiList) { wifiName ->
@@ -253,4 +242,4 @@ class WiFiManagementActivity : AppCompatActivity() {
unregisterReceiver(wifiScanReceiver) unregisterReceiver(wifiScanReceiver)
popupWindow?.dismiss() popupWindow?.dismiss()
} }
} }

View File

@@ -20,18 +20,11 @@
android:layout_height="1dp" android:layout_height="1dp"
android:background="@color/dialog_view" /> android:background="@color/dialog_view" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout <androidx.recyclerview.widget.RecyclerView
android:id="@+id/swipeRefreshLayout" android:id="@+id/rvWifiList"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1"> android:layout_weight="1"
android:overScrollMode="never" />
<androidx.recyclerview.widget.RecyclerView </LinearLayout>
android:id="@+id/rvWifiList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>