T
- Type of the items to inflatepublic abstract class GenericInflater<T> extends Object
android.preference.GenericInflater
,
which is in turn modeled after LayoutInflater
. This can be used to recursively inflate a
hierarchy of items. All items in the hierarchy must inherit the generic type T
, and the
specific implementation is expected to handle inserting child items into the parent, by
implementing onAddChildItem(Object, Object)
.Modifier and Type | Class and Description |
---|---|
static interface |
GenericInflater.Factory<T> |
Modifier | Constructor and Description |
---|---|
protected |
GenericInflater(Context context)
Create a new inflater instance associated with a
particular Context.
|
protected |
GenericInflater(GenericInflater<T> original,
Context newContext)
Create a new inflater instance that is a copy of an
existing inflater, optionally with its Context
changed.
|
Modifier and Type | Method and Description |
---|---|
abstract GenericInflater |
cloneInContext(Context newContext)
Create a copy of the existing inflater object, with the copy
pointing to a different Context than the original.
|
T |
createItem(String name,
String prefix,
AttributeSet attrs)
Low-level function for instantiating by name.
|
Context |
getContext()
Return the context we are running in, for access to resources, class
loader, etc.
|
String |
getDefaultPackage()
Returns the default package, or null if it is not set.
|
GenericInflater.Factory<T> |
getFactory()
Return the current factory (or null).
|
T |
inflate(int resource) |
T |
inflate(int resource,
T root)
Inflate a new item hierarchy from the specified xml resource.
|
T |
inflate(int resource,
T root,
boolean attachToRoot)
Inflate a new hierarchy from the specified xml resource.
|
T |
inflate(XmlPullParser parser,
T root)
Inflate a new hierarchy from the specified xml node.
|
T |
inflate(XmlPullParser parser,
T root,
boolean attachToRoot)
Inflate a new hierarchy from the specified XML node.
|
protected abstract void |
onAddChildItem(T parent,
T child) |
protected boolean |
onCreateCustomFromTag(XmlPullParser parser,
T node,
AttributeSet attrs)
Before this inflater tries to create an item from the tag, this method
will be called.
|
protected T |
onCreateItem(String name,
AttributeSet attrs)
This routine is responsible for creating the correct subclass of item
given the xml element name.
|
protected T |
onMergeRoots(T givenRoot,
boolean attachToGivenRoot,
T xmlRoot) |
void |
setDefaultPackage(String defaultPackage)
Sets the default package that will be searched for classes to construct
for tag names that have no explicit package.
|
void |
setFactory(GenericInflater.Factory<T> factory)
Attach a custom Factory interface for creating items while using this
inflater.
|
protected final Context mContext
protected GenericInflater(Context context)
context
- The Context in which this inflater will
create its items; most importantly, this supplies the theme
from which the default values for their attributes are
retrieved.protected GenericInflater(GenericInflater<T> original, Context newContext)
cloneInContext(android.content.Context)
.original
- The original inflater to copy.newContext
- The new Context to use.public abstract GenericInflater cloneInContext(Context newContext)
ContextThemeWrapper
to create a new inflater to go along
with the new Context theme.newContext
- The new Context to associate with the new inflater.
May be the same as the original Context if desired.public void setDefaultPackage(String defaultPackage)
defaultPackage
- The default package. This will be prepended to the
tag name, so it should end with a period.public String getDefaultPackage()
setDefaultPackage(String)
public Context getContext()
public final GenericInflater.Factory<T> getFactory()
public void setFactory(GenericInflater.Factory<T> factory)
onCreateItem(java.lang.String, android.util.AttributeSet)
method is called.
If you have an existing inflater and want to add your
own factory to it, use cloneInContext(android.content.Context)
to clone the existing
instance and then you can use this function (once) on the returned new
instance. This will merge your own factory with whatever factory the
original instance is using.
public T inflate(int resource)
public T inflate(int resource, T root)
resource
- ID for an XML resource to load (e.g.,
R.layout.main_page
)root
- Optional parent of the generated hierarchy.public T inflate(XmlPullParser parser, T root)
Important For performance reasons, inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use inflater with an XmlPullParser over a plain XML file at runtime.
parser
- XML dom node containing the description of the
hierarchy.root
- Optional parent of the generated hierarchy.public T inflate(int resource, T root, boolean attachToRoot)
resource
- ID for an XML resource to load (e.g.,
R.layout.main_page
)root
- Optional root to be the parent of the generated hierarchy (if
attachToRoot is true), or else simply an object that
provides a set of values for root of the returned
hierarchy (if attachToRoot is false.)attachToRoot
- Whether the inflated hierarchy should be attached to
the root parameter?public T inflate(XmlPullParser parser, T root, boolean attachToRoot)
Important For performance reasons, inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use inflater with an XmlPullParser over a plain XML file at runtime.
parser
- XML dom node containing the description of the
hierarchy.root
- Optional to be the parent of the generated hierarchy (if
attachToRoot is true), or else simply an object that
provides a set of values for root of the returned
hierarchy (if attachToRoot is false.)attachToRoot
- Whether the inflated hierarchy should be attached to
the root parameter?public final T createItem(String name, String prefix, AttributeSet attrs) throws ClassNotFoundException, InflateException
There are two things that can happen in an error case: either the exception describing the error will be thrown, or a null will be returned. You must deal with both possibilities -- the former will happen the first time createItem() is called for a class of a particular name, the latter every time there-after for that class name.
name
- The full name of the class to be instantiated.attrs
- The XML attributes supplied for this instance.ClassNotFoundException
InflateException
protected T onCreateItem(String name, AttributeSet attrs) throws ClassNotFoundException
name
- The fully qualified class name of the item to be create.attrs
- An AttributeSet of attributes to apply to the item.ClassNotFoundException
protected boolean onCreateCustomFromTag(XmlPullParser parser, T node, AttributeSet attrs) throws XmlPullParserException
parser
- XML dom node containing the description of the hierarchy.node
- The item that should be the parent of whatever you create.attrs
- An AttributeSet of attributes to apply to the item.XmlPullParserException