0

Using a sample of SlidingTabsBasicFragment from Android Developer samples, I have 3 tabs which I want to show different content in. The current issue is that upon clicking or swiping to any of the 3 tabs, it is just loading the same default pager_item.xml, which displays "Page" and "Page Number"

I understand that I should make each tab load a different xml altogether. Could any kind soul please show me how to do this?

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

    }

}

}

I know that something has to be done here to achieve the outcome.

        @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;
    }
James Quah
  • 25
  • 3
  • check this thread [Adding Sliding Tabs using Support Library v7:21 with Toolbar in Android Developer Tools for existing project](http://stackoverflow.com/questions/26754948/adding-sliding-tabs-using-support-library-v721-with-toolbar-in-android-develope) – Xcihnegn Feb 08 '15 at 17:01

1 Answers1

0

I had the same problem than you, and I followed a tutorial for using fragments to each tab (really is a simple tutorial).

Take a look: Tabs tutorial

And if you want to get a Material Design style, check this library: Material Design Tabs

JavierSegoviaCordoba
  • 6,531
  • 9
  • 37
  • 51