DrawableXmlParser.java 文件源码

java
阅读 44 收藏 0 点赞 0 评论 0

项目:polar-dashboard 作者:
public static List<Category> parse(@NonNull Context context, @XmlRes int xmlRes) {
  mCategories = new ArrayList<>();
  XmlResourceParser parser = null;
  try {
    parser = context.getResources().getXml(xmlRes);
    int eventType = parser.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
      switch (eventType) {
        case XmlPullParser.START_TAG:
          final String tagName = parser.getName();
          if (tagName.equalsIgnoreCase("category")) {
            mCurrentCategory = new Category(parser.getAttributeValue(null, "title"));
            mCategories.add(mCurrentCategory);
          } else if (tagName.equalsIgnoreCase("item")) {
            if (mCurrentCategory == null) {
              mCurrentCategory = new Category(context.getString(R.string.default_category));
              mCategories.add(mCurrentCategory);
            }
            mCurrentCategory.addItem(
                new Icon(parser.getAttributeValue(null, "drawable"), mCurrentCategory));
          }
          break;
      }
      eventType = parser.next();
    }

  } catch (XmlPullParserException | IOException e) {
    e.printStackTrace();
  } finally {
    if (parser != null) {
      parser.close();
    }
  }

  mCurrentCategory = null;
  return mCategories;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号