2016-01-22 99 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()方法時,不應刪除或添加任何視圖,因爲下次使用循環佈局時,將不會找到已刪除的視圖。代替這個,你可以在視圖上使用顯示/隱藏。

如果您動態地將任何視圖添加到佈局,稍後再回收此佈局時,它還包含之前添加的額外視圖。

同樣,如果您從佈局中動態刪除任何視圖,稍後在回收此佈局時,它將不包含您之前刪除的視圖。