package com.example.basecore.widget
|
|
import android.app.Activity
|
import android.content.Context
|
import android.graphics.Color
|
import android.text.TextUtils
|
import android.util.AttributeSet
|
import android.view.LayoutInflater
|
import android.view.View
|
import android.widget.FrameLayout
|
import android.widget.ImageView
|
import android.widget.TextView
|
import androidx.core.view.isVisible
|
import com.example.basecore.R
|
|
|
class TitleLayout : FrameLayout {
|
private lateinit var mInflater: LayoutInflater
|
private lateinit var titleTv: TextView
|
private lateinit var moreTv: TextView
|
private lateinit var backTv: ImageView
|
private var mLayout: FrameLayout? = null
|
private lateinit var line: View
|
|
private lateinit var moreImg: ImageView
|
|
constructor(context: Context) : super(context)
|
|
|
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
|
initView(attrs)
|
}
|
|
|
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) :
|
super(context, attrs, defStyleAttr) {
|
initView(attrs)
|
}
|
|
|
private fun initView(attrs: AttributeSet?) {
|
mInflater = LayoutInflater.from(context)
|
val view: View = mInflater.inflate(R.layout.title_layout, this)
|
backTv = view.findViewById(R.id.topbar_back)
|
titleTv = view.findViewById(R.id.topbar_title)
|
moreTv = view.findViewById(R.id.topbar_more)
|
mLayout = view.findViewById(R.id.top_bar_layout)
|
line = view.findViewById(R.id.line)
|
|
moreImg = view.findViewById(R.id.more_img)
|
|
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleLayout)
|
val title = typedArray.getString(R.styleable.TitleLayout_title_text)
|
val imgLeft =
|
typedArray.getResourceId(R.styleable.TitleLayout_img_left, R.mipmap.ic_back_black)
|
val hideBack = typedArray.getBoolean(R.styleable.TitleLayout_hide_back, false)
|
typedArray.recycle()
|
if (!TextUtils.isEmpty(title)) {
|
titleTv.text = title
|
}
|
backTv.visibility = if (hideBack) View.GONE else View.VISIBLE
|
backTv.setImageResource(imgLeft)
|
backTv.setOnClickListener {
|
(context as Activity).finish()
|
mBackClickListener?.onBackClick()
|
}
|
}
|
|
fun getContainerView() = mLayout
|
|
|
private var mBackClickListener: OnBackEvent? = null
|
|
fun setBackClickListener(backClickListener: OnBackEvent) {
|
mBackClickListener = backClickListener
|
}
|
|
fun setMore(
|
more: String,
|
onClickListener: OnClickListener
|
) {
|
moreTv.visibility = View.VISIBLE
|
moreTv.text = more
|
moreTv.setOnClickListener(onClickListener)
|
}
|
|
fun setMoreImg(resource: Int?, onClickListener : OnClickListener) {
|
if (resource != null) {
|
moreImg.setImageResource(resource)
|
}
|
moreImg.isVisible = true
|
moreImg.setOnClickListener (onClickListener)
|
}
|
|
fun setLineVisibility(visibility: Int) {
|
line.visibility = visibility
|
}
|
|
fun getMoreTv(): TextView? {
|
return moreTv
|
}
|
|
fun setTitle(title: String?) {
|
if (title != null)
|
titleTv.text = title
|
}
|
|
fun setWhiteTitle() {
|
titleTv.setTextColor(Color.WHITE)
|
backTv.setImageResource(R.mipmap.ic_back_white)
|
}
|
|
|
interface OnBackEvent {
|
fun onBackClick()
|
}
|
}
|