示例#1
0
    private FrameLayout constructFragment(Context context)
        throws InstantiationException, IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {
      TestAttributeSet attributeSet =
          new TestAttributeSet(
              attributes, resourceExtractor, attrResourceLoader, View.class, isSystem);
      if (strictI18n) {
        attributeSet.validateStrictI18n();
      }

      Class<? extends Fragment> clazz = loadFragmentClass(attributes.get("android:name"));
      Fragment fragment = ((Constructor<? extends Fragment>) clazz.getConstructor()).newInstance();
      if (!(context instanceof FragmentActivity)) {
        throw new RuntimeException(
            "Cannot inflate a fragment unless the activity is a FragmentActivity");
      }

      FragmentActivity activity = (FragmentActivity) context;

      String tag = attributeSet.getAttributeValue("android", "tag");
      int id = attributeSet.getAttributeResourceValue("android", "id", 0);
      // TODO: this should probably be changed to call TestFragmentManager.addFragment so that the
      // inflated fragments don't get started twice (once in the commit, and once in
      // ShadowFragmentActivity's
      // onStart()
      activity.getSupportFragmentManager().beginTransaction().add(id, fragment, tag).commit();

      View view = fragment.getView();

      FrameLayout container = new FrameLayout(context);
      container.setId(id);
      container.addView(view);
      return container;
    }
示例#2
0
 private View constructView(Context context)
     throws InstantiationException, IllegalAccessException, InvocationTargetException,
         NoSuchMethodException {
   Class<? extends View> clazz = pickViewClass();
   try {
     TestAttributeSet attributeSet =
         new TestAttributeSet(
             attributes, resourceExtractor, attrResourceLoader, clazz, isSystem);
     if (strictI18n) {
       attributeSet.validateStrictI18n();
     }
     return ((Constructor<? extends View>)
             clazz.getConstructor(Context.class, AttributeSet.class))
         .newInstance(context, attributeSet);
   } catch (NoSuchMethodException e) {
     try {
       return ((Constructor<? extends View>) clazz.getConstructor(Context.class))
           .newInstance(context);
     } catch (NoSuchMethodException e1) {
       return ((Constructor<? extends View>) clazz.getConstructor(Context.class, String.class))
           .newInstance(context, "");
     }
   }
 }