2016-04-04 20 views
1

Üç sekmem var. Bunlardan ikisi Fragment ve üçüncü sekme FragmentActivity uzanır. getItem() yöntemi nasıl uygulanır?Aynı yöntemle fragman ve fragmentactivity nasıl döndürülebilir?

public class UpdatesFragment extends FragmentActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.updates_layout); 

     if (savedInstanceState == null) { 
      addRssFragment(); 
     } 
    } 

    private void addRssFragment() { 
     FragmentManager manager = getSupportFragmentManager(); 
     FragmentTransaction transaction = manager.beginTransaction(); 
     RssFragment fragment = new RssFragment(); 
     transaction.add(R.id.fragment_container, fragment); 
     transaction.commit(); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putBoolean("fragment_added", true); 
    } 
} 

public class TabFragment extends Fragment { 

    public static TabLayout tabLayout; 
    public static ViewPager viewPager; 
    public static int int_items = 3; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     /** 
     *Inflate tab_layout and setup Views. 
     */ 
     View x = inflater.inflate(R.layout.tab_layout,null); 
     tabLayout = (TabLayout) x.findViewById(R.id.tabs); 
     viewPager = (ViewPager) x.findViewById(R.id.viewpager); 

     /** 
     *Set an Apater for the View Pager 
     */ 
     viewPager.setAdapter(new MyAdapter(getChildFragmentManager())); 

     /** 
     * Now , this is a workaround , 
     * The setupWithViewPager dose't works without the runnable . 
     * Maybe a Support Library Bug . 
     */ 

     tabLayout.post(new Runnable() { 
      @Override 
      public void run() { 
        tabLayout.setupWithViewPager(viewPager); 
      } 
     }); 

     return x; 
    } 
} 

class MyAdapter extends FragmentPagerAdapter{ 

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

    /** 
    * Return fragment with respect to Position . 
    */ 
    @Override 
    public Fragment getItem(int position) 
    { 
     switch (position){ 
      case 0 : return new PrimaryFragment(); 
      case 1 : return new SocialFragment(); 
      case 2: return new UpdatesFragment(); 
     } 

    return null; 
    } 

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

    /** 
    * This method returns the title of the tab according to the position. 
    */ 

    @Override 
    public CharSequence getPageTitle(int position) { 

     switch (position){ 
      case 0 : 
       return "Primary"; 
      case 1 : 
       return "Social"; 
      case 2 : 
       return "Updates"; 
     } 
      return null; 
    } 
} 

cevap

0

Fragment ve FragmentActivity hem farklı şeyler vardır. FragmentActivity'un içine Fragment'u ekleyebiliriz. Ancak FragmentActivity'u Fragment olarak kullanamazsınız. ViewPagerUpdatesFragment kullanmak istiyorsanız, o zaman Fragment'dan genişletin.