【问题标题】:android fragment in a listview列表视图中的android片段
【发布时间】:2018-03-22 21:52:58
【问题描述】:

我有一个包含片段的自定义布局的 listView。我遇到的问题是,当我将片段添加到 listView 行时,我得到了一个永无止境的循环,该循环不断地遍历 getView 代码。

我只做了几个月的 Android 编码,所以任何帮助都会很棒。如果我需要发布更多代码,请告诉我

这是我的适配器代码:

class AdapterReply(
    private val context: Context,
    private val ph: Phan,
    private val replies: ArrayList<Reply> ) : BaseAdapter() {

override fun getCount(): Int {
    return replies.size
}
override fun getItem(position: Int): Reply {
    return replies[position]
}
override fun getItemId(position: Int): Long {
    return position.toLong()
}
override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View? {
    val rowMain: View?
    val rowHolder: ListRowHolder
    val contacts = ph.contacts
    val reply = replies[position]
    Log.d("AdapterReply", "Binding reply: Position $position ${reply.id} Detail: ${reply.detail}")
    if (convertView == null) {
        val layoutInflater = LayoutInflater.from(viewGroup!!.context)
        rowMain = layoutInflater.inflate(R.layout.reply_item_row, viewGroup, false)
        rowHolder = ListRowHolder(rowMain)
        rowMain.tag = rowHolder
        Log.d("AdapterReply", "New Item")
    } else {
        rowMain = convertView
        rowHolder = rowMain.tag as ListRowHolder
        Log.d("AdapterReply", "Old item from memory")
    }
    rowHolder.itemDetail.text = Helpers.anonymizeContent(reply.detail, ph.anonymousMetadata, ph.isViewerMember())
    rowHolder.itemLastActive.text = Helpers.getFormattedDate(reply.lastActive())


    if(reply.attachments.size > 0){
        val imageAttachment = reply.attachments.filter { attachment: Attachment -> attachment.type == 0 }[0]
        var imageUrl = Constants.BASE_URL + imageAttachment.url
        if(imageAttachment.anonymous()){
            imageUrl = Constants.BASE_URL + imageAttachment.anonUrl
        }
        Picasso.with(rowMain!!.context).load(imageUrl).into(rowHolder.itemImage)
    }
    var poster: Contact? = reply.contact
    if(contacts.size > 0) {
        val posterList = contacts.filter { contact: Contact -> contact.id == reply.rlContactID }
        if (posterList.isNotEmpty()) {
            poster = posterList[0]
        }
    }
    if(poster != null) {
        if(poster.anonymizedName.isNotEmpty()) {
            rowHolder.itemPoster.text = poster.anonymizedName
        } else {
            val posterName = "${poster.firstName} ${poster.lastName}"
            rowHolder.itemPoster.text = posterName
        }
        //Code the causes the problem
        val manager = (rowMain!!.context as AppCompatActivity).supportFragmentManager
        val posterAvatarFragment =
                AvatarFragment.newInstance(poster)
        manager.beginTransaction()
                .add(R.id.reply_avatar_fragment, posterAvatarFragment, "ReplyAvatarFragment")
                .commit()
       //End Code the causes the problem
    }
    bindReplies(rowMain, ph, reply.replies)
    return rowMain

}
internal class ListRowHolder(row: View?) {
    var itemDetail : TextView = row!!.reply_view_detail
    var itemImage : ImageView = row!!.reply_view_image
    var itemPoster : TextView = row!!.reply_view_posterName
    var itemLastActive : TextView= row!!.reply_view_lastActive
    var itemReplyReplies: ListView = row!!.reply_view_replyList
}
private fun bindReplies(viewRow: View?, ph: Phan, replies : ArrayList<Reply>){
    if(replies.isNotEmpty()) {
        val myObject = AdapterReply(context, ph, replies)
        val replyListView = viewRow!!.reply_view_replyList
        Log.d("AdapterReply", "Binding Replies: ${ph.encodedId} ${replies.size}")
        replyListView.adapter = myObject
    }
}

}

【问题讨论】:

  • 绝对有必要在列表视图中使用片段吗?我认为它增加了不必要的复杂性,并且膨胀等效布局会更有效。

标签: android listview android-fragments kotlin


【解决方案1】:
manager.beginTransaction()
            .add(R.id.reply_avatar_fragment, posterAvatarFragment, "ReplyAvatarFragment")
            .commit()

伙计,我不确定你是否知道,listview 中执行适配器类的函数是什么。适配器类用于通过类构造函数内的数组中传递的行来填充列表视图。 getView 方法会为数组中的每一行调用,例如,如果您有一个包含 10 行的数组,则此代码将触发 10 次。如果您确实将片段自动更改为另一个,并且在填充旧视图期间您将布局更改为具有相同数据的另一个布局,您的代码将形成无限循环,因为您将重复所有时间相同的情况。你应该避免在加载他的过程中动态改变布局的动作。在我的建议中,如果你想在一个适配器内使用多个布局,有两种特殊的方法来设置行的布局,基于一些条件:getItemViewTypegetViewTypeCount。在第一个中,您可以使用您的条件来检查应该为行使用哪种布局。第二个是设置布局的数量,这将在您的适配器类中使用。让我们搜索一些用法示例。

【讨论】:

  • 谢谢,我确实需要把片段放在那里。这个应用程序就像一个包含帖子和回复的论坛,片段包含显示作者图像/徽章的逻辑(它在应用程序中到处使用)。我将研究其他解决方案,然后是 listView。也许让它成为一个 recyleView?
猜你喜欢
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 2021-09-27
相关资源
最近更新 更多