RecyclerView不更新View

时间:2015-12-15 23:24:55

标签: android android-recyclerview

我有一个回收者视图。这是第一次加载所有数据,视图显示完美。但是当动态添加数据时,拉到刷新,我将其添加到数据列表并调用notifyDataSetChanged。它会增加列表的计数,并使用最新数据更新列表,但视图不会使用添加到列表中的最新数据进行更新。 onBindViewHolder具有更新的listView大小,listView包含其中的所有有效数据。

    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.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)
                        }
                    }
                }
            }
        }

    }

ActionView是Enum。

如果我做错了,请告诉我。

0 个答案:

没有答案