feat(character): 캐릭터 탭에 Yandex 인라인 배너를 추가한다
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package kr.co.vividnext.sodalive.common
|
||||
|
||||
import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.yandex.mobile.ads.banner.BannerAdSize
|
||||
import com.yandex.mobile.ads.banner.BannerAdView
|
||||
import com.yandex.mobile.ads.common.AdRequest
|
||||
import kr.co.vividnext.sodalive.extensions.dpToPx
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
class YandexInlineBannerHeaderAdapter(
|
||||
private val adUnitId: String,
|
||||
private val screenWidth: Int
|
||||
) : RecyclerView.Adapter<YandexInlineBannerHeaderAdapter.BannerViewHolder>() {
|
||||
|
||||
private var bannerAdView: BannerAdView? = null
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
|
||||
val horizontalPadding = 24f.dpToPx().toInt()
|
||||
val verticalPadding = 24f.dpToPx().toInt()
|
||||
val container = FrameLayout(parent.context).apply {
|
||||
layoutParams = ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding)
|
||||
}
|
||||
val bannerView = BannerAdView(parent.context).apply {
|
||||
layoutParams = FrameLayout.LayoutParams(
|
||||
FrameLayout.LayoutParams.MATCH_PARENT,
|
||||
FrameLayout.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
}
|
||||
container.addView(bannerView)
|
||||
return BannerViewHolder(container, bannerView).apply {
|
||||
setIsRecyclable(false)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = 1
|
||||
|
||||
override fun onBindViewHolder(holder: BannerViewHolder, position: Int) {
|
||||
bannerAdView = holder.bannerView
|
||||
holder.bannerView.post {
|
||||
val density = holder.bannerView.resources.displayMetrics.density
|
||||
val adWidthPixels = holder.bannerView.width.takeIf { it > 0 }
|
||||
?: (screenWidth - 48f.dpToPx().toInt())
|
||||
val adWidthDp = (adWidthPixels / density).roundToInt().coerceAtLeast(1)
|
||||
val maxAdHeightDp = 90
|
||||
|
||||
holder.bannerView.apply {
|
||||
setAdUnitId(adUnitId)
|
||||
setAdSize(
|
||||
BannerAdSize.inlineSize(
|
||||
context,
|
||||
adWidthDp,
|
||||
maxAdHeightDp
|
||||
)
|
||||
)
|
||||
loadAd(AdRequest.Builder().build())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun destroy() {
|
||||
bannerAdView?.destroy()
|
||||
bannerAdView = null
|
||||
}
|
||||
|
||||
class BannerViewHolder(
|
||||
container: FrameLayout,
|
||||
val bannerView: BannerAdView
|
||||
) : RecyclerView.ViewHolder(container)
|
||||
}
|
||||
Reference in New Issue
Block a user