2016-09-30 29 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); 
} 

}