HomeCourseItemView.kt 2.46 KB
Newer Older
1 2
package com.yidianling.home.ui.view

ydl committed
3
import android.annotation.SuppressLint
4 5 6 7 8 9 10 11
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import com.ydl.ydl_image.manager.YDLImageCacheManager
import com.yidianling.home.R
import com.yidianling.home.event.IHomeBaseEvent
import com.yidianling.home.model.bean.HomeCourseBean
konghaorui committed
12
import kotlinx.android.synthetic.xlzx.home_course_item_view.view.*
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 课程成长模块item
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/14
 */
class HomeCourseItemView(private val mContext: Context, private var homeEvent: IHomeBaseEvent?) :
    RelativeLayout(mContext) {

    init {
        initView()
    }

    private fun initView() {
        val params = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        layoutParams = params
konghaorui committed
34
        View.inflate(mContext, R.layout.home_course_item_view, this)
35 36 37 38 39
    }

    /**
     * 刷新数据
     */
ydl committed
40
    @SuppressLint("SetTextI18n")
41 42 43 44 45 46 47 48 49 50 51 52
    fun updateData(bean: HomeCourseBean.ListBean?) {

        YDLImageCacheManager.showImage(mContext, bean?.pic, homeModuleCourseItemViewImg)
        homeModuleCourseItemViewTitle.text = bean?.title
        homeModuleCourseItemViewPersonNum.text = bean?.readNums.toString()

        if (bean?.applyFee == 0f) {
            homeModuleCourseItemViewFreePrice.visibility = View.VISIBLE
            homeModuleCourseItemViewExpensePriceHideView.visibility = View.GONE
        } else {
            if (bean?.isPromotion == 1) {
                homeModuleCourseItemViewExpensePrice.text =
ydl committed
53
                    "¥" + getLastPrice(String.format("%.2f", bean?.promotionApplyFee?:0))
54 55
            } else {
                homeModuleCourseItemViewExpensePrice.text =
ydl committed
56
                    "¥" + getLastPrice(String.format("%.2f", bean?.applyFee?:0))
57 58 59 60
            }
        }

        setOnClickListener {
ydl committed
61 62 63 64 65 66 67 68
            bean?.let {
                homeEvent?.courseItemClick(bean)
            }
        }
    }

    fun getLastPrice(price: String): String {
        if (price.length > 3) {
ydl committed
69
            if ("0" == price[price.lastIndex].toString() && "0" == price[price.lastIndex - 1].toString() && "." == price[price.lastIndex - 2].toString()
ydl committed
70 71 72
            ) {
                return price.substring(0, price.length - 3)
            }
73
        }
ydl committed
74
        return price
75 76
    }
}