弹出窗口的Recyclerview中的Onclick侦听器

阿什什

我试图使弹出窗口,用户可以在其中看到Recyclerview(评论列表)。但是我想在其中添加2个按钮以进行投票和降级以进行评论。

我已经完成了Recyclerview的视图并根据需要正确显示了它,但是我无法在recyclerview中添加按钮。这是我的代码。

我的活动中的弹出窗口代码:

lateinit var  getAllcomment : GetAllCommentsAdapter
lateinit var upVote : View.OnClickListener
lateinit var downVote : View.OnClickListener

viewallcomments_txt.setOnClickListener {it->
            val layoutInflater = [email protected](Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val customView = layoutInflater.inflate(R.layout.popup, null)
            val display = windowManager.defaultDisplay
            val size = Point()
            display.getSize(size)
            val popupWindow=PopupWindow(customView, size.x-50, size.y-660, true);
            popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
            popupWindow.setAnimationStyle(R.style.PopupAnimation)
            popupWindow.showAtLocation(linearLayout1, Gravity.CENTER, 0, 0)
            popupWindow.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            customView.popup_rcv.layoutManager = LinearLayoutManager(this)
            getAllcomment = GetAllCommentsAdapter(ArrayList(), upVote, downVote) // I have declare Adapter all parameter
            getMainApp().swiftAPI.getAllComments(post_id).enqueue(object : Callback<ArrayList<Comments>>{
                override fun onFailure(call: Call<ArrayList<Comments>>, t: Throwable) {
                    Toast.makeText(this@ViewSinglePostActivity, t?.message, Toast.LENGTH_SHORT)
                }

                override fun onResponse(call: Call<ArrayList<Comments>>, response: Response<ArrayList<Comments>>) {
                    if (response?.isSuccessful!!){
                        getAllcomment.commentList.addAll(response.body()!!)
                        customView.popup_rcv.adapter = getAllcomment
                        getAllcomment.notifyDataSetChanged()
                    }
                }
            }) 

GetAllcomments适配器:

class GetAllCommentsAdapter (var commentList: ArrayList<Comments>, val upVote: View.OnClickListener, val downVote: View.OnClickListener) : RecyclerView.Adapter<GetAllCommentsAdapter.ViewHolder>() {
    var comment_id = 0
    var commentHashMap = HashMap<Int, Int>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.popup_rcv, parent, false)
        return ViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return commentList.count()
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.comment?.setText(commentList.get(position).comment)
        holder.username?.setText(commentList.get(position).username)
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var username : TextView ?= null
        var comment : TextView ?= null
        var upvote : ImageView ?= null
        var downvote : ImageView ?= null
        init {
            this.username = itemView.findViewById(R.id.dialog_cmt_user_txt)
            this.comment = itemView.findViewById(R.id.dialog_cmt_txt)
            this.upvote = itemView.findViewById(R.id.upvote_comment_img)
            this.downvote = itemView.findViewById(R.id.downvote_comment_img)
        }
    }

但是,当我打开弹出窗口时出现此错误。

lateinit属性upVote尚未初始化

提前致谢

Oziomajnr

这是实现过程中的错误,通常应var在编译时进行初始化,但是使用lateinit关键字向您保证,编译器将在应用程序运行时稍后对其进行初始化,并且在编译期间不应标记错误,但是您没有这样做。不要信守承诺,现在当您尝试使用从未初始化的变量时,您会遇到异常。

因此,解决办法是提供一个实现upVote,并downVote在使用之前点击听众。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章