2015-02-08 3 views
0

Используя образец SlidingTabsBasicFragment из образцов для разработчиков Android, у меня есть 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

проверить эту нить [Добавление Sliding Tabs с помощью поддержки Библиотека v7: 21 с панели инструментов в Android Developer Инструменты для существующего проекта] (http://stackoverflow.com/questions/26754948/adding-sliding-tabs-using-support-library-v721-with-toolbar-in-android-develope) – Xcihnegn

ответ

0

У меня была та же проблема, чем вы, и я последовал за учебник для использования фрагментов для каждой вкладки (на самом деле это простой учебник).

Посмотрите: Tabs tutorial

И если вы хотите, чтобы получить материал стиль дизайна, проверить эту библиотеку: Material Design Tabs