2016-01-22 50 views
0

我有一个recyclerView。当我进行刷新时,如果新数据只是一个清单项目,则回收站视图会完美地加载项目。但是,如果更新的数据包含2个或更多,那么我认为视图不能正确回收。在actionContainer中,应该只为每个更新的列表项添加一个项目。但是在拉动刷新期间,只有当有2个或更多的列表项被更新时,actionContainer会显示2个数据,它应该只有一个。有人可以帮我解决这个问题吗?Recycler查看回收问题

override fun onBindViewHolder(holder: HistoryListAdapter.ViewHolder?, position: Int) { 
      info("onBindViewHolder =>"+listAssets.size) 
      info("onBindViewHolder itemCount =>"+itemCount) 
      info("onBindViewHolder position =>"+position) 
     val notesButton = holder?.notesButton 
     val notesView = holder?.notesTextView 

     val dateTime = listAssets[position].date 
     val location = listAssets[position].location 

     val sessionId = listAssets[position].id 
     holder?.sessionID = sessionId 
     holder?.portraitImageView?.setImageDrawable(listAssets[position].image) 
     holder?.titleTextView?.text = DateTimeFormatter.getFormattedDate(context, dateTime) 

     val timeString = DateTimeFormatter.getFormattedTime(context, dateTime) 

     if (location.length != 0) { 
      holder?.subtitleTextView?.text = "$timeString @ $location" 
     } else { 
      holder?.subtitleTextView?.text = "$timeString" 
     } 

     val data = listAssets[position].data 

     for (actionData in data) { 
      val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater 
      val parent = inflater.inflate(R.layout.history_card_action, null) 

      val icon = parent?.findViewById(R.id.historyActionIcon) as ImageView 
      val title = parent?.findViewById(R.id.historyActionTitle) as TextView 
      val subtitle = parent?.findViewById(R.id.historyActionSubtitle) as TextView 

      var iconDrawable: Drawable? = null 

      when(actionData.type) { 

       ActionType.HEART -> { 
        iconDrawable = ContextCompat.getDrawable(context, R.drawable.heart) 
       } 
       ActionType.LUNGS -> { 
        iconDrawable = ContextCompat.getDrawable(context, R.drawable.lungs) 
       } 
       ActionType.TEMPERATURE -> { 
        iconDrawable = ContextCompat.getDrawable(context, R.drawable.temperature) 
       } 
      } 

      icon.setImageDrawable(iconDrawable) 

      val titleString = actionData.title 

      titleString?.let { 
       title.text = titleString 
      } 

      val subtitleString = actionData.subtitle 

      subtitleString?.let { 
       subtitle.text = subtitleString 
      } 

      holder?.actionContainer?.addView(parent) 
     } 


     val notes = listAssets[position].notes 
     notesView?.text = notes 

     if (notes.length == 0) { 
      notesButton?.layoutParams?.width = 0 
     } else { 
      notesButton?.layoutParams?.width = toggleButtonWidth 
     } 

     if (expandedNotes.contains(sessionId)) { 
      notesView?.expandWithoutAnimation() 
     } else { 
      notesView?.collapseWithoutAnimation() 
     } 

     notesButton?.onClick { 
      notesView?.toggleExpansion() 
     } 
    } 


     data class ListAssets(val id: String, 
            val date: Date, 
            val location: String, 
            val notes: String, 
            val image: Drawable, 
            val data: ArrayList<ListData>) 

      data class ListData(val type: ActionType, 
           val title: String?, 
           val subtitle: String?) 

    override fun onViewRecycled(holder: HistoryListAdapter.ViewHolder?) { 
      super.onViewRecycled(holder) 

      if (holder != null) { 
    holder.actionContainer.removeAllViewsInLayout() 
       holder.actionContainer.removeAllViews() 

       val notesTextView = holder.notesTextView 

       if (notesTextView != null) { 
        if (notesTextView.expandedState) { 
         val sessionID = holder.sessionID 

         sessionID?.let { 
          val sessionSearch = expandedNotes.firstOrNull { 
           it.contentEquals(sessionID) 
          } 

          if (sessionSearch == null) { 
           expandedNotes.add(sessionID) 
          } 
         } 

        } else { 
         val sessionID = holder.sessionID 

         sessionID?.let { 
          val sessionSearch = expandedNotes.firstOrNull { 
           it.contentEquals(sessionID) 
          } 

          if (sessionSearch != null) { 
           expandedNotes.remove(sessionSearch) 
          } 
         } 
        } 
       } 
      } 

     } 

回答

0

首先,除非您必须执行一些非常特殊的资源清理,否则您应该不会覆盖onViewRecycled()。 您想要在显示之前设置您的视图的地方是onBindViewHolder()

其次,您不需要在RecyclerView项目中动态添加或删除视图,只需切换VISIBLEGONE之间视图的可见性就可以更简单,更高效。在这种情况下,如果视图太不相同,则应该声明不同的视图类型,这将为每个视图类型创建单独的ViewHolder

0

在重写RecyclerView Adapter的BindViewHoder()方法时,不应删除或添加任何视图,因为下次使用循环布局时,将不会找到已删除的视图。代替这个,你可以在视图上使用显示/隐藏。

如果您动态地将任何视图添加到布局,稍后再回收此布局时,它还包含之前添加的额外视图。

同样,如果您从布局中动态删除任何视图,稍后在回收此布局时,它将不包含您之前删除的视图。