Skip to content

Instantly share code, notes, and snippets.

@xylophone21
Created December 18, 2014 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xylophone21/e8ba54145bc4817aef44 to your computer and use it in GitHub Desktop.
Save xylophone21/e8ba54145bc4817aef44 to your computer and use it in GitHub Desktop.
public class AndroidLayoutPagerInflater implements IPagerInflater {
public static final String TAG = AndroidLayoutPagerInflater.class
.getSimpleName();
private String mBaseResPath;
private Context mContext;
private String mViewPackage;
public AndroidLayoutPagerInflater(Context context, String baseResPath,
String viewPackage) {
mContext = context;
mBaseResPath = baseResPath;
mViewPackage = viewPackage + ".";
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private View onCreateView(View parent, String name, AttributeSet attrs) {
try {
Class clazz = Class.forName(mViewPackage + name);
Constructor constructor = clazz.getConstructor(Context.class,
AttributeSet.class);
Object v = constructor.newInstance(mContext, attrs);
if (v instanceof View) {
if (parent != null && parent instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) parent;
LayoutParams lp = vg.generateLayoutParams(attrs);
if(lp != null) {
vg.addView((View) v, lp);
}
else {
vg.addView((View)v);
}
}
return (View) v;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public boolean identify(String src) {
if (src.endsWith(".xml")) {
File file = new File(mBaseResPath + src);
if (file.exists()) {
return true;
}
}
return false;
}
@Override
public View inflate(String src) {
InputStream input = null;
try {
File file = new File(mBaseResPath + src);
input = new FileInputStream(file);
XmlPullParser xpp = Xml.newPullParser();
xpp.setInput(input, "utf-8");
Stack<View> viewStack = new Stack<View>();
List<View> retViews = new ArrayList<View>();
int eventCode = xpp.getEventType();
while (eventCode != XmlPullParser.END_DOCUMENT) {
switch (eventCode) {
case XmlPullParser.START_TAG:
String name = xpp.getName();
Log.d(TAG,
"Found TAG name=" + name + " cnt="
+ xpp.getAttributeCount());
AttributeSet attrs = Xml.asAttributeSet(xpp);
View parent = null;
if (!viewStack.isEmpty()) {
parent = viewStack.peek();
if (parent != null && !(parent instanceof ViewGroup)) {
Log.w(TAG,
"Parent "
+ parent.toString()
+ " is not a ViewGroup, ignore its sub views");
continue;
}
}
View v = onCreateView(parent, name, attrs);
viewStack.push(v);
if (v == null) {
Log.w(TAG, "CreateView [" + name + "] failed");
}
break;
case XmlPullParser.END_TAG:
String name1 = xpp.getName();
Log.d(TAG, "END_TAG TAG name=" + name1);
View v1 = viewStack.pop();
if (viewStack.isEmpty() && v1 != null) {
Log.d(TAG, "Found root view:" + name1);
retViews.add(v1);
}
break;
default:
break;
}
eventCode = xpp.next();
}
int retCnt = retViews.size();
if (retCnt == 1) {
Log.d(TAG, "One view was created, return it.");
return retViews.get(0);
} else if (retCnt > 1) {
Log.d(TAG,
retCnt
+ "views were created, will new a FrameLayout return it.");
FrameLayout root = new FrameLayout(mContext);
for (View v : retViews) {
root.addView(v);
}
return root;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment