firebase / FirebaseUI-Android

Optimized UI components for Firebase
https://firebaseopensource.com/projects/firebase/firebaseui-android/
Apache License 2.0
4.64k stars 1.83k forks source link

'getItem' is final and cannot be overriden #2164

Open Cosmopal opened 4 months ago

Cosmopal commented 4 months ago

Welcome to FirebaseUI and thanks for submitting an issue!

Please take a look at open issues, as well as resolved issues, to see if your issue is either already being addressed, or has been solved by someone else.

If not, please feel free to fill in the following info so we can help faster!

Step 1: Are you in the right place?

Step 2: Describe your environment

Step 3: Describe the problem:

I am unable to override getItem function when extending the FirestorePagingAdapter. The build fails saying 'getItem' is final and cannot be overriden. I need to override getItem because I am trying to insert a header item in the list.

Steps to reproduce:




Observed Results:

Build fails: image

Expected Results:

Expected to be able to override this function as it is marked protected in PagingDataAdapter class

Relevant Code:

class VideoFirebaseAdapter(var context: Context,
                                    private var showCategory: Boolean = true,
                                    options: FirestorePagingOptions<VideoMetadata>,
                                    private val hasHeader:Boolean = false)
    :FirestorePagingAdapter<VideoMetadata, RecyclerView.ViewHolder>(options) {

    private val TYPE_HEADER = 0
    private val TYPE_ITEM = 1

    val TAG = "VideoAdapter"

    var config = ImageLoaderConfiguration.Builder(this.context)
            .build()!!

    private val loader: ImageLoader = ImageLoader.getInstance()

    init {
        loader.init(config)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (hasHeader && viewType == TYPE_HEADER) {
            getHeaderVH(parent, viewType)!!
        } else {
            val itemView = LayoutInflater.from(context).inflate(R.layout.video_item, parent, false)
            VideoListItemVH(itemView, showCategory, context, loader)
        }
    }

    abstract fun getHeaderVH(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder?

    override fun getItemViewType(position: Int): Int {
        return TYPE_ITEM
        return if (hasHeader && position == 0)
            TYPE_HEADER
        else
            TYPE_ITEM
    }

    override fun getItemCount(): Int {
        return if (hasHeader)
            super.getItemCount() + 1
        else
            super.getItemCount()
    }

  //  fails to override saying getItem is final
  override fun getItem(position: Int): DocumentSnapshot? {
        if(hasHeader) {
            if(position==0) return super.getItem(0) // This will be ignored!
            return super.getItem(position-1)
        }
        return super.getItem(position)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, video: VideoMetadata) {
        if (hasHeader && holder !is VideoListItemVH) {
            return
        } else {
            (holder as VideoListItemVH).bind(video)
        }
    }
}