使用DataBinding自定义控件:
1、view_time_type.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.caowj.enetcloud.view.widget.RadiusTextView
android:id="@+id/rtv_week"
style="@style/type_selected"
android:text="@string/type_week"
app:layout_constraintEnd_toStartOf="@+id/rtv_month"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.caowj.enetcloud.view.widget.RadiusTextView
android:id="@+id/rtv_month"
style="@style/type_unselected"
android:text="@string/type_month"
app:layout_constraintEnd_toStartOf="@+id/rtv_quarter"
app:layout_constraintStart_toEndOf="@+id/rtv_week"
app:layout_constraintTop_toTopOf="@+id/rtv_week" />
<com.caowj.enetcloud.view.widget.RadiusTextView
android:id="@+id/rtv_quarter"
style="@style/type_unselected"
android:text="@string/type_quarter"
app:layout_constraintEnd_toStartOf="@+id/rtv_year"
app:layout_constraintStart_toEndOf="@+id/rtv_month"
app:layout_constraintTop_toTopOf="@+id/rtv_month" />
<com.caowj.enetcloud.view.widget.RadiusTextView
android:id="@+id/rtv_year"
style="@style/type_unselected"
android:text="@string/type_year"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/rtv_quarter"
app:layout_constraintTop_toTopOf="@+id/rtv_quarter" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
2、TimeTypeView.kt
package com.caowj.enetcloud.view.widget
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.DataBindingUtil
import com.caowj.enetcloud.R
import com.caowj.enetcloud.databinding.ViewTimeTypeBinding
class TimeTypeView : ConstraintLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
constructor(
context: Context,
attrs: AttributeSet?,
defStyleAttr: Int,
defStyleRes: Int
) : super(context, attrs, defStyleAttr, defStyleRes)
private lateinit var _binding: ViewTimeTypeBinding
private var onWeekViewClick: (() -> Unit)? = null
private var onMonthViewClick: (() -> Unit)? = null
private var onQuarterViewClick: (() -> Unit)? = null
private var onYearViewClick: (() -> Unit)? = null
init {
initBinding()
initListener()
}
private fun initBinding() {
val inflater: LayoutInflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
_binding = DataBindingUtil.inflate(inflater, R.layout.view_time_type, this, true)
}
private fun initListener() {
_binding.rtvWeek.setOnClickListener {
initDate()
setSelectedItem(_binding.rtvWeek)
onWeekViewClick?.invoke()
}
_binding.rtvMonth.setOnClickListener {
initDate()
setSelectedItem(_binding.rtvMonth)
onMonthViewClick?.invoke()
}
_binding.rtvQuarter.setOnClickListener {
initDate()
setSelectedItem(_binding.rtvQuarter)
onQuarterViewClick?.invoke()
}
_binding.rtvYear.setOnClickListener {
initDate()
setSelectedItem(_binding.rtvYear)
onYearViewClick?.invoke()
}
}
private fun initDate() {
_binding.rtvWeek.setViewSelected(false)
_binding.rtvMonth.setViewSelected(false)
_binding.rtvQuarter.setViewSelected(false)
_binding.rtvYear.setViewSelected(false)
}
private fun setSelectedItem(radiusTextView: RadiusTextView) {
radiusTextView.setViewSelected(true)
}
fun setOnClick(
weekListener: () -> Unit,
monthListener: () -> Unit,
quarterListener: () -> Unit,
yearListener: () -> Unit
) {
this.onWeekViewClick = weekListener
this.onMonthViewClick = monthListener
this.onQuarterViewClick = quarterListener
this.onYearViewClick = yearListener
}
fun setOnWeekViewClick(weekListener: () -> Unit): TimeTypeView {
this.onWeekViewClick = weekListener
return this
}
fun setOnMonthViewClick(monthListener: () -> Unit): TimeTypeView {
this.onMonthViewClick = monthListener
return this
}
fun setOnQuarterViewClick(quarterListener: () -> Unit): TimeTypeView {
this.onQuarterViewClick = quarterListener
return this
}
fun setOnYearViewClick(yearListener: () -> Unit): TimeTypeView {
this.onYearViewClick = yearListener
return this
}
}