0

This is probably an easy fix, but what do you initalize the ViewHolder class so that i wouldn't have to call it when the function is called in another class.

Here's what I mean

fun registerFragmentState(holder: BaseBindingViewHolder) = object: OnStateChange {// BaseBindingViewHolder = ???
        override fun onResumed() {
            holder.itemView.cCount1.visibility = View.GONE
        }

    } 

I need to initialize BaseBindingViewHolder so that I don't have to fill it in on the file I am calling it on. Thanks again.

For clarification. I am using the ViewHolder to access the views in the Recycler View, as a parameter to registerFragmentStatesince there is no initial value, when calling the function, it going to ask for a value (or this case a view of some sort to fill in).

enter image description here

I'm looking to avoid or solve registerFragmentState() error

OEThe11
  • 341
  • 2
  • 11

1 Answers1

4

I think you're going about this the wrong way, hence why you're in this awkward position. Your view holders should have no knowledge of the current fragment state. This would get you into trouble as your recycler view is swapping items in and out - you'd have no guarantee that the view who's visibility you're trying to update is actually valid.

When working with RecyclerView, to update the state of individual item views, you should be updating the underlying domain objects that represent the view state and notifying the adapter that those items have changed. Then the adapter will refresh, pulling the latest state of those domain objects to update the state of the view.

See this question and see if it helps: How to update RecyclerView Adapter Data

dominicoder
  • 9,338
  • 1
  • 26
  • 32
  • That sounds great, but How would I go about doing it? I've been trying, but nothing seems to be working. Can you give an example? – OEThe11 Mar 14 '23 at 13:16
  • I linked you a SO question already. There's also the documentation: https://developer.android.com/develop/ui/views/layout/recyclerview. You can also Google for thousands of examples of how to use RecyclerView. – dominicoder Mar 14 '23 at 14:54