Commit 064cf704 authored by guest's avatar guest

修改banner问题

parent af98239b
...@@ -48,7 +48,7 @@ class NewMainActivity : BaseActivity<ActivityMainBinding>() { ...@@ -48,7 +48,7 @@ class NewMainActivity : BaseActivity<ActivityMainBinding>() {
// ConfigHelper.enterHomeTimes++ // ConfigHelper.enterHomeTimes++
BarUtils.setStatusBarLightMode(this, true) BarUtils.setStatusBarLightMode(this, true)
BarUtils.setStatusBarColor(this, Color.TRANSPARENT) BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
AdmobBannerUtils.showCollapsibleBannerAd(this,binding.banner) AdmobBannerUtils.showCollapsibleBannerAd(this,binding.banner,binding.parentlayout)
// binding.root.updatePadding(top = BarUtils.getStatusBarHeight()) // binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
EventUtils.event("page_home") EventUtils.event("page_home")
if (!hasNotifications()) { if (!hasNotifications()) {
......
...@@ -2,15 +2,19 @@ package com.test.easy.easycleanerjunk.helps.ads ...@@ -2,15 +2,19 @@ package com.test.easy.easycleanerjunk.helps.ads
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.ViewTreeObserver import android.view.ViewTreeObserver
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.core.view.children
import com.google.ads.mediation.admob.AdMobAdapter import com.google.ads.mediation.admob.AdMobAdapter
import com.google.android.gms.ads.AdListener import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView import com.google.android.gms.ads.AdView
import com.test.easy.easycleanerjunk.BuildConfig
import com.test.easy.easycleanerjunk.helps.ConfigHelper import com.test.easy.easycleanerjunk.helps.ConfigHelper
import com.test.easy.easycleanerjunk.utils.SPUtils
import java.util.UUID import java.util.UUID
object AdmobBannerUtils { object AdmobBannerUtils {
...@@ -18,26 +22,38 @@ object AdmobBannerUtils { ...@@ -18,26 +22,38 @@ object AdmobBannerUtils {
private var adView: AdView? = null private var adView: AdView? = null
private var listener: ViewTreeObserver.OnGlobalLayoutListener? = null private var listener: ViewTreeObserver.OnGlobalLayoutListener? = null
fun showCollapsibleBannerAd(context: Context, parent: ViewGroup) { fun showCollapsibleBannerAd(context: Context, view: ViewGroup,parent: LinearLayout) {
val isShowBanner = SPUtils.getInstance().getString("isShowBanner", "0").toInt()
if (isShowBanner == 0) {
return
}
if (adView != null) { if (adView != null) {
adView?.destroy() adView?.destroy()
} }
adView = AdView(context) adView = AdView(context)
parent.removeAllViews() adView?.tag = "CollapsibleBannerAd"
parent.addView(adView) // parent.removeAllViews()
val list = view.children
list.forEach {
if (it.tag != "zhanweitu") {
view.removeView(it)
}
}
view.addView(adView)
listener = ViewTreeObserver.OnGlobalLayoutListener { listener = ViewTreeObserver.OnGlobalLayoutListener {
val screenPixelDensity = context.resources.displayMetrics.density val screenPixelDensity = context.resources.displayMetrics.density
val adWidth = (parent.width / screenPixelDensity).toInt() val adWidth = (view.width / screenPixelDensity).toInt()
val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, adWidth) val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, adWidth)
adView?.adUnitId = if (BuildConfig.DEBUG) ConfigHelper.bannerAdmobIdTest else ConfigHelper.bannerAdmobId adView?.adUnitId = if (BuildConfig.DEBUG) ConfigHelper.bannerAdmobIdTest else ConfigHelper.bannerAdmobId
adView?.setAdSize(adSize) adView?.setAdSize(adSize)
loadCollapsibleBanner() loadCollapsibleBanner(view,context,parent,adSize.height)
parent.viewTreeObserver.removeOnGlobalLayoutListener(listener) view.viewTreeObserver.removeOnGlobalLayoutListener(listener)
} }
parent.viewTreeObserver.addOnGlobalLayoutListener(listener) view.viewTreeObserver.addOnGlobalLayoutListener(listener)
} }
private fun loadCollapsibleBanner() { private fun loadCollapsibleBanner(parent: ViewGroup, context: Context, parentView: LinearLayout, height:Int) {
val extras = Bundle() val extras = Bundle()
extras.putString("collapsible", "bottom") extras.putString("collapsible", "bottom")
extras.putString("collapsible_request_id", UUID.randomUUID().toString()) extras.putString("collapsible_request_id", UUID.randomUUID().toString())
...@@ -45,7 +61,34 @@ object AdmobBannerUtils { ...@@ -45,7 +61,34 @@ object AdmobBannerUtils {
AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter::class.java, extras).build() AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter::class.java, extras).build()
adView?.adListener = adView?.adListener =
object : AdListener() { object : AdListener() {
override fun onAdLoaded() {}
override fun onAdImpression() {
super.onAdImpression()
val layout = parentView.layoutParams as FrameLayout.LayoutParams
layout.bottomMargin = context.dpToPx(height)
parentView.layoutParams = layout
}
override fun onAdLoaded() {
}
override fun onAdOpened() {
// LogEx.logDebug(TAG, "onAdOpened")
}
override fun onAdClosed() {
super.onAdClosed()
// LogEx.logDebug(TAG, "onAdClosed")
val removeList = arrayListOf<View>()
parent.children.forEach {
if (it.tag != "CollapsibleBannerAd") {
removeList.add(it)
}
}
removeList.forEach {
parent.removeView(it)
}
}
} }
adView?.loadAd(adRequest) adView?.loadAd(adRequest)
} }
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/parentlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/white"> android:orientation="vertical">
<androidx.viewpager2.widget.ViewPager2 <androidx.viewpager2.widget.ViewPager2
android:id="@+id/id_vp" android:id="@+id/id_vp"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/id_ll_nav" android:layout_weight="1" />
app:layout_constraintTop_toTopOf="parent" />
<View <View
android:id="@+id/id_view" android:id="@+id/id_view"
...@@ -92,11 +96,12 @@ ...@@ -92,11 +96,12 @@
</androidx.appcompat.widget.LinearLayoutCompat> </androidx.appcompat.widget.LinearLayoutCompat>
</LinearLayout>
<FrameLayout <FrameLayout
android:id="@+id/banner" android:id="@+id/banner"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent" android:layout_gravity="bottom" />
app:layout_constraintTop_toBottomOf="@id/id_ll_nav" </FrameLayout>
tools:ignore="UselessLeaf" /> \ No newline at end of file
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment