Created
November 30, 2017 12:31
-
-
Save Stickerbox/128fa6dde11d7bf067f214836b5f4a5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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