什么是使用参数(FragmentManager fm)和Bundle args = new Bundle();?

时间:2016-09-30 17:12:33

标签: android android-fragments material-design android-tabs

Android新手在这里尝试使用Android开发者网站的标签来学习轻扫视图。但我无法弄清楚这里使用的某些代码是什么,因为我正在尝试理解代码的每个部分,请帮忙。

Creating Swipe Views with Tabs

// Since this is an object collection, use a FragmentStatePagerAdapter,
// and NOT a FragmentPagerAdapter.
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
    public DemoCollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DemoObjectFragment();
        Bundle args = new Bundle();
        // Our object is just an integer :-P
        args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 100;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
}

我无法理解课程中 FragmentManager fm super(fm)的论点的目的。构造

此外,不能使用此代码

    Bundle args = new Bundle();
    // Our object is just an integer :-P
    args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
    fragment.setArguments(args);

请帮助我理解标签活动或为我提供理解这个和其他Android概念的良好来源。

2 个答案:

答案 0 :(得分:0)

片段必须具有空构造函数。因此,它们不能像通常用于对象一样传入参数。因此,他们使用get/setArguments()方法(相当麻烦和痛苦)的方式。通常,您可以使用Framgnent执行类似的操作:

public class MyFragment extends Fragment {

   public static MyFragment newInstance(int parameter1, String parameter2) {
      Bundle args = new Bundle();
      args.putInt("parameter1", parameter1);
      args.putString("parameter2", parameter2);

      MyFragment newFragment = new MyFragment();
      newFragment.setArguments(args);
      return newFragment;
   }

   private int parameter1;
   private String parameter2;

   @Overide
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        parameter1 = args.getInt("parameter1");
        parameter2 = args.getString("parameter2");
   }
}

然后,当您想要创建片段而不是通过创建新对象来实例化片段时,可以调用MyFragment frag = MyFragment.newInstance(1, "parameter");。这有助于确保MyFragment处于正常状态。

你所选择的例子选择不使用这种技术,但实际上,在创建片段时要记住哪些参数是必需真的很痛苦。 newInstance方法有助于告诉您正常运行所需的内容。

编辑:

原因是:

public DemoCollectionPagerAdapter(FragmentManager fm) {
   super(fm);
}

您正在延长FragmentPagerAdapter。此适配器为您执行了大量的片段添加和删除,因此需要FragmentManager。在Java中,如果类没有空的默认构造函数,则所有子类都必须创建一个构造函数,该构造函数调用super()构造函数来完全构造父类。

答案 1 :(得分:0)

//the constructor of your adapter - the fragment manager is needed to    
//inflate the fragment when you swipe to another tab
public DemoCollectionPagerAdapter(FragmentManager fm) {
    //with super you are calling the constructor of the base class
    //you are extending your class from (FragmentStatePageAdapter)
    //and pass the fragmentmanager to the super constructor
    super(fm);
}


//this method returns the fragment for a certain position
//it is needed to tell the adapter which fragment to return
@Override
public Fragment getItem(int i) {
    //you are creating the fragment and passing the needed
    //parameters here
    //you could do it like this but I would create a static
    //method newInstance(...) in the fragment and use this
    //you can read more about this and the reason for it here: http://www.androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html
    Fragment fragment = new DemoObjectFragment();
    Bundle args = new Bundle();
    // Our object is just an integer :-P
    args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
    fragment.setArguments(args);
    return fragment;
}


//needed to tell the adapter how many fragment it contains
@Override
public int getCount() {
    return 100;
}

@Override
public CharSequence getPageTitle(int position) {
    return "OBJECT " + (position + 1);
}

}