我正在Kotlin中的Android Studio中制作一个应用程序,其片段与Faribase平台同步,其中在片段中制作RecyclerView和Adapter作为网页列表,一切正常,但是当我将手机从垂直到水平,或者当我离开片段并返回时,元素会重复。我不知道错误是什么,如果你能帮助我,我认为错误出在适配器中。
class ShareFragment : Fragment() {
private lateinit var shareBinding: FragmentShareBinding
private lateinit var shareViewModel: ShareViewModel
private lateinit var shareAdapter: ShareAdapter
private var shareVehicle: ArrayList<PaperworkServer> = ArrayList()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
shareBinding = FragmentShareBinding.inflate(inflater, container, false)
shareViewModel = ViewModelProvider(this)[ShareViewModel::class.java]
return shareBinding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
shareViewModel.loadShareFromServerDone.observe(viewLifecycleOwner) { result ->
onLoadChareFromServerDoneSubscribe(result)
}
shareViewModel.loadPaperworkServer()
shareAdapter = ShareAdapter(shareVehicle, onItemClicked = { onShareItemClicked(it) })
shareBinding.shareView.apply {
layoutManager = LinearLayoutManager([email protected]())
adapter = shareAdapter
setHasFixedSize(false)
}
}
private fun onShareItemClicked(share: PaperworkServer) {
findNavController().navigate(ShareFragmentDirections.actionShareFragmentToLinkTwoFragment(share))
}
private fun onLoadChareFromServerDoneSubscribe(shareServerLoaded: ArrayList<PaperworkServer>) {
shareVehicle = shareServerLoaded
shareAdapter.appendItems(shareVehicle)
}
}
适配器
class ShareAdapter(
private val shareList: ArrayList<PaperworkServer>,
private val onItemClicked: (PaperworkServer) -> Unit
) : RecyclerView.Adapter<ShareAdapter.ShareViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ShareViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.card_view_item_share, parent, false)
return ShareViewHolder(view)
}
override fun onBindViewHolder(holder: ShareViewHolder, position: Int) {
val share = shareList[position]
holder.bind(share)
holder.itemView.setOnClickListener{ onItemClicked(shareList[position])}
}
override fun getItemCount(): Int = shareList.size
fun appendItems(newList: ArrayList<PaperworkServer>){
shareList.clear()
shareList.addAll(newList)
notifyDataSetChanged()
}
class ShareViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
private val binding = CardViewItemShareBinding.bind(itemView)
fun bind(share: PaperworkServer){
with(binding){
titleLinkTextView.text = share.name
cardShareTextView.text = share.information
}
}
}
}
视图模型
class ShareViewModel : ViewModel() {
val paperworkServerRepository = PaperworkServerRepository ()
private var shareVehicle: ArrayList<PaperworkServer> = ArrayList()
val loadPaperworkServer: MutableLiveData<ArrayList<PaperworkServer>> = MutableLiveData()
val loadShareFromServerDone: LiveData<ArrayList<PaperworkServer>> = loadPaperworkServer
fun loadPaperworkServer (){
GlobalScope.launch(Dispatchers.IO){
val querySnapshot = paperworkServerRepository.share()
for (document in querySnapshot) {
val paperworkServer: PaperworkServer = document.toObject<PaperworkServer>()
shareVehicle.add(paperworkServer)
}
loadPaperworkServer.postValue(shareVehicle)
}
}
}
操作截图
旋转屏幕或从片段移动到片段时出错,元素重复
我尝试修改适配器但没有结果。另外,notifyDataSetChanged() 方法不起作用。
要处理屏幕旋转,请检查 savingInstanceState 并将此代码添加到块中
if (savedInstanceState == null) {
shareViewModel.loadPaperworkServer()
shareAdapter = ShareAdapter(shareVehicle, onItemClicked = { onShareItemClicked(it) })
shareBinding.shareView.apply {
layoutManager = LinearLayoutManager([email protected]())
adapter = shareAdapter
setHasFixedSize(false)
}
}