androidkotlinadmobnative-ads

can't show admob native advanced ad inside RecyclerView


I want to show an advanced native ad inside a RecyclerView

this is the code I have tried inside the adabter

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ITEM_SONG -> {
                val view = LayoutInflater.from(parent.context)
                        .inflate(R.layout.song_card, parent, false)
                SongViewHolder(view)
            }
            else -> {
                val bannerView = LayoutInflater.from(parent.context)
                        .inflate(R.layout.ad_frame, parent)
                AdViewHolder(bannerView)
            }
        }
    }

    private fun populateUnifiedNativeAdView(nativeAd: UnifiedNativeAd, adView: UnifiedNativeAdView) {
        adView.mediaView = adView.findViewById(R.id.ad_media)

        adView.headlineView = adView.findViewById(R.id.ad_headline)
        adView.bodyView = adView.findViewById(R.id.ad_body)
        adView.callToActionView = adView.findViewById(R.id.ad_call_to_action)

        (adView.headlineView as TextView).text = nativeAd.headline
        adView.mediaView.setMediaContent(nativeAd.mediaContent)

        if (adView.bodyView == null) {
            adView.bodyView.visibility = View.INVISIBLE
        } else {
            adView.bodyView.visibility = View.VISIBLE
            (adView.bodyView as TextView).text = nativeAd.body
        }

        if (adView.callToActionView == null) {
            adView.callToActionView.visibility = View.INVISIBLE
        } else {
            adView.callToActionView.visibility = View.VISIBLE
            (adView.callToActionView as TextView).text = nativeAd.callToAction
        }
        adView.setNativeAd(nativeAd)
    }

inside the onBindViewHolder

ITEM_BANNER -> {
                val adViewHolder = holder as AdViewHolder
                val parent = adViewHolder.itemView as ViewGroup
                val adView = LayoutInflater.from(App.context)
                        .inflate(R.layout.ad_unified, null) as UnifiedNativeAdView
                val builder = AdLoader.Builder(App.context, NATIVE_ID)
                        .forUnifiedNativeAd { unifiedNativeAd: UnifiedNativeAd? ->
                            if (unifiedNativeAd != null) {
                                populateUnifiedNativeAdView(unifiedNativeAd, adView)
                                parent.removeAllViews()
                                parent.addView(adView)
                            }
                        }

                val videoOptions = VideoOptions.Builder()
                        .setStartMuted(true)
                        .build()

                val adOptions = NativeAdOptions.Builder()
                        .setVideoOptions(videoOptions)
                        .build()

                builder.withNativeAdOptions(adOptions)

                builder.build().loadAd(AdRequest.Builder().build())
        }
    }
}

in MainActivity this code is used to add the FrameLayout to the list of items

private fun getNativeAds(list: ArrayList<Any>) {
        var i = 0
        while (i < list.size) {
            list.add(i, FrameLayout(this@MainActivity))
            i += ITEMS_PER_AD
        }
    }

I get this error

java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to c.c.a.b.a
        at c.c.a.a.a.b(:82)
        at androidx.recyclerview.widget.RecyclerView$g.c(:7065)
        at androidx.recyclerview.widget.RecyclerView$g.a(:7107)
        at androidx.recyclerview.widget.RecyclerView$v.a(:6012)
        at androidx.recyclerview.widget.RecyclerView$v.a(:6279)
        at androidx.recyclerview.widget.RecyclerView$v.b(:6118)
        at androidx.recyclerview.widget.RecyclerView$v.d(:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$c.a(:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.a(:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.a(:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.c(:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(:4404)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(:1915)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(:446)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:755)
        at android.view.View.layout(View.java:17564)
        at android.view.ViewGroup.layout(ViewGroup.java:5656)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2422)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2142)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1299)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
        at android.view.Choreographer.doCallbacks(Choreographer.java:683)
        at android.view.Choreographer.doFrame(Choreographer.java:619)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6316)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

Solution

  • Please check your method:

    getItemViewType(position: Int): Int 
    

    It might not returning the correct itemView type