RecyclerView的setIsRecyclable无法正常工作

时间:2019-12-14 20:38:28

标签: android android-recyclerview recycler-adapter androidx android-viewholder

我正在尝试创建一个类似于Google Play商店的系列概述活动的界面。 为此,我使用的是RecyclerView方法(顶部是DetailsViewHolder,下面是EpisodesViewHolder)。

当前,UI可以正常工作,但是我正面临着这样的问题:DetailsViewHolder如果不在屏幕上,则会进行回收(默认行为)。但是我想防止回收并保持状态+避免图像加载。

我尝试过holder.setIsRecyclable(false),但是我没有运气,它仍然在呼叫onBindViewHolder。我正在使用此依存关系:androidx.recyclerview:recyclerview:1.1.0

setIsRecyclable函数本身可能存在问题,或者我做错了吗?

代码:

型号:

abstract class BaseModel {
    abstract fun getType(): Int

    companion object {
        const val DETAILS = 0
        const val EPISODE = 1
    }
}

class DetailsModel(val data: Data): BaseModel() {
    override fun getType(): Int {
        return DETAILS
    }
}

class EpisodeModel(val episode: Episode): BaseModel() {
    override fun getType(): Int {
        return EPISODE
    }
}

ViewHolders:

abstract class BaseViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) {
    abstract fun bind(itemModel: T)
}

class DetailsViewHolder(val parent: ViewGroup): BaseViewHolder<DetailsModel>(
    LayoutInflater.from(parent.context).inflate(R.layout.viewholder_details, parent, false)
), View.OnClickListener {

    init {
        itemView.setOnClickListener(this)
        setIsRecyclable(false)
    }

    override fun bind(itemModel: DetailsModel) {
        itemView.title.text = itemModel.data.title
    }

    override fun onClick(v: View?) {
        itemView.title.text = "CHANGED"
    }
}

class EpisodeViewHolder(val parent: ViewGroup): BaseViewHolder<EpisodeModel>(
    LayoutInflater.from(parent.context).inflate(R.layout.viewholder_episode, parent, false)
), View.OnClickListener {
    override fun bind(itemModel: EpisodeModel) {
        // some bindings
    }
}

适配器:

class ActivityListAdapter : ListAdapter<BaseModel, BaseViewHolder<*>>(DIFF) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
        return when (viewType) {
            BaseModel.DETAILS -> DetailsViewHolder(parent)
            BaseModel.EPISODE -> EpisodeViewHolder(parent)
            else -> throw IllegalStateException("Unsupported view type")
        }
    }

    override fun onBindViewHolder(holder: BaseViewHolder<*>, position: Int) {
        Log.d(TAG, "onBindViewHolder: pos = $position, type = ${holder.itemViewType}")

        when (holder) {
            is DetailsViewHolder -> {
                holder.bind(getItem(position) as DetailsModel)
            }
            is EpisodeViewHolder -> {
                holder.bind(getItem(position) as EpisodeModel)
            }
            else -> throw IllegalArgumentException("onBindViewHolder: holder problem")
        }

    }

    override fun getItemViewType(position: Int): Int {
        return getItem(position).getType() // DETAILS, EPISODE (BaseModel)
    }

    companion object {
        val TAG = ActivityrListAdapter::class.java.simpleName

        val DIFF = object : DiffUtil.ItemCallback<BaseModel>() {
            ...
        }
    }
}

感谢您的帮助;)

0 个答案:

没有答案