弹出窗口Recyclerview中的Onclick监听器

问题描述 投票:2回答:1

我正在尝试制作弹出窗口,用户可以在其中看到Recyclerview(评论列表)。但是我想在其中添加2个按钮,用于upvote和downvote用于评论。

我已经完成了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尚未初始化

提前致谢

android kotlin android-recyclerview
1个回答
1
投票

这是你的实现的一个错误,通常你应该在编译时初始化一个var,但是使用lateinit关键字你承诺编译器你将在以后运行应用程序时初始化它并且它不应该在编译期间标记错误,但是你没有遵守这个承诺,现在当你尝试使用你从未初始化的变量时,你会得到一个例外。

所以解决方案是在使用之前为upVotedownVote点击监听器提供实现。

© www.soinside.com 2019 - 2024. All rights reserved.