HomePagerBannerRealView.kt 6.7 KB
Newer Older
徐健 committed
1 2 3 4 5 6 7 8 9 10 11 12
package com.yidianling.home.ui.view

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.DecelerateInterpolator
import android.view.animation.TranslateAnimation
import android.widget.LinearLayout
徐健 committed
13
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
konghaorui committed
14
import com.yidianling.home.constants.HomeBIConstants
徐健 committed
15
import com.yidianling.home.R
16
import com.yidianling.home.event.IHomeBaseEvent
徐健 committed
17
import com.yidianling.home.model.bean.HomeHeaderBean
konghaorui committed
18 19 20
import kotlinx.android.synthetic.ydl.home_real_view_in.view.*
import kotlinx.android.synthetic.ydl.home_real_view_left.view.*
import kotlinx.android.synthetic.ydl.home_real_view_out.view.*
徐健 committed
21 22 23 24 25 26 27 28

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 首页顶部模块-动态标签模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/16
 */
29
class HomePagerBannerRealView(mContext : Context,homeEvent : IHomeBaseEvent) : LinearLayout(mContext){
徐健 committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

    private val STATUS_IN = 0
    private val STATUS_OUT = 1
    private var curTipIndex = 0
    private var lastTimeMillis: Long = 0
    private val ANIM_DELAYED_MILLIONS = 2 * 1000
    /**
     * 动画持续时长
     */
    private val ANIM_DURATION = 500
    /**
     * 进、出 两个view (主要用于做动画,其实是两个相同的布局文件)
     */
    private var view_out: View? = null
    private var view_in:View? = null
    /**
     * 进、出 两个View 的动画
     */
    private var anim_out: Animation? = null
    private var anim_in:Animation? = null
    /**
     * 数据缓存
     */
    private var mDataList : List<HomeHeaderBean.HomeSaleDataBean>? = null

55
    private var homeEvent : IHomeBaseEvent? = null
徐健 committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    private val mHandler: Handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            updateTipAndPlayAnimation()
            sendMessageDelayed(Message(), ANIM_DELAYED_MILLIONS.toLong())
        }
    }

    init {
        this.homeEvent = homeEvent
        initView()
        initAnimation()
    }

    /**
     * 界面初始化
     */
    private fun initView() {
        orientation = HORIZONTAL
        var params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
konghaorui committed
77 78 79
        View.inflate(context, R.layout.home_real_view_left, this)
        view_out = View.inflate(context, R.layout.home_real_view_out,null)
        view_in = View.inflate(context, R.layout.home_real_view_in,null)
徐健 committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        homeModuleRealViewLeftFrameLayout.addView(view_out)
        homeModuleRealViewLeftFrameLayout.addView(view_in)
        layoutParams = params
    }

    private fun initAnimation() {
        anim_out = newAnimation(0f, -1f)
        anim_in = newAnimation(1f, 0f)
        anim_in!!.setAnimationListener(object : Animation.AnimationListener {

            override fun onAnimationStart(animation: Animation) {

            }

            override fun onAnimationRepeat(animation: Animation) {

            }

            override fun onAnimationEnd(animation: Animation) {
                updateViewVisibility()
            }
        })
    }

    /**
     * 设置数据
     */
    fun initData(list: List<HomeHeaderBean.HomeSaleDataBean>?){
        mHandler.removeCallbacksAndMessages(null)
        if (null == list || list.isEmpty()){
            visibility = View.GONE
            return
        }
        visibility = View.VISIBLE
        if (null == mDataList){
            mDataList = ArrayList()
        }else{
            (mDataList as ArrayList).clear()
        }
        (mDataList as ArrayList).addAll(list)
        curTipIndex = 0
        updateTip(STATUS_OUT)
        updateTipAndPlayAnimation()
        mHandler.sendMessageDelayed(Message(), ANIM_DELAYED_MILLIONS.toLong())
    }

    private fun updateViewVisibility() {
        if (curTipIndex % 2 == 0) {
            view_out!!.visibility = View.INVISIBLE
        } else {
            view_in!!.visibility = View.INVISIBLE
        }
    }

    private fun newAnimation(fromYValue: Float, toYValue: Float): Animation {
        val anim = TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, fromYValue, Animation.RELATIVE_TO_SELF, toYValue)
        anim.duration = ANIM_DURATION.toLong()
        anim.interpolator = DecelerateInterpolator()
        return anim
    }

    private fun updateTipAndPlayAnimation() {
        view_in!!.visibility = View.VISIBLE
        view_out!!.visibility = View.VISIBLE
        if (curTipIndex % 2 == 0) {
            updateTip(STATUS_OUT)
            view_in!!.startAnimation(anim_out)
            view_out!!.startAnimation(anim_in)
            this.bringChildToFront(view_in)
        } else {
            updateTip(STATUS_IN)
            view_out!!.startAnimation(anim_out)
            view_in!!.startAnimation(anim_in)
            this.bringChildToFront(view_out)
        }
    }

    private fun updateTip(status : Int){
        val bodyBean = getNextTip() ?: return

        when(status){
            STATUS_IN -> {
                tv_in_doctor_name.text = bodyBean.doctorName
                tv_in_content_before.text = getName(bodyBean)
                tv_in_content_after.text = String.format("老师%s",bodyBean.contentAfter)
                view_in!!.setOnClickListener{
徐健 committed
167
                    ActionCountUtils.count(HomeBIConstants.YDL_USER_NOTICE_CLICK)
徐健 committed
168 169 170 171 172 173 174 175
                    homeEvent!!.linkTo(bodyBean.url!!)
                }
            }
            STATUS_OUT -> {
                tv_out_doctor_name.text = bodyBean.doctorName
                tv_out_content_before.text = getName(bodyBean)
                tv_out_content_after.text = String.format("老师%s",bodyBean.contentAfter)
                view_out!!.setOnClickListener{
徐健 committed
176
                    ActionCountUtils.count(HomeBIConstants.YDL_USER_NOTICE_CLICK)
徐健 committed
177 178 179 180 181 182 183 184 185 186 187
                    homeEvent!!.linkTo(bodyBean.url!!)
                }
            }
        }
    }

    private fun getName(bean : HomeHeaderBean.HomeSaleDataBean?) : String{
        var nameBuffer = StringBuffer()
        nameBuffer.append(bean?.contentBefore)
        nameBuffer.append(" ")
        nameBuffer.append(bean?.nickName)
konghaorui committed
188
        nameBuffer.append(resources.getString(R.string.home_real_appoint))
徐健 committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202
        return nameBuffer.toString()
    }

    private fun getNextTip() : HomeHeaderBean.HomeSaleDataBean? {
        if (null == mDataList || mDataList!!.isEmpty()){
            return null
        }
        return mDataList!![curTipIndex++ % mDataList!!.size]
    }

    fun onDestory(){
        mHandler.removeCallbacksAndMessages(null)
    }
}