2016-09-30 28 views
-1

一位Android新手在这里试图学习Android开发人员网站上的标签刷卡视图。但我无法弄清楚这里使用的某些代码的用法,因为我正在试着了解代码的每个部分,请帮助。使用参数(FragmentManager fm)和Bundle args = new Bundle();?

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超(FM)的目的在类的构造函数。

而且,不能使用该代码的下

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

请帮助我理解选项卡活动或提供给我一个很好的来源,了解这一点,其他Android概念。

回答

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中,如果一个类没有一个空的默认构造函数,那么所有的子类都必须创建一个调用构造函数的构造函数来构造父类。

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); 
} 

}