Skip to content

Instantly share code, notes, and snippets.

@Stickerbox
Created November 30, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stickerbox/128fa6dde11d7bf067f214836b5f4a5c to your computer and use it in GitHub Desktop.
Save Stickerbox/128fa6dde11d7bf067f214836b5f4a5c to your computer and use it in GitHub Desktop.
class CustView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
: ConstraintLayout(context, attrs, defStyleAttr) {
var buttonTapped: (() -> Unit)? = null
init {
val view = LayoutInflater.from(context).inflate(R.layout.custom_view, this, false)
val set = ConstraintSet()
addView(view)
set.clone(this)
set.match(view, this)
button.setOnClickListener {
buttonTapped?.invoke()
}
}
}
fun ConstraintSet.match(view: View, parentView: View) {
this.connect(view.id, ConstraintSet.TOP, parentView.id, ConstraintSet.TOP)
this.connect(view.id, ConstraintSet.START, parentView.id, ConstraintSet.START)
this.connect(view.id, ConstraintSet.END, parentView.id, ConstraintSet.END)
this.connect(view.id, ConstraintSet.BOTTOM, parentView.id, ConstraintSet.BOTTOM)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment