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

时间:2019-03-04 09:55:00

标签: android kotlin android-recyclerview

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

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

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

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

viewallcomments_txt.setOnClickListener {it->
            val layoutInflater = this@ViewSinglePostActivity.getSystemService(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尚未初始化

预先感谢

1 个答案:

答案 0 :(得分:1)

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

因此解决方案将是在使用upVotedownVote的Click侦听器之前为其提供实现。