Commit 6023ade7 authored by mengcuiguang's avatar mengcuiguang

预加载优化

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