Question

I worked with Fragment before, so i have 5 fragments that corresponds with 5 tabs in ViewPagerIndicator, but how can i populate each tab? Here's an example of a Fragment i have :

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        viewta = inflater.inflate(R.layout.agridviewxml, container, false);
}

And here's how my ViewPagerIndicator Works:

private static final String[] CONTENT = new String[] { "Samsung", "HTC",
            "LG", "Sony", "Search" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_tabs);

        FragmentPagerAdapter adapter = new GoogleMusicAdapter(
                getSupportFragmentManager());

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(pager);
    }

    class GoogleMusicAdapter extends FragmentPagerAdapter {
        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        @Override
        public int getCount() {
            return CONTENT.length;
        }
    }
}

Any idea or suggestions?? THANKS ALOT

Each tab is empty, i want to fill it (populate it) with data: enter image description here

Was it helpful?

Solution

You need write a TestFragment that shows your desired UI.

For example, to have "A Fragment that contains listview with a custom adapter", you can have TestFragment extend ListFragment, and populate the adapter in a method like onActivityCreated().

OTHER TIPS

Do something like the following in order to connect tabs with Fragments:

mTabsAdapter = new TabsAdapter(this, mViewPager);

    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.monday)),
            Monday.class, packBundle);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.tuesday)),
            Tuesday.class, packBundle);
    mTabsAdapter.addTab(
            bar.newTab().setText(getString(R.string.wednesday)),
            Wednesday.class, packBundle);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.thursday)),
            Thursday.class, packBundle);
    mTabsAdapter.addTab(bar.newTab().setText(getString(R.string.friday)),
            Friday.class, packBundle);

Where bar is my actionbar and the Monday.class etc are my Fragments. the packBundle at the end are extras that i send to every Fragment for personal use! My TabsAdapter is your own GoogleMusicAdapter! Change it accordingly!

AddTab method:

public void addTab(ActionBar.Tab tab,Class<?> clss,Bundle args)

You need two classes in your project.

  1. TabsAdapter which will extend FragmentPagerAdapter implements OnPageChangeListener, TabListener
  2. A simple class that will extend Fragment. Basically this will contain which fragment to load.

In TabsAdapter class you need to pass in all the data like what all classes you need to load and title of tab like

public TabsAdapter(SherlockFragmentActivity fa, ViewPager vPager) {
    // TODO Auto-generated constructor stub
    super(fa.getSupportFragmentManager());
    context=fa;
    ab=fa.getSupportActionBar();
    mvPager=vPager;
    mvPager.setAdapter(this);
    mvPager.setOnPageChangeListener(this);


}

add another method to your TabsAdapter class which will add tabs to your ViewPager

 public void addTab(ActionBar.Tab tab,Class<?> clss,Bundle args){
     TabInfo info=new TabInfo(clss, args);
    tab.setTag(info);
    tab.setTabListener(this);
    mtabs.add(info);
    ab.addTab(tab);
    notifyDataSetChanged();
}

and from the activity pass values like :

    adap=new TabsAdapter(this,vPager);
    adap.addTab(ab.newTab().setText("title of tab"), Fragment_1.class,null);
    vPager.setAdapter(adap);

Now customize your tabsadapter class accordingly and use the overriden methods as per your needs. I hope this small hint is helpful to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top