简体   繁体   中英

onClick on RecyclerView.ViewHolder not working

I am following the Shrine example application appeared on Google official Codelabs ( here ).

I want to create a simple view (Fragment like other Fragments) to show detail text of any item when clicked.

I think the best way to implement onClick on elements is in the StaggeredProductCardViewHolder class as I see it implements RecyclerView.ViewHolder(itemView) .

This is what I tried already but I cannot even catch a click on any item anywhere on screen, so what is going wrong ?

class StaggeredProductCardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

    var productImage: NetworkImageView = itemView.findViewById(R.id.product_image)
    var productTitle: TextView = itemView.findViewById(R.id.product_title)
    var productPrice: TextView = itemView.findViewById(R.id.product_price)


    override fun onClick(v: View?) {
        println("wow clicked")
        v?.context?.startActivity(Intent(v.context, ProductCardDetailActivity::class.java))
    }

    class ProductCardDetailActivity : Fragment(){

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            println("onCreate")
        }
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            println("onCreateView")
            return super.onCreateView(inflater, container, savedInstanceState)
        }
    }
}

Note: I added a Fragment ProductCardDetailActivity where I will be implementing other methods to show item Fragment description when clicked.

I really don't know from where to start and where to head, otherwise I would show more effort on this but I keep trying meanwhile.

You are just implementing View.OnClickListener interface. It does nothing with handling click. You should attach this for view holder's view

class StaggeredProductCardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

var productImage: NetworkImageView = itemView.findViewById(R.id.product_image)
var productTitle: TextView = itemView.findViewById(R.id.product_title)
var productPrice: TextView = itemView.findViewById(R.id.product_price)
init{
   itemView.setOnClickListener(this)
}


override fun onClick(v: View?) {
    println("wow clicked")
    v?.context?.startActivity(Intent(v.context, ProductCardDetailActivity::class.java))
}

class ProductCardDetailActivity : Fragment(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        println("onCreate")
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        println("onCreateView")
        return super.onCreateView(inflater, container, savedInstanceState)
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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