RecyclerView setOnLongClickListener Kotlin

时间:2019-03-17 07:41:20

标签: android kotlin android-recyclerview recycler-adapter

首先: 我想将数据从RecyclerAdapter传递到Fragment,因此我在下面实现了此适配器:

class RecyclerItemCart(private val cart: MutableList<Post>, val context: Context, val listener: (Post) -> Unit) : RecyclerView.Adapter<RecyclerItemCart.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.cart_item, parent, false)
        return ViewHolder(view)
    }

    interface OnItemClickListener {
        fun onItemClickListener(post: Post)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(cart[position], listener)
        holder.mView.setOnLongClickListener {
            true }
    }

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

    inner class ViewHolder (val mView: View) : RecyclerView.ViewHolder(mView) {
        fun bind(mItem: Post, listener: (Post) -> Unit) = with(itemView)
        {
            val title: TextView = mView.findViewById(R.id.title)
            val authorName: TextView = mView.findViewById<TextView>(R.id.owner)
            val priceIT: TextView = mView.findViewById(R.id.priceIncT)
            val priceExcT: TextView = mView.findViewById(R.id.priceExcT)
            val pic: ImageView = mView.findViewById(R.id.pic)
            title.text = mItem.title
            authorName.text = mItem.authorName
            priceIT.text = mItem.priceIT
            priceExcT.text = mItem.priceExcT
            //        Glide.with(context).load(mItem.url).into(pic)
            setOnLongClickListener { listener(mItem)
                true }
        }
    }
}

然后在片段(版本1)中设置onLongClickListener:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val v = inflater.inflate(R.layout.cart_fragment, container, false)
        activity = getActivity() as MainActivity
        recyclerView = v.findViewById(R.id.recylerItemCart)

        totalAmountView = v.findViewById(R.id.total_amount)
        activity.toolbarTitle.text = resources.getString(R.string.cart)
        RequestGetCart.MakeRequestTask(this, activity).execute(activity.user.token)
        v.findViewById<Button>(R.id.checkout).setOnClickListener(this)
        recyclerView.setOnLongClickListener {
            recyclerView.adapter = RecyclerItemCart(cart.elements, activity){
                RequestRemoveItemCart.MakeRequestTask(this, activity).execute(activity.user.token, it.id, "1")
            }
           AlertDialog.Builder(activity)
                .setTitle("REMOVE ITEM")
                .setMessage(context!!.resources.getString(R.string.removeItemCart))
                .setPositiveButton(android.R.string.yes, DialogInterface.OnClickListener { dialog, which ->
                    recyclerView.adapter = RecyclerItemCart(cart.elements, activity){
                        RequestRemoveItemCart.MakeRequestTask(this, activity).execute(activity.user.token, it.id, "1")
                    }
                })
                .setNegativeButton(android.R.string.no, null)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show()
            true}
        return v
    }

但是当我尝试longClick时,什么也没发生。它永远不会进入侦听器。

所以我尝试将其修改为:

recyclerView.setOnLongClickListener(View.OnLongClickListener {
            true
             }) {
            recyclerView.adapter = RecyclerItemCart(cart.elements, activity){
                RequestRemoveItemCart.MakeRequestTask(this, activity).execute(activity.user.token, it.id, "1")
            }
           AlertDialog.Builder(activity)
                .setTitle("REMOVE ITEM")
                .setMessage(context!!.resources.getString(R.string.removeItemCart))
                .setPositiveButton(android.R.string.yes, DialogInterface.OnClickListener { dialog, which ->
                    recyclerView.adapter = RecyclerItemCart(cart.elements, activity){
                        RequestRemoveItemCart.MakeRequestTask(this, activity).execute(activity.user.token, it.id, "1")
                    }
                })
                .setNegativeButton(android.R.string.no, null)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show()
            true}

但是我使用

时出错
recyclerView.setOnLongClickListener(View.OnLongClickListener {
            true
             })

这是说我没有传递好的参数,但是我按要求传递了View.onLongClickListener。 如果您对此setOnLongClickListener有任何想法,或者对处理longClick并将数据从Recycler传递到片段有其他了解。 只有一种情况:我想在片段中做一些事情,而不是在回收站中做。

例如,我可以从Adapter捕获onLongClick并调用回调以返回片段,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

除了冗余Proc sort data=companies; by date Industry_category; run; Data companies(drop= company_names); set companies; by date Industry_category; If first.Industry_category then count=1; else count+1; if last.Industry_category; run; 之外,我认为适配器代码还可以。由于您在适配器构造函数中传递了侦听器,因此您的回调方法将是初始化适配器的lambda。

OnItemClickListener