简体   繁体   English

为什么 onClick 不适用于我的 RecyclerView 项目?

[英]Why the onClick does not work with my RecyclerView item?

I have the issue with my RecyclerView item onClick event.我的 RecyclerView 项目 onClick 事件有问题。 Here is my adapter class:这是我的适配器类:

class CategoryRecyclerAdapter( val listener: (position: Int) -> Unit):
RecyclerView.Adapter<CategoryRecyclerAdapter.ViewHolder>() {

private var catTitles = arrayOf(
    "lorem ipsum cat one",
    "lorem ipsum cat otwo",
    "lorem ipsum cat three",
    "lorem ipsum cat four",
    "lorem ipsum cat five",
    "lorem ipsum cat six"
)

private var catImages = arrayOf(
    R.drawable.ic_star,
    R.drawable.ic_star,
    R.drawable.ic_star,
    R.drawable.ic_star,
    R.drawable.ic_star,
    R.drawable.ic_star
)

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

override fun onBindViewHolder(holder: CategoryRecyclerAdapter.ViewHolder, position: Int) {
    holder.catTitle.text = catTitles[position]
    holder.catImage.setImageResource(catImages[position])
    when(position){
        0 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#FF5668"))
        }
        1 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#18C2E9"))
        }
        2 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#686DF6"))
        }
        3 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#FFA925"))
        }
        4 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#18C2E9"))
        }
        5 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#FF5668"))
        }
    }
}

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

inner class ViewHolder(itemView:View): RecyclerView.ViewHolder(itemView), View.OnClickListener{
    var catImage: ImageView = itemView.findViewById(R.id.catImage)
    var catTitle: TextView = itemView.findViewById(R.id.catTitle)
    var catCard: CardView = itemView.findViewById(R.id.catCardItem)

    override fun onClick(viewType: View?) {
        listener.invoke(adapterPosition)
    }
  }
}

and here is my mainActivity code:这是我的 mainActivity 代码:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val categoryList = findViewById<RecyclerView>(R.id.mainCategoryGrid)

    categoryList.layoutManager = GridLayoutManager(this, 2).also {
        it.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return if (position == 4 )
                    2
                else if (position == 5)
                    2
                else
                    1
            }
        }
    }

    categoryList.adapter = CategoryRecyclerAdapter { position ->

        val topActivity = Intent(this, TopActivity::class.java)
        val flowersAlphabetActivity = Intent(this, FlowersActivity::class.java)
        val readActivity = Intent(this, ReadActivity::class.java)
        val commonActivity = Intent(this, CommonActivity::class.java)
        val stepperActivity = Intent(this, StepperActivity::class.java)
        val phrasesActivity = Intent(this, PhrasesActivity::class.java)

        when (position) {
            0 -> startActivity(alphabetActivity)
            1 -> startActivity(flowersAlphabetActivity)
            2 -> startActivity(readActivity)
            3 -> startActivity(commonActivity)
            4 -> startActivity(stepperActivity)
            5 -> startActivity(phrasesActivity)
        }
     }

   }
 }

In the app before I used the same structure for implement the RecyclerView item onClick Listener and it work pretty well.在应用程序中,我使用相同的结构来实现 RecyclerView 项 onClick 侦听器,它工作得很好。 Now this code does not works.现在这段代码不起作用。 Maybe the issue generate because I'm using spanSizeLookup in this case.也许问题是因为我在这种情况下使用 spanSizeLookup 而产生的。 Please help to figure out why it does not work now.请帮助弄清楚为什么它现在不起作用。

PS: I know there are a lot info about RecyclerView Item Click Listener on Stackoverflow but I do not ask how to target this goal I just need to figure out why my code does not work. PS:我知道 Stackoverflow 上有很多关于 RecyclerView Item Click Listener 的信息,但我不问如何实现这个目标,我只需要弄清楚为什么我的代码不起作用。 And one more I just dive into android development, and I use only Kotlin not Java.还有一个我只是潜入 android 开发,我只使用 Kotlin 而不是 Java。

I'm Java Developer so I cannot code with Kotlin but I'll show you how can you change cardView background on Onclick .我是 Java 开发人员,所以我无法使用 Kotlin 进行编码,但我将向您展示如何在Onclick上更改cardView背景。

You are directly using conditions without any itemView .您直接使用没有任何itemView的条件。

just put your condition in holder.itemView.setOnClickListner .只需将您的条件放入holder.itemView.setOnClickListner See below code见下面的代码

holder.itemView.setOnClickListener(view -> {
Toast.makeText(context, "Item Clicked", Toast.LENGTH_SHORT).show();
   when(position){
        0 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#FF5668"))
        }
        1 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#18C2E9"))
        }
        2 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#686DF6"))
        }
        3 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#FFA925"))
        }
        4 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#18C2E9"))
        }
        5 -> {
            holder.catCard.setCardBackgroundColor(Color.parseColor("#FF5668"))
        }
    }

});

Make sure to change JAVA to Kotlin in my code.确保在我的代码中将JAVA更改为Kotlin If you any problem is causing, Let me know如果您有任何问题,请告诉我

Add Click listner in recylerview在 recyclerview 中添加 Click listner

private lateinit var mListener: OnItemClickListener

    interface OnItemClickListener : AdapterView.OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        mListener = listener
    }

And Then接着

You can simply use this for your click listner of recylerview in MainActivity .您可以简单地将它用于MainActivity中 recylerview 的点击列表

adapter.setOnItemClickListener(object : CategoryRecyclerAdapter.OnItemClickListener {
               
                override fun onClick(position: Int) {
                   when (position) {
                         0 -> startActivity(alphabetActivity)
                         1 -> startActivity(flowersAlphabetActivity)
                         2 -> startActivity(readActivity)
                         3 -> startActivity(commonActivity)
                         4 -> startActivity(stepperActivity)
                         5 -> startActivity(phrasesActivity)
                   }
                }
    
               
            })

(Need to change Function and variable name) (需要更改函数和变量名)

Hope it will work for you.希望它对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM