繁体   English   中英

如何编写以片段类作为参数的方法,使我可以创建该片段的新实例?

[英]How can I write a method with a fragment class as an argument that will allow me to create a new instance of that fragment?

目标

我正在尝试编写一种方法,该方法将替换用于交换片段的代码,以使复制和发布保持最少(并保持DRY)

问题

当我尝试使用传入的类作为参数创建该类的新实例时遇到错误。

错误发生在此代码行的运算符左侧(等号):

 newFragmentClass new_fragment = newFragmentClass.newInstance(); 

它给我的错误是:“ newFragmentClass无法解析为类型”。

完整代码
  private void changeFragment(Class<?> newFragmentClass) { // Detect the current fragment Fragment current_fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); // Create a new instance of the given class, edit later to gracefully handle errors newFragmentClass new_fragment = newFragmentClass.newInstance(); // Switch to the new fragment FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.detach(current_fragment); transaction.replace(R.id.fragment_container, new_fragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.commit(); // Change the tab background to indicate that the tab is active, reset backgrounds for any other tabs findViewById(R.id.page_one_tab).setBackgroundResource(R.drawable.menu_button_background_active); findViewById(R.id.page_two_tab).setBackgroundResource(R.drawable.menu_button_background); findViewById(R.id.page_three_tab).setBackgroundResource(R.drawable.menu_button_background); } // Page One Tab Button Functionality public void pageOneTab (View v) { // Change the fragment to SelectPlayersFragment changeFragment(Page_One_Fragment.class); } 
尝试的解决方案

我已经搜索StackOverflow和整个互联网已有相当长的一段时间了,但一直没有找到解决方案。 像这样的一些主题似乎似乎可以解决问题,但是随后我在transaction.replace行上遇到了错误,找不到解决方法:“ FragmentTransaction类型的方法replace(int,Fragment)不适用于参数(int,Object)”。

您可能想要类似的东西:

private Fragment changeFragment(Class<? extends Fragment> newFragmentClass)  {
    Fragment current_fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
    // Create a new instance of the given class, edit later to gracefully handle errors
    Fragment new_fragment = null;
    try {
         new_fragment = newFragmentClass.newInstance();
    } catch (InstantiationException e) {            
        throw new RuntimeException(e); // for some reason this fragment loading has failed so crash
    } catch (IllegalAccessException e) {            
        throw new RuntimeException(e);
    } 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.detach(current_fragment);
    transaction.replace(R.id.fragment_container, new_fragment);
    // ...   

也许更简单的解决方案是传递一个Fragment作为参数,而不是Class ,并使用它来替换当前片段。 另外,您不需要分离当前片段,这就是replace()为您所做的。

像这样:

public void changeFragment(Fragment fragment) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.fragment_container, fragment, "tag");
    transaction.commit();
    //.....
}

您可以像这样使用它:

changeFragment(new Page_One_Fragment());

我想你想要这个:

private <T extends Fragment> void changeFragment(Class<T> newFragmentClass)
{
    ...
    // Create a new instance of the given class, edit later to gracefully handle errors
    T new_fragment = newFragmentClass.newInstance(); 
    ...
}

因为“ newFragmentClass”只是方法的参数,所以它不是类型,因此无法实例化。请按照我的代码解决您的问题

private <T extends Fragment> void changeFragment(Class<T> newFragmentClass)
    {
        // Detect the current fragment
            Fragment current_fragment = getSupportFragmentManager().findFragmentById(R.id.content);

        // Create a new instance of the given class, edit later to gracefully handle errors
            T new_fragment = newFragmentClass.newInstance(); 

        // Switch to the player select fragment
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.detach(current_fragment);
            transaction.replace(R.id.fragment_container, new_fragment);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            transaction.commit();

        // Change the tab background to indicate that the tab is active, reset backgrounds for any other tabs
            findViewById(R.id.page_one_tab).setBackgroundResource(R.drawable.menu_button_background_active);
            findViewById(R.id.page_two_tab).setBackgroundResource(R.drawable.menu_button_background);
            findViewById(R.id.page_three_tab).setBackgroundResource(R.drawable.menu_button_background);

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM