Skip to content
This repository has been archived by the owner on Sep 12, 2019. It is now read-only.

Bindings to custom container based on ViewContainer #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ public class PersonView(context: Context, attrs: AttributeSet?) : LinearLayout(c

// List binding with optional items being omitted.
val nameViews: List<TextView> 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`.



Expand Down
11 changes: 11 additions & 0 deletions src/androidTest/kotlin/butterknife/ViewTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/butterknife/ButterKnife.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,31 @@ public fun <T : View> Dialog.bindView(id: Int): ReadOnlyProperty<Any, T> = ViewB
public fun <T : View> Fragment.bindView(id: Int): ReadOnlyProperty<Any, T> = ViewBinding(id)
public fun <T : View> SupportFragment.bindView(id: Int): ReadOnlyProperty<Any, T> = ViewBinding(id)
public fun <T : View> ViewHolder.bindView(id: Int): ReadOnlyProperty<Any, T> = ViewBinding(id)
public fun <T : View> ViewContainer.bindView(id: Int): ReadOnlyProperty<Any, T> = ViewBinding(id)

public fun <T : View> ViewGroup.bindOptionalView(id: Int): ReadOnlyProperty<Any, T?> = OptionalViewBinding(id)
public fun <T : View> Activity.bindOptionalView(id: Int): ReadOnlyProperty<Any, T?> = OptionalViewBinding(id)
public fun <T : View> Dialog.bindOptionalView(id: Int): ReadOnlyProperty<Any, T?> = OptionalViewBinding(id)
public fun <T : View> Fragment.bindOptionalView(id: Int): ReadOnlyProperty<Any, T?> = OptionalViewBinding(id)
public fun <T : View> SupportFragment.bindOptionalView(id: Int): ReadOnlyProperty<Any, T?> = OptionalViewBinding(id)
public fun <T : View> ViewHolder.bindOptionalView(id: Int): ReadOnlyProperty<Any, T?> = OptionalViewBinding(id)
public fun <T : View> ViewContainer.bindOptionalView(id: Int): ReadOnlyProperty<Any, T?> = OptionalViewBinding(id)

public fun <T : View> ViewGroup.bindViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = ViewListBinding(ids)
public fun <T : View> Activity.bindViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = ViewListBinding(ids)
public fun <T : View> Dialog.bindViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = ViewListBinding(ids)
public fun <T : View> Fragment.bindViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = ViewListBinding(ids)
public fun <T : View> SupportFragment.bindViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = ViewListBinding(ids)
public fun <T : View> ViewHolder.bindViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = ViewListBinding(ids)
public fun <T : View> ViewContainer.bindViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = ViewListBinding(ids)

public fun <T : View> ViewGroup.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = OptionalViewListBinding(ids)
public fun <T : View> Activity.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = OptionalViewListBinding(ids)
public fun <T : View> Dialog.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = OptionalViewListBinding(ids)
public fun <T : View> Fragment.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = OptionalViewListBinding(ids)
public fun <T : View> SupportFragment.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = OptionalViewListBinding(ids)
public fun <T : View> ViewHolder.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = OptionalViewListBinding(ids)
public fun <T : View> ViewContainer.bindOptionalViews(vararg ids: Int): ReadOnlyProperty<Any, List<T>> = OptionalViewListBinding(ids)

private fun findView<T : View>(thisRef: Any, id: Int): T? {
[suppress("UNCHECKED_CAST")]
Expand All @@ -46,6 +50,7 @@ private fun findView<T : View>(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?
}
Expand Down Expand Up @@ -97,3 +102,6 @@ private class Lazy<T> {
return value as T
}
}

public open class ViewContainer(val view: View) {
}