2015-02-08 42 views
0

使用Android Developer示例中的SlidingTabsBasicFragment示例,我有3個選項卡,我想要顯示不同的內容。當前問題是,在單擊或滑動到3個選項卡中的任何選項卡,它只是加載相同的默認pager_item.xml,其中顯示「頁面」和「頁碼」獲取SlidingTabsBasicFragment的每個選項卡以顯示不同的內容

據我所知,我應該使每個標籤加載一個不同的XML完全。請問任何一種靈魂,請告訴我如何做到這一點?

class SamplePagerAdapter extends PagerAdapter { 

    /** 
    * Sets the Titles for the various tabs 
    */ 

    private String[] titles={"Browse", "Activity", "Me"}; 
    /** 
    * @return the number of pages to display 
    */ 
    @Override 
    public int getCount() { 
     return 3; 
    } 

    /** 
    * @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the 
    * same object as the {@link View} added to the {@link ViewPager}. 
    */ 
    @Override 
    public boolean isViewFromObject(View view, Object o) { 
     return o == view; 
    } 

    // BEGIN_INCLUDE (pageradapter_getpagetitle) 
    /** 
    * Return the title of the item at {@code position}. This is important as what this method 
    * returns is what is displayed in the {@link SlidingTabLayout}. 
    * <p> 
    * Here we construct one using the position value, but for real application the title should 
    * refer to the item's contents. 
    */ 
    @Override 
    public CharSequence getPageTitle(int position) { 

     return titles[position]; 

    } 


    // END_INCLUDE (pageradapter_getpagetitle) 

    /** 
    * Instantiate the {@link View} which should be displayed at {@code position}. Here we 
    * inflate a layout from the apps resources and then change the text view to signify the position. 
    */ 
    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     // Inflate a new layout from our resources 

     View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_main_menu, 
       container, false); 

     // Add the newly created View to the ViewPager 
     container.addView(view); 

     // Retrieve a TextView from the inflated View, and update it's text 
     // TextView title = (TextView) view.findViewById(R.id.item_title); 
     // title.setText(String.valueOf(position + 1)); 

     // Return the View 
     return view; 
    } 

    /** 
    * Destroy the item from the {@link ViewPager}. In our case this is simply removing the 
    * {@link View}. 
    */ 
    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     container.removeView((View) object); 

    } 

} 

}

我知道,有些事情要在這裏完成,實現的結果。

 @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     // Inflate a new layout from our resources 

     View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item, 
       container, false); 

     // Add the newly created View to the ViewPager 
     container.addView(view); 

     // Retrieve a TextView from the inflated View, and update it's text 
     TextView title = (TextView) view.findViewById(R.id.item_title); 
     title.setText(String.valueOf(position + 1)); 

     // Return the View 
     return view; 
    } 
+0

檢查這個線程[添加使用支持庫V7滑動標籤:用工具欄在Android開發者21現有項目工具](http://stackoverflow.com/questions/26754948/adding-sliding-tabs-using-support-library-v721-with-toolbar-in-android-develope) – Xcihnegn 2015-02-08 17:01:36

回答

0

我有同樣的問題比你,我跟着教程使用片段到每個選項卡(真的是一個簡單的教程)。

請看下圖:Tabs tutorial

如果你想獲得材料的設計風格,看看這個庫:Material Design Tabs

相關問題