5

I was wondering if it was possible to launch a fragment by variable name rather then hard coding the fragments name.

Allow me to post a sample

This is how you traditionally launch a fragment:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.your_placehodler, new YourFragment());
ft.commit();

But say you are trying to launch the fragment without knowing the name of it, or possibly which fragment it is. Say like a listFragment, or a Listview and you are running through an array of Fragment names. Hence you would do something like this:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

     private String[] values = new String[] { "frag1", "frag2", "frag3" };

     String someFragment = values[position];
     String fragName = (someFragment + ".class");

     try {
     FragmentTransaction ft = getFragmentManager().beginTransaction();
     ft.replace(R.id.your_placehodler, new fragName());
     ft.commit();
     } catch (Exception e) {
     //print message
     }

I know this is not correct, but I feel like if it's possible I may be close. I searched for a while but I found nothing.

So my question, Is this possible? If so how would I implement it? Thanks!

Edit I attempted what I thought may work with the Reflections API using this code

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    String questions = values[position];

    try {
        Fragment frags = (Fragment) Class.forName("com.example.android." + questions).newInstance();            
        getFragmentManager()
                .beginTransaction()
                .setCustomAnimations(android.R.animator.fade_in,
                        android.R.animator.fade_out)
                .replace(R.id.header_fragment_container, frags).commit();

    }

    catch (Exception e) {

    }

}
}

I get a message saying 05-08 04:38:14.124: W/dalvikvm(812): dvmFindClassByName rejecting 'com.android.example.Ovens'

Yet if in my code I change the line to say Fragment frags = (Fragment) Class.forName("com.android.example." + "Ovens").newInstance();

It works

The variable "questions" is an exact copy of the class name. I don't see why it wouldn't work. Nothing happens, nothing prints to the logcat

Final Edit

Got it! I was missing the "" marker. Here is the final working code, thanks for all the help

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    String questions = values[position];
    try {
        Fragment frags = (Fragment) Class.forName("com.android.example." + "" + questions).newInstance();
        getFragmentManager()
                .beginTransaction()
                .setCustomAnimations(android.R.animator.fade_in,
                        android.R.animator.fade_out)
                .replace(R.id.header_fragment_container, frags).commit();
    }

    catch (Exception e) {

    }
}
}
4

2 回答 2

8

回答可能为时已晚,但为其他人放在这里。而不是使用

 Fragment frags = (Fragment) Class.forName("com.android.example." + "" + questions).newInstance();

利用

Fragment frags = Fragment.instantiate(mContext,"com.android.example." + "" + questions);

如果你想将参数传递给你可以使用的片段

Fragment frags = Fragment.instantiate(mContext,"com.android.example." + "" + questions,bundle);

其中 bundle 是携带您的数据的 Bundle。

编辑:

至于为什么你应该更喜欢这个而不是其他:

  1. 您可以将 Bundle args 发送给您的片段
  2. 使用 sClassMap 进行缓存
  3. 完整性检查初始化的类是否为片段

这是片段中实例化方法的代码

public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) {
        try {
            Class<?> clazz = sClassMap.get(fname);
            if (clazz == null) {
                // Class not found in the cache, see if it's real, and try to add it
                clazz = context.getClassLoader().loadClass(fname);
                if (!Fragment.class.isAssignableFrom(clazz)) {
                    throw new InstantiationException("Trying to instantiate a class " + fname
                            + " that is not a Fragment", new ClassCastException());
                }
                sClassMap.put(fname, clazz);
            }
            Fragment f = (Fragment)clazz.newInstance();
            if (args != null) {
                args.setClassLoader(f.getClass().getClassLoader());
                f.mArguments = args;
            }
            return f;
        } catch (ClassNotFoundException e) {
            throw new InstantiationException("Unable to instantiate fragment " + fname
                    + ": make sure class name exists, is public, and has an"
                    + " empty constructor that is public", e);
        } catch (java.lang.InstantiationException e) {
            throw new InstantiationException("Unable to instantiate fragment " + fname
                    + ": make sure class name exists, is public, and has an"
                    + " empty constructor that is public", e);
        } catch (IllegalAccessException e) {
            throw new InstantiationException("Unable to instantiate fragment " + fname
                    + ": make sure class name exists, is public, and has an"
                    + " empty constructor that is public", e);
        }
    }

其中 sClassMap 是一个 HashMap

private static final HashMap<String, Class<?>> sClassMap =
        new HashMap<String, Class<?>>();

显然您可以在上面的代码中实现相同的功能。不这样做的理由“干”

于 2014-01-03T09:14:23.893 回答
6

实现这一目标的一种方法是通过反射 API。

Class.forName("com.example.MyFragment").newInstance();

这可能会引发大量异常,因此请注意这一点。

另一种方法是为您的片段创建一个简单的工厂类。

public abstract class MyFragmentFactory {
    private MyFragmentFactory(){}

    public static <T extends Fragment> T getFragment(String name){
        if("MyFragment".equals(name)){
            return new MyFragment();
        }else if("whatever".equals(name)){
            // ...
        }else{
            throw new RuntimeException("unknown fragment "+ name);
        }
    }
}
于 2013-05-08T01:05:17.727 回答