AdViewPagerAdapter.kt 2.6 KB
Newer Older
YKai committed
1
package com.yidianling.dynamic.trendsDetail
konghaorui committed
2 3

import android.content.Context
YKai committed
4
import androidx.viewpager.widget.PagerAdapter
konghaorui committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
import com.ydl.ydl_image.module.GlideApp
import com.ydl.ydl_router.manager.YDLRouterManager
import com.ydl.ydlcommon.utils.BuryPointUtils
import com.ydl.ydlcommon.utils.ScreenUtil
import com.yidianling.dynamic.R
import com.yidianling.dynamic.common.net.DynamicApiUtils
import com.yidianling.dynamic.model.Command
import com.yidianling.dynamic.model.TrendsDetailInfoBean
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
konghaorui committed
20
import kotlinx.android.synthetic.main.dynamic_item_trend_detail_ad.view.*
konghaorui committed
21 22 23 24 25 26 27 28 29

/**
 * 动态广告图展示
 * Created by zqk on 17-7-10.
 */
class AdViewPagerAdapter(private val context: Context, private val adList: MutableList<TrendsDetailInfoBean.Ad>) : PagerAdapter() {

    var onClickListener: View.OnClickListener? = null

YKai committed
30
    override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object`
konghaorui committed
31 32 33

    override fun getCount() = adList.size

YKai committed
34
    override fun instantiateItem(container: ViewGroup, position: Int): Any {
konghaorui committed
35
        val view = LayoutInflater.from(context).inflate(R.layout.dynamic_item_trend_detail_ad, container, false)
konghaorui committed
36 37 38 39
        view.ivAd.layoutParams.height = ScreenUtil.getDisplayWidth() * 19 / 66
        val ad = adList[position]
        GlideApp.with(context)
                .load(ad.adImg)
konghaorui committed
40
                .placeholder(R.drawable.dynamic_default_img)
konghaorui committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
                .into(view.ivAd)
        view.setOnClickListener {

            if (YDLRouterManager.router(ad.url)) {

            }else{
                val h5Params = H5Params(ad.url, null, ad.share)
                NewH5Activity.start(context, h5Params)
                DynamicApiUtils.adClickCount(Command.AdClickCount(ad.focId))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe({}, {})
                BuryPointUtils.getInstance().createMap()
                        .put("dynamic_ad_name",ad.share?.title)
                        .put("dynamic_ad_location","${position+1}")
                        .burryPoint("Dynamic_AD_Click")
            }
        }
YKai committed
59
        container.addView(view)
konghaorui committed
60 61 62 63
        view.ivAd.requestLayout()
        return view
    }

YKai committed
64
    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
konghaorui committed
65
        val view = `object` as View
YKai committed
66
        container.removeView(view)
konghaorui committed
67 68 69 70
    }


}