I have the issue with my RecyclerView item onClick event. 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:
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. Now this code does not works. Maybe the issue generate because I'm using spanSizeLookup in this case. 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. And one more I just dive into android development, and I use only Kotlin not Java.
CodePudding user response:
I'm Java Developer so I cannot code with Kotlin but I'll show you how can you change cardView
background on Onclick
.
You are directly using conditions without any itemView
.
just put your condition in 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. If you any problem is causing, Let me know
CodePudding user response:
You can simply use this for your click listner of recylerview in MainActivity
.
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.