Commit 6023ade7 authored by mengcuiguang's avatar mengcuiguang

预加载优化

parent 5fdbd924
......@@ -8,6 +8,7 @@ import com.anythink.core.api.ATAdConst
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.anythink.nativead.api.*
import com.mints.xunmi.MintsApplication
import com.mints.xunmi.manager.TopOnManager
import com.mints.xunmi.ui.widgets.NativeDemoRender
import com.mints.xunmi.utils.LogUtil
......@@ -21,19 +22,20 @@ object TopOnNativeAd {
private val TAG = TopOnNativeAd::class.java.simpleName
var context: Context? = null
var atNatives: ATNative? = null
var mNativeAd: NativeAd? = null
var anyThinkNativeAdView: ATNativeAdView? = null
private lateinit var context: Context
private var atNatives: ATNative? = null
private var mNativeAd: NativeAd? = null
private var anyThinkNativeAdView: ATNativeAdView? = null
var padding: Int = 0
var containerHeight: Int = 0
private var padding: Int = 0
private var containerHeight: Int = 0
/**
* 应用初始化
*/
fun init(context: Context) {
this.context = context
fun init() {
val contextMints = MintsApplication.getContext()
this.context = contextMints
atNatives = ATNative(context, TopOnManager.INFORMATION_ID, object : ATNativeNetworkListener {
override fun onNativeAdLoaded() {
LogUtil.e(TAG, "onNativeAdLoaded")
......@@ -43,9 +45,9 @@ object TopOnNativeAd {
LogUtil.e(TAG, "onNativeAdLoadFail" + adError.desc)
}
})
padding = dip2px(context, 10f)
containerHeight = dip2px(context, 290f)
val adViewWidth: Int = context.getResources().getDisplayMetrics().widthPixels - 2 * padding
padding = dip2px(contextMints, 10f)
containerHeight = dip2px(contextMints, 290f)
val adViewWidth: Int = contextMints.getResources().getDisplayMetrics().widthPixels - 2 * padding
val adViewHeight = containerHeight - 2 * padding
val localMap: MutableMap<String, Any> = HashMap()
// since v5.6.4
......@@ -61,18 +63,19 @@ object TopOnNativeAd {
}
/**
* 预加载
* 预加载广告
*/
fun loadNativeAd(frameLayout: FrameLayout?) {
atNatives?.makeAdRequest()
showNativeAd(frameLayout)
fun loadNativeAd() {
try {
atNatives?.makeAdRequest()
} catch (e: Exception) {
}
}
/**
* 显示
* 显示广告
*/
private fun showNativeAd(frameLayout: FrameLayout?) {
fun showNativeAd(frameLayout: FrameLayout?) {
val nativeAd = atNatives?.nativeAd
nativeAd?.let {
if (mNativeAd != null) {
......@@ -120,10 +123,13 @@ object TopOnNativeAd {
it.prepare(anyThinkNativeAdView, anyThinkRender.getClickView(), null)
if (anyThinkNativeAdView != null && anyThinkNativeAdView!!.parent == null) {
frameLayout?.addView(anyThinkNativeAdView, FrameLayout.LayoutParams(context?.getResources()!!.getDisplayMetrics().widthPixels, containerHeight))
frameLayout?.addView(anyThinkNativeAdView, FrameLayout.LayoutParams(context.getResources().getDisplayMetrics().widthPixels, containerHeight))
}
}
}
// 预加载
loadNativeAd()
}
fun dip2px(context: Context, dipValue: Float): Int {
......@@ -142,6 +148,5 @@ object TopOnNativeAd {
fun onDestroy() {
anyThinkNativeAdView?.destory()
mNativeAd?.destory()
context = null
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.mints.library.recyclerview.HeaderAndFooterRecyclerViewAdapter
import com.mints.library.recyclerview.RecyclerViewUtils
import com.mints.library.utils.nodoubleclick.AntiShake
import com.mints.xunmi.MintsApplication
import com.mints.xunmi.R
import com.mints.xunmi.ad.TopOnNativeAd
import com.mints.xunmi.common.AppConfig
......@@ -69,21 +70,8 @@ class MainFragment : BaseFragment()
override fun getContentViewLayoutID() = R.layout.fragment_main_first
override fun initViewsAndEvents() {
if (!::mainFragmentPresenter.isInitialized) {
mainFragmentPresenter = MainFragmentPresenter()
}
mainFragmentPresenter.attachView(this)
initManager()
context?.let {
TopOnNativeAd.init(it)
TopOnNativeAd.loadNativeAd(null)
}
linearLayoutManager = LinearLayoutManager(activity)
rvMain.layoutManager = linearLayoutManager
initTopOnNativeAd()
initLinstener()
......@@ -95,7 +83,15 @@ class MainFragment : BaseFragment()
// }
// }
mHandler = WeakHandler(this)
}
/**
* 初始化 预加载 topon信息流
*/
private fun initTopOnNativeAd() {
TopOnNativeAd.init()
TopOnNativeAd.loadNativeAd()
}
override fun onHiddenChanged(hidden: Boolean) {
......@@ -316,6 +312,11 @@ class MainFragment : BaseFragment()
}
private fun initManager() {
if (!::mainFragmentPresenter.isInitialized) {
mainFragmentPresenter = MainFragmentPresenter()
}
mainFragmentPresenter.attachView(this)
spUtil = SPUtil.getInstance()
userManager = UserManager.getInstance()
......@@ -323,6 +324,11 @@ class MainFragment : BaseFragment()
if (spUtil?.getBoolean(Constant.LOAN_PERMISSION_FLAG, true)!!) {
powerDialog()
}
linearLayoutManager = LinearLayoutManager(activity)
rvMain.layoutManager = linearLayoutManager
mHandler = WeakHandler(this)
}
private fun initLinstener() {
......
......@@ -92,7 +92,7 @@ class MyFragment : BaseFragment()
myPresenter?.userLogin(context!!)
}
TopOnNativeAd.loadNativeAd(flMyAdContainer)
TopOnNativeAd.showNativeAd(flMyAdContainer)
}
}
......
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