android 为什么onLongclicklistener在带有Kotlin的onBindview中不起作用

epggiuax  于 2023-01-11  发布在  Android
关注(0)|答案(4)|浏览(143)

我已经寻找了这个问题的解决方案,但找不到答案。我可以从适配器的onBindViewHolder内部让我的onClickListener工作(Kotlin),但onLongClickListener(Kotlin)没有响应,即使代码没有显示错误

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

    val ingredientdisplay=displayItems[position]
    holder.setData(ingredientdisplay,position)
    runningTotal=runningTotal+holder.itemView.tvcost.text.toString().toDouble()
    println ("running total $runningTotal")

    val intent = Intent("message_from_displayadapter")
    intent.putExtra("runningtotal", runningTotal)
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent)   

    holder.itemView.setOnLongClickListener {view->
        println("longclick")
        true
    }

    holder.itemView.setOnClickListener {
        val intent = Intent(context, ChooseIngredientsActivity::class.java)
        ContextCompat.startActivity(context, intent, null)
    }
}

我只是尝试println或运行Toast,但什么也没发生。
我不明白为什么?你的帮助将不胜感激。

oxiaedzo

oxiaedzo1#

首先,不要将您的onClicklistiner放在onBindViewHolder中,这不是一个好的做法,请像这样更新您的适配器类,OnLongclicklistiner也工作正常

class NewsAdapter (val context: Context, private val arrayList: ArrayList 
<NewsModel>):
RecyclerView.Adapter <NewsAdapter.Holder> () {

companion object {
   // val TAG: String = OperationAdapter::class.java.simpleName
}

override fun onCreateViewHolder (parent: ViewGroup, viewType: Int): Holder {
    return Holder (LayoutInflater.from (parent.context ).inflate (R.layout.newsitemlist , parent, false))
}

override fun getItemCount (): Int = arrayList. size

override fun onBindViewHolder (holder: Holder, position: Int) {

    val mynews= arrayList[position]
    holder.setData(mynews, position)

}

inner class Holder (itemView: View): RecyclerView.ViewHolder (itemView) {
    private var currentnews: NewsModel? = null
    private var currentPosition: Int = 0

    init {
        //The click listener

        itemView.cardview.setOnClickListener {

            val i = Intent(context,NewsReaderActivity::class.java)
            i.putExtra("body",currentnews!!.body)
            i.putExtra("title",currentnews!!.title)
            context.startActivity(i)

        }

        itemView.cardview.setOnLongClickListener{
            Toast.makeText(context,"Long Clicked",Toast.LENGTH_SHORT).show()
            true
        }

        //the end of the init
    }

    //getting data from model and bind it into View
    fun setData(news: NewsModel?, position: Int) {
        news?.let {
            itemView.newstitle.text = news.title
            itemView.body.text = news.body
            itemView.txtdate.text = news.ndate

        }

        this.currentnews = news
        this.currentPosition = position
          }
       }

  }
kwvwclae

kwvwclae2#

我已经把监听器移到了内部类中,完整适配器的代码如下所示。但是onLongClicListener仍然没有响应。
类RecipiesDisplayAdapter(瓦尔为上下文:上下文,值为显示项:列表):循环视图.适配器(){

var runningTotal:Double=0.00

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

}

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

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

    val ingredientdisplay=displayItems[position]
    holder.setData(ingredientdisplay,position)
    runningTotal=runningTotal+holder.itemView.tvcost.text.toString().toDouble()
    println ("running total $runningTotal")

    val intent = Intent("message_from_displayadapter")
    intent.putExtra("runningtotal", runningTotal)
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent)

}

inner class MyViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){

 private var  currentIngredientDisplay:IngredientDisplay?=null
 private var  currentPosition:Int=0

    init {

        itemView.setOnLongClickListener {
            Log.i ("clicked ", "longclick")
            context.showToast("longClicked")
             true
        }

        itemView.setOnClickListener {

            val intent = Intent(context, ChooseIngredientsActivity::class.java)
            ContextCompat.startActivity(context, intent, null)

        }

    }

   fun setData(ingredientdisplay: IngredientDisplay?, pos:Int){

       val fromrate=getfromdb(ingredientdisplay!!.unitPurchased)
       val torate =getfromdb(ingredientdisplay!!.unitPerRecipe)

       val minunitcost= ingredientdisplay!!.costPurchased/ingredientdisplay!!.quantityPurchased/fromrate
       val costperrecipe=minunitcost*ingredientdisplay!!.quantityPerRecipe*torate
       val s = String.format( "%.2f", costperrecipe);

       val pdu=ingredientdisplay!!.quantityPerRecipe.toString()+" "+ingredientdisplay!!.unitPerRecipe

       ingredientdisplay?.let {

           itemView.tvIngredientName.text = ingredientdisplay!!.ingredientName
           itemView.tvcost.text = s
           itemView.tvqty.text = pdu
       }
       this.currentPosition=pos
       this.currentIngredientDisplay=ingredientdisplay

   }

    fun getfromdb (unit:String) :Double{

        var sendback:String=""

        context.database.use {
            select("Units", "convertionrate")
                .`whereSimple`("unitname = ?", unit)

                .exec {

                    parseList(DoubleParser).forEach{

                        println("result $it")

                        val resu= it.toString()

                        sendback=resu
                    }
                }

        }

        return sendback.toDouble()
    }

}

}

我不知道为什么!

xqkwcwgp

xqkwcwgp3#

问题解决了。我犯了一个新手错误。我把所有的onclicklistener添加到我的适配器中。然后后来又开始添加onlongclicklistener到它们中。信不信由你。我把它设置在了错误的适配器中。为你的麻烦道歉,但是你的答案很有帮助。

deyfvvtc

deyfvvtc4#

上下文有时不起作用或在较新版本中不起作用,因此您也可以使用
吐司.makeText(it.context,“长时间点击”,Toast.长度_短).show()

相关问题