RecycleView列表项没有出现

时间:2016-12-24 18:03:03

标签: android android-recyclerview kotlin anko

我一直在尝试让RecyclerView在我的应用中工作,其中列表项为ImageView s,图像下载并放入onBindViewHolder方法内(异步) 。我的代码中没有任何错误,但出于某种原因

  

仅对用户可见(甚至部分)的列表项   当活动加载时,将图像加载到其中。

虽然我看不到图像,但我观察到这些项目的高度和宽度已正确分配。因为首先下载图像,然后确定ImageView的尺寸,我认为问题与RecyclerView本身有关?如果有人能够对此有所了解,那就太棒了。感谢。

我还想补充一点,如果活动暂停然后恢复(通过点击“方形”导航按钮然后恢复它),所有列表项的图像都会正确加载。

Pic#1 as you can see the gray portion where an image is supposed to be 图#2 somewhat more convincing :)

这是我的代码:

onCreate方法:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        recyclerView {
            id = ViewID.ID_LIST
        }

        val imgList = ArrayList<ImageView>()
        imgList.add(ImageView(ctx))
        imgList.add(ImageView(ctx))
        imgList.add(ImageView(ctx))
        imgList.add(ImageView(ctx))
        val lv = findViewById(ViewID.ID_LIST) as RecyclerView
        lv.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        lv.adapter = ImageRecyclerAdapter(ctx, imgList)

}

RecyclerView.Adapter类:

private class ImageRecyclerAdapter(val context: Context, val imageList: ArrayList<ImageView>) :
            RecyclerView.Adapter<RecyclerView.ViewHolder>() {

        override fun onViewRecycled(holder: RecyclerView.ViewHolder?) {
            super.onViewRecycled(holder)
            if (holder != null) {
                val v = holder.itemView as ImageView
                v.setImageBitmap(null)
            }
        }

        override fun onBindViewHolder(p: RecyclerView.ViewHolder, position: Int) {
            val v = p.itemView as ImageView
            Ion.with(v)
                    .load("https://pbs.twimg.com/profile_images/616076655547682816/6gMRtQyY.jpg")
                    .setCallback({ exception, t ->
                        if (t != null) {
                            val dm = Point()
                            context.windowManager.defaultDisplay.getRealSize(dm)
                            val w = t.maxWidth
                            val h = t.maxHeight

                            val params = t.layoutParams
                            if (params != null) {
                                params.width = dm.x
                                params.height = (dm.x * (h.toDouble() / w.toDouble())).toInt()
                                t.layoutParams = params
                                t.requestLayout()
                            }
                        }
                    })
        }

        override fun getItemCount(): Int {
            return imageList.size
        }

        override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
            val v = ImageView(context)
            return object : RecyclerView.ViewHolder(v) {}
        }
    }

1 个答案:

答案 0 :(得分:0)

在我将数据绑定到同步请求之后,我将ImageView操作(更改为LayoutParams)移到onViewAttachedToWindow重写中了我的适配器的方法。

onViewAttachedToWindow

override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder?) {
    super.onViewAttachedToWindow(holder)
    val t = holder?.itemView as ImageView
    val dm = Point()
    context.windowManager.defaultDisplay.getRealSize(dm)
    val w = t.maxWidth
    val h = t.maxHeight
    val params = t.layoutParams
    if (params != null) {
        params.width = dm.x
        params.height = (dm.x * (h.toDouble() / w.toDouble())).toInt()
        t.layoutParams = params
        t.requestLayout()
    }
}

onBindViewHolder

override fun onBindViewHolder(p: RecyclerView.ViewHolder, position: Int) {
    val v = p.itemView as ImageView
    Ion.with(v)
            .load(imageList[position].toString())
            .tryGet()
}