JumpTextView.kt 8.32 KB
Newer Older
1
package com.ydl.ydlcommon.view
konghaorui committed
2 3 4 5 6 7 8 9 10

import android.content.Context
import android.graphics.drawable.Drawable
import android.text.InputFilter
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
konghaorui committed
11
import android.widget.ImageView
12
import com.ydl.ydlcommon.R
konghaorui committed
13
import kotlinx.android.synthetic.main.platform_ui_jump_text_view.view.*
konghaorui committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

/**
 * 右侧带箭头,高度最低60dp,左侧可以设置图标,左侧文字,右侧文字,文字颜色
 * Created by Dog on 2015/6/24.
 */
class JumpTextView : FrameLayout {


    //内容
    internal var mLeftTextContent: String? = null
    internal var mRightTextContent: String? = null
    //文字大小
    internal var mLeftTextSize: Int = 0
    internal var mRightTextSize: Int = 0
    //颜色
    internal var mLeftTextColor: Int = 0
    internal var mRightTextColor: Int = 0
    //图标
    internal var mIconDrawable: Drawable? = null
    //图标大小
    internal var mLeftIconWidth: Int = 0
    internal var mLeftIconHeight: Int = 0
    //文字的最大行数
    internal var maxLine = -1
    //是否隐藏右箭头
    internal var hideArrow = false
    //右箭头资源文件id
    internal var arrowRes: Int = 0
    //右侧最多显示数字
    internal var rightMaxLength = -1
    //是否可以点击
    internal var isClickEnable = true
    //右侧文字gravity
    internal var rightGravity = 1
    //左侧文字宽度
    internal var leftTextWidth = -1
    //左侧小红点
    internal var isRedDotShow = false


    /**
     * 设置左侧文字
     */
    var leftText: String?
        get() = mLeftTextContent
        set(leftTextContent) {
            mLeftTextContent = leftTextContent
            setupViews()
        }

    /**
     * 设置右侧文字
     */
    var rightText: String?
        get() = mRightTextContent
        set(s) {
            if (s == null) return
            mRightTextContent = s
            setupViews()
        }

    constructor(context: Context) : super(context) {
konghaorui committed
76
        View.inflate(context, R.layout.platform_ui_jump_text_view, this)
konghaorui committed
77 78 79 80 81 82 83

        initRootView(null, 0)

        setupViews()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
konghaorui committed
84
        View.inflate(context, R.layout.platform_ui_jump_text_view, this)
konghaorui committed
85 86 87 88 89 90 91

        initRootView(attrs, 0)

        setupViews()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
konghaorui committed
92
        View.inflate(context, R.layout.platform_ui_jump_text_view, this)
konghaorui committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        initRootView(attrs, defStyleAttr)

        setupViews()
    }

    /**
     * 设置左侧文字红点是否显示
     */
    fun setLeftRedDotVisibility(visibitity: Int) {
        img_reddot!!.visibility = visibitity
    }

    /**
     * 是否隐藏箭头
     *
     * @param isArrowShow 是否隐藏
     */
    fun setArrow(isArrowShow: Boolean) {
        hideArrow = !isArrowShow

        if (hideArrow) {
            arrowRes = 0
        } else {
konghaorui committed
116
            arrowRes = R.drawable.platform_mine_next
konghaorui committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        }

        tv_right_text!!.setCompoundDrawablesWithIntrinsicBounds(0, 0, arrowRes, 0)
    }

    fun setClickEnable(enable: Boolean) {
        isClickEnable = enable
    }

    /**
     * 初始化根视图
     */
    internal fun initRootView(attrs: AttributeSet?, defStyleAttr: Int) {

        val a = context.obtainStyledAttributes(
konghaorui committed
132
                attrs, R.styleable.Platform_JumpTextView, defStyleAttr, 0)
konghaorui committed
133

konghaorui committed
134 135 136 137 138
        mLeftTextContent = a.getString(R.styleable.Platform_JumpTextView_pa_jump_tv_left_text)
        mLeftTextColor = a.getColor(R.styleable.Platform_JumpTextView_pa_jump_tv_left_color,
                resources.getColor(R.color.platform_default_text_color))
        mLeftTextSize = a.getDimensionPixelSize(R.styleable.Platform_JumpTextView_pa_jump_tv_left_size,
                resources.getDimensionPixelSize(R.dimen.platform_default_text_size))
konghaorui committed
139

konghaorui committed
140 141 142 143 144
        mRightTextContent = a.getString(R.styleable.Platform_JumpTextView_pa_jump_tv_right_text)
        mRightTextColor = a.getColor(R.styleable.Platform_JumpTextView_pa_jump_tv_right_color,
                resources.getColor(R.color.platform_default_text_color))
        mRightTextSize = a.getDimensionPixelSize(R.styleable.Platform_JumpTextView_pa_jump_tv_right_size,
                resources.getDimensionPixelSize(R.dimen.platform_default_text_size))
konghaorui committed
145

konghaorui committed
146
        mIconDrawable = a.getDrawable(R.styleable.Platform_JumpTextView_pa_jump_tv_icon)
konghaorui committed
147

konghaorui committed
148
        mLeftIconWidth = a.getDimensionPixelSize(R.styleable.Platform_JumpTextView_pa_jump_tv_left_icon_width,
konghaorui committed
149 150
                -1)

konghaorui committed
151
        mLeftIconHeight = a.getDimensionPixelSize(R.styleable.Platform_JumpTextView_pa_jump_tv_left_icon_hight,
konghaorui committed
152 153
                -1)

konghaorui committed
154
        maxLine = a.getInteger(R.styleable.Platform_JumpTextView_pa_jump_tv_max_line, -1)
konghaorui committed
155

konghaorui committed
156 157
        hideArrow = a.getBoolean(R.styleable.Platform_JumpTextView_pa_jump_tv_hide_arrow, false)
        isClickEnable = a.getBoolean(R.styleable.Platform_JumpTextView_pa_jump_tv_can_click, !hideArrow)
konghaorui committed
158

konghaorui committed
159
        rightMaxLength = a.getInt(R.styleable.Platform_JumpTextView_pa_jump_tv_right_max_length, rightMaxLength)
konghaorui committed
160

konghaorui committed
161 162
        leftTextWidth = a.getDimensionPixelSize(R.styleable.Platform_JumpTextView_pa_jump_tv_left_width, -1)
        rightGravity = a.getInteger(R.styleable.Platform_JumpTextView_pa_jump_tv_right_gravity, 2)
konghaorui committed
163

konghaorui committed
164
        isRedDotShow = a.getBoolean(R.styleable.Platform_JumpTextView_pa_jump_tv_left_red_dot, false)
konghaorui committed
165 166

        a.recycle()
konghaorui committed
167
        minimumHeight = resources.getDimensionPixelSize(R.dimen.platform_jump_text_view_min_height)
konghaorui committed
168 169 170 171 172
        //setBackgroundColor(Color.WHITE);

        if (hideArrow) {
            arrowRes = 0
        } else {
konghaorui committed
173
            arrowRes = R.drawable.platform_mine_next
konghaorui committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        }
        setWillNotDraw(false)
    }


    /**
     * 初始化子view
     */
    internal fun setupViews() {
        if (rightMaxLength != -1) {

            val filters = tv_right_text!!.filters
            val newFilters = arrayOfNulls<InputFilter>(filters.size + 1)
            System.arraycopy(filters, 0, newFilters, 0, filters.size)
            newFilters[filters.size] = InputFilter.LengthFilter(rightMaxLength)
            tv_right_text!!.filters = newFilters
        }

        tv_right_text!!.setCompoundDrawablesWithIntrinsicBounds(0, 0, arrowRes, 0)

        if (mIconDrawable != null) {
            iv_icon!!.visibility = View.VISIBLE
            iv_icon!!.setImageDrawable(mIconDrawable)
            if (mLeftIconWidth != -1 && mLeftIconHeight != -1) {
                iv_icon!!.layoutParams.height = mLeftIconHeight
                iv_icon!!.layoutParams.width = mLeftIconWidth
            }
        } else {
            iv_icon!!.visibility = View.GONE
        }

        tv_left_text!!.text = mLeftTextContent
        tv_left_text!!.setTextColor(mLeftTextColor)
        tv_left_text!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLeftTextSize.toFloat())

        tv_right_text!!.text = mRightTextContent
        tv_right_text!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, mRightTextSize.toFloat())
        tv_right_text!!.setTextColor(mRightTextColor)

        if (maxLine != -1) {
            tv_left_text!!.maxLines = maxLine
            tv_right_text!!.maxLines = maxLine
        }

        if (leftTextWidth != -1) {
            tv_left_text!!.width = leftTextWidth
        }

        if (rightGravity == 1) {
            tv_right_text!!.gravity = Gravity.LEFT
        } else {
            tv_right_text!!.gravity = Gravity.RIGHT
        }

        if (isRedDotShow) {
            img_reddot!!.visibility = View.VISIBLE
        } else {
            img_reddot!!.visibility = View.GONE
        }
    }

    fun setRighttextColor(color: Int) {
        this.mRightTextColor = color
        tv_right_text!!.setTextColor(color)
    }
konghaorui committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

    fun getTvIcon(): ImageView {
        return iv_icon!!
    }

    fun setTvIcon(iconDrawable: Drawable) {
        mIconDrawable  = iconDrawable
        iv_icon!!.visibility = View.VISIBLE
        iv_icon!!.setImageDrawable(mIconDrawable)
        if (mLeftIconWidth != -1 && mLeftIconHeight != -1) {
            iv_icon!!.layoutParams.height = mLeftIconHeight
            iv_icon!!.layoutParams.width = mLeftIconWidth
        }
    }

konghaorui committed
254 255 256 257 258 259 260 261 262 263
    fun setLefttextColor(color: Int) {
        this.mLeftTextColor = color
        tv_left_text!!.setTextColor(color)
    }

    fun setRight(s: String) {
        mRightTextContent = s
        setupViews()
    }
}