diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/GroupServerFragment.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/GroupServerFragment.kt index 1734f4f6..be4f406f 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/GroupServerFragment.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/GroupServerFragment.kt @@ -1,6 +1,5 @@ package com.v2ray.ang.ui -import android.annotation.SuppressLint import android.os.Bundle import android.util.Log import android.view.LayoutInflater @@ -33,7 +32,6 @@ class GroupServerFragment : BaseFragment() { override fun inflateBinding(inflater: LayoutInflater, container: ViewGroup?) = FragmentGroupServerBinding.inflate(inflater, container, false) - @SuppressLint("NotifyDataSetChanged") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { adapter = MainRecyclerAdapter(requireActivity() as MainActivity) @@ -54,11 +52,7 @@ class GroupServerFragment : BaseFragment() { return@observe } Log.d(TAG, "GroupServerFragment updateListAction subId=$subId") - if (index >= 0) { - adapter.notifyItemChanged(index) - } else { - adapter.notifyDataSetChanged() - } + adapter.setData(mainViewModel.serversCache, index) } mainViewModel.isRunning.observe(viewLifecycleOwner) { isRunning -> adapter.isRunning = isRunning diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt index 931498ac..7d1728a5 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt @@ -1,5 +1,6 @@ package com.v2ray.ang.ui +import android.annotation.SuppressLint import android.content.Intent import android.graphics.Color import android.text.TextUtils @@ -19,6 +20,7 @@ import com.v2ray.ang.databinding.ItemRecyclerFooterBinding import com.v2ray.ang.databinding.ItemRecyclerMainBinding import com.v2ray.ang.dto.EConfigType import com.v2ray.ang.dto.ProfileItem +import com.v2ray.ang.dto.ServersCache import com.v2ray.ang.extension.toast import com.v2ray.ang.extension.toastError import com.v2ray.ang.extension.toastSuccess @@ -44,17 +46,24 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter = emptyList() - /** - * Gets the total number of items in the adapter (servers count + footer view) - * @return The total item count - */ - override fun getItemCount() = mActivity.mainViewModel.serversCache.size + 1 + @SuppressLint("NotifyDataSetChanged") + fun setData(newData: List?, position: Int = -1) { + data = newData ?: emptyList() + if (position >= 0) { + notifyItemChanged(position) + } else { + notifyDataSetChanged() + } + } + + override fun getItemCount() = data.size + 1 override fun onBindViewHolder(holder: BaseViewHolder, position: Int) { if (holder is MainViewHolder) { - val guid = mActivity.mainViewModel.serversCache[position].guid - val profile = mActivity.mainViewModel.serversCache[position].profile + val guid = data[position].guid + val profile = data[position].profile val isCustom = profile.configType == EConfigType.CUSTOM || profile.configType == EConfigType.POLICYGROUP holder.itemView.setBackgroundColor(Color.TRANSPARENT) @@ -292,7 +301,7 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter