0

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

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

        }
4

3 回答 3

0

在覆盖 RecyclerView Adapter 的 onBindViewHoder() 方法时,您不应删除或添加任何视图,因为下次使用回收的布局时,将找不到已删除的视图。取而代之的是,您可以在视图上使用显示/隐藏。

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

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

于 2016-09-08T05:33:52.083 回答
0

我已经实现了 RecyclerView 和 Retrofit,它具有 SwipeView 布局(拉到刷新)。这是存储库的链接。 https://github.com/frankodoom/Retrofit-RecyclerVew

于 2017-02-10T09:01:18.957 回答
0

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

其次,您不需要在 RecyclerView 项目中动态添加或删除视图,仅在VISIBLE和之间切换视图的可见性更简单、更有效GONE。如果由于视图差异太大而这还不够,您应该声明不同的视图类型,这将为ViewHolder每种视图类型创建单独的 s。

于 2016-01-23T11:22:39.770 回答