为什么onLongclicklistener在Kotlin的onBindviewholder中不起作用

时间:2019-06-28 09:42:08

标签: android kotlin recycler-adapter onlongclicklistener

我一直在寻找解决此问题的方法,但找不到答案。 我可以从适配器的onClickListener内部使onBindViewHolder正常工作(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,但没有任何反应。

我不明白为什么?您的帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

首先不要将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
          }
       }

  }

答案 1 :(得分:0)

我已将侦听器移至内部类,完整的适配器的代码如下。仍然onLongClicListener没有响应。

RecipiesDisplayAdapter类(val context:Context,val displayItems:List):RecyclerView.Adapter(){

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()
    }

}

}

我不知道为什么!

答案 2 :(得分:0)

问题已解决。我犯了一个菜鸟错误。我将所有onclicklistener添加到我的适配器中。然后后来开始向他们添加onlongclicklistener。信不信由你。我在错误的适配器中设置了它。为您的麻烦准备的应用程序,但是您的答案很有帮助。