0
Every thing works fine if i m not scrolling , if i scroll i get null pointer exception. I tried using getchildAt(position) and findViewHolderForAdapterPosition(position) basically i m getting the view at this position to expand the view, after scrolling on click of that view it throws null pointer exception.

Basically getChildAt and findViewHolderForAdapterPosition is null after scroll in android

 My requirement is on click of any view the view should be expanded and on click of same view view should be close and same if one view is clicked and if clicking on other view the view which was already expanded/open should be close and open the clicked view.

How to achieve all case in recycler view?

I have pojo and pojo contains one of the variable as isread boolean (true/false)

Any help is appreciated!

Below is the code:

holder.card_view_layer.setOnClickListener {
                if (position != expandedPos && expandedPos != -1) {
                    notificationPojoList[expandedPos].isRead = true
                    var expandedView  :View = SavedNotificationActivityV2.rv_notificationList!!.getChildAt(expandedPos)
                    var expandedMsg:TextView = expandedView.findViewById(R.id.tv_message)
                    var expandedIsRead:View = expandedView.findViewById(R.id.view_isRead)
                    expandedIsRead.setBackgroundColor(context.resources.getColor(R.color.surya_read_notif_grey))
                    expandedMsg.visibility = View.GONE
                    expandedIsRead.visibility = View.VISIBLE

                    holder.message.visibility = View.VISIBLE
                    holder.view_isRead.visibility = View.INVISIBLE
                    expandedPos = position
                } else {
                    if (holder.message.visibility == View.VISIBLE) {
                        expandedPos = -1
                        holder.message.visibility = View.GONE
                        holder.view_isRead.visibility = View.VISIBLE
                        holder.view_isRead.setBackgroundColor(context.resources.getColor(R.color.colorAccent))
                        notificationPojoList[position].isRead = true
                        (context as SavedNotificationActivityV2).updategui()
                    } else {
                        holder.message.visibility = View.VISIBLE
                        holder.view_isRead.visibility = View.INVISIBLE
                        notificationPojoList[position].isRead = true
                        expandedPos = position
                    }
                }
            }

Replaced this line with findViewHolderForAdapterPosition also still i m getting null pointer exception when i scroll and click last view in recycler view var expandedView :View = SavedNotificationActivityV2.rv_notificationList!!.getChildAt(expandedPos)

Any help is appreciated!
Lakshmi n
  • 29
  • 3

1 Answers1

1
    I myself found solution to above question.Please find the code below:
if(position == lastPos){
                    holder.message.visibility = View.VISIBLE
                }else{
                    holder.message.visibility = View.GONE
                }
                holder.itemView.setOnClickListener(){
                    if(lastPos == position){
                        holder.message.visibility = View.GONE
                        lastPos = -1
                    }else {
                        lastPos = position;
                        notifyDataSetChanged()
                    }

                }
Lakshmi n
  • 29
  • 3