android – Interface between recyclerview adapter & viewholder-ThrowExceptions

Exception or error:

So I have a Recyclerview & multiple viewholders. Now one of my viewholder contains an item that has few animations. Now if a user clicks on the item activity opens and I have no control over the activity, the only way I got to know is via the callback in my fragment i.e onPause & onResume. I want to call animation.onResume() once my fragment onResume() is invoked.

Interface class

interface CustomTabClosedListener {
        fun onCustomTabClosed()

Adapter Relevant code:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
   lateinit var viewViewHolder: RecyclerView.ViewHolder
   val layoutInflater = LayoutInflater.from(context)
   val view = layoutInflater!!.inflate(R.layout.feed_coverage, parent, false)
                viewViewHolder = CoverageViewHolder(view)
                listener = viewViewHolder

where listener is private var listener: CustomTabClosedListener?=null


class CoverageViewHolder(val view: View) :CustomTabClosedListener {

override fun onCustomTabClosed() {

Now my fragment calls this adapter method in onResume –

fun customTabClosed() {

But here my listener is getting null and I don’t know the reason why. How can i invoke my method in viewholder via my adapter/fragment? Is there any other better approach?

How to solve:

I guess the reason why you get null is because you should have only a single call back in the Adapter viewHolder, and all the animation should be managed by the view. Is better and clean if the adapter manage some really elementar business logic, leaving what should happen to the view into the view itself.

Let’s say you have an xml file with your animation
you could have from the view the control to notify the adapter and start the animation

fun IamACoolCallBack(){

 AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_fall_down);

I found this tutorial it looks pretty neat to me

Leave a Reply

Your email address will not be published. Required fields are marked *