diff --git a/README.md b/README.md index 7f82169..4fd8a7d 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,18 @@ public class PersonView(context: Context, attrs: AttributeSet?) : LinearLayout(c // List binding with optional items being omitted. val nameViews: List by bindOptionalViews(R.id.first_name, R.id.middle_name, R.id.last_name) + + // Binding elements to custom container + class ExampleViewContainer(view: View) : ViewContainer(view) { + val name: View by bindView(R.id.name) + } + var container: ExampleViewContainer by Delegates.notNull() + container = ExampleViewContainer(someView) } ``` These methods are available on subclasses of `Activity`, `Dialog`, `ViewGroup`, `Fragment`, -the support library `Fragment`, and recycler view's `ViewHolder`. +the support library `Fragment`, and recycler view's `ViewHolder` or custom container based on `ViewContainer`. diff --git a/src/androidTest/kotlin/butterknife/ViewTest.kt b/src/androidTest/kotlin/butterknife/ViewTest.kt index a7d9750..a6a5acc 100644 --- a/src/androidTest/kotlin/butterknife/ViewTest.kt +++ b/src/androidTest/kotlin/butterknife/ViewTest.kt @@ -144,6 +144,17 @@ public class ViewTest : AndroidTestCase() { assertEquals(2, example.name.size) } + public fun testViewContainerBind() { + class ExampleViewContainer(view: View) : ViewContainer(view) { + val name: View by bindView(1) + } + + val layout = FrameLayout(getContext()) + layout.addView(viewWithId(1)) + val example = ExampleViewContainer(layout) + assertNotNull(example.name) + } + private fun viewWithId(id: Int) : View { val view = View(getContext()) view.setId(id) diff --git a/src/main/kotlin/butterknife/ButterKnife.kt b/src/main/kotlin/butterknife/ButterKnife.kt index c8f2547..60b4229 100644 --- a/src/main/kotlin/butterknife/ButterKnife.kt +++ b/src/main/kotlin/butterknife/ButterKnife.kt @@ -15,6 +15,7 @@ public fun Dialog.bindView(id: Int): ReadOnlyProperty = ViewB public fun Fragment.bindView(id: Int): ReadOnlyProperty = ViewBinding(id) public fun SupportFragment.bindView(id: Int): ReadOnlyProperty = ViewBinding(id) public fun ViewHolder.bindView(id: Int): ReadOnlyProperty = ViewBinding(id) +public fun ViewContainer.bindView(id: Int): ReadOnlyProperty = ViewBinding(id) public fun ViewGroup.bindOptionalView(id: Int): ReadOnlyProperty = OptionalViewBinding(id) public fun Activity.bindOptionalView(id: Int): ReadOnlyProperty = OptionalViewBinding(id) @@ -22,6 +23,7 @@ public fun Dialog.bindOptionalView(id: Int): ReadOnlyProperty Fragment.bindOptionalView(id: Int): ReadOnlyProperty = OptionalViewBinding(id) public fun SupportFragment.bindOptionalView(id: Int): ReadOnlyProperty = OptionalViewBinding(id) public fun ViewHolder.bindOptionalView(id: Int): ReadOnlyProperty = OptionalViewBinding(id) +public fun ViewContainer.bindOptionalView(id: Int): ReadOnlyProperty = OptionalViewBinding(id) public fun ViewGroup.bindViews(vararg ids: Int): ReadOnlyProperty> = ViewListBinding(ids) public fun Activity.bindViews(vararg ids: Int): ReadOnlyProperty> = ViewListBinding(ids) @@ -29,6 +31,7 @@ public fun Dialog.bindViews(vararg ids: Int): ReadOnlyProperty Fragment.bindViews(vararg ids: Int): ReadOnlyProperty> = ViewListBinding(ids) public fun SupportFragment.bindViews(vararg ids: Int): ReadOnlyProperty> = ViewListBinding(ids) public fun ViewHolder.bindViews(vararg ids: Int): ReadOnlyProperty> = ViewListBinding(ids) +public fun ViewContainer.bindViews(vararg ids: Int): ReadOnlyProperty> = ViewListBinding(ids) public fun ViewGroup.bindOptionalViews(vararg ids: Int): ReadOnlyProperty> = OptionalViewListBinding(ids) public fun Activity.bindOptionalViews(vararg ids: Int): ReadOnlyProperty> = OptionalViewListBinding(ids) @@ -36,6 +39,7 @@ public fun Dialog.bindOptionalViews(vararg ids: Int): ReadOnlyPropert public fun Fragment.bindOptionalViews(vararg ids: Int): ReadOnlyProperty> = OptionalViewListBinding(ids) public fun SupportFragment.bindOptionalViews(vararg ids: Int): ReadOnlyProperty> = OptionalViewListBinding(ids) public fun ViewHolder.bindOptionalViews(vararg ids: Int): ReadOnlyProperty> = OptionalViewListBinding(ids) +public fun ViewContainer.bindOptionalViews(vararg ids: Int): ReadOnlyProperty> = OptionalViewListBinding(ids) private fun findView(thisRef: Any, id: Int): T? { [suppress("UNCHECKED_CAST")] @@ -46,6 +50,7 @@ private fun findView(thisRef: Any, id: Int): T? { is Fragment -> thisRef.getView().findViewById(id) is SupportFragment -> thisRef.getView().findViewById(id) is ViewHolder -> thisRef.itemView.findViewById(id) + is ViewContainer -> thisRef.view.findViewById(id) else -> throw IllegalStateException("Unable to find views on type.") } as T? } @@ -97,3 +102,6 @@ private class Lazy { return value as T } } + +public open class ViewContainer(val view: View) { +} \ No newline at end of file