0

我有一个 recyclerview 项目,它包含另一个 recyclerview 来显示图像列表:

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<android.support.v7.widget.RecyclerView
    android:id="@+id/imageRv"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

如果我在 ViewHolder 中使用分页,我必须像这样调用

class GankViewHolder(parent: View) : RecyclerView.ViewHolder(parent) {
private val mContext = parent.context
private val profileIv = itemView.findViewById<AvatarImageView>(R.id.profileIv)
private val titleTv = itemView.findViewById<TextView>(R.id.titleTv)
private val timeTv = itemView.findViewById<TextView>(R.id.timeTv)
private val description = itemView.findViewById<TextView>(R.id.contentTv)
private val imageRv = itemView.findViewById<RecyclerView>(R.id.imageRv)
fun bind(gankModel: GankModel?) {
    gankModel?.let {
        val random = Random()
        val r = random.nextInt(256)
        val g = random.nextInt(256)
        val b = random.nextInt(256)
        profileIv.setTextAndColor(gankModel.who, Color.rgb(r, g, b))
        titleTv.text = gankModel.desc
        timeTv.text = TimeHelper.getLiveTime(gankModel.publishedAt)
        description.text = gankModel.url
    }
    if (gankModel?.images != null && gankModel.images.size != 0) {
        var liveData: LiveData<PagedList<String>> = PagingHelper.getLocalLiveData(gankModel.images)
        imageRv.layoutManager = GridLayoutManager(mContext, 2)
        val adapter = ImagePagingAdapter(object : ImagePagingAdapter.OnItemTouchListener {
            override fun onItemClick(view: View, position: Int) {
            }
        })
        imageRv.adapter = adapter
        liveData.observeForever(android.arch.lifecycle.Observer {
            adapter.submitList(liveData.value)
            LogUtils.e("livedata observer size:" + liveData.hasActiveObservers() + "and:" + liveData.hasObservers())
        })

    }

}

}

但它在 ViewHolder 中没有 LifecycleOwner

4

0 回答 0