library / org.akop.kotx.tools.view / ViewParser

ViewParser

object ViewParser

Parses a view hierarchy and maps out references to specific views by reading annotations.

For instance, in an activity, declare a member variable as follows:

@ViewParser.Leaf(layoutId = R.id.code)
private val codeField: EditText? = null

and have ViewParser parse the hierarchy after Activity.setContentView:

setContentView(R.layout.activity_main)
parseViewTree()

When Leaf.layoutId is omitted, ViewParser will look for a resource id that matches the variable's name, automatically converting camelCase member variable names to underscore-delimited resources id's (e.g. codeField => code_field).

Using annotations with ViewParser is more efficient than using View.findViewById alone, because view tree traversal is done only once.

Annotations

Leaf

annotation class Leaf

Functions

parse

fun parse(activity: Activity): Unit

Traverses view hierarchy of an activity, then for each member variable of the activity annotated with Leaf, assigns a reference to the corresponding View. Should be called after Activity.setContentView.

fun parse(activity: Activity, obj: Any): Unit

Traverses view hierarchy of an activity, then for each member variable of obj annotated with Leaf, assigns a reference to the corresponding View. Should be called after Activity.setContentView.

fun parse(view: View, obj: Any): View

Traverses hierarchy of view, then for each member variable of obj annotated with Leaf, assigns a reference to the corresponding View.