Is it possible to create tabs at runtime(dynamically) using tabhost (or normal tabs)?

Hi All,

I am developing an application for restaurants. Different restaurants have different menus. Category types for each restaurant comes from the DB. I want to show the categories to a user in the form of tabs.

My code goes like this:

// After getting the category list
// inside OnCreate method
if (!string.IsNullOrEmpty(jsonResult))
{
categoryList = ParseAndDisplay (jsonResult);

        }

        foreach (var category in categoryList) {
            AddTab (category.Name, new CategoryFragment ());
        }

}

// add tab method

void AddTab (string tabText,  Fragment view)
    {
        var tab = this.ActionBar.NewTab ();            
        tab.SetText (tabText);

        // must set event handler before adding tab
        tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
        {
            var fragment = this.FragmentManager.FindFragmentById(Resource.Id.fragmentContainer);
            if (fragment != null)
                e.FragmentTransaction.Remove(fragment);         
            e.FragmentTransaction.Add (Resource.Id.fragmentContainer, view);
        };
        //          tab.TabUnselected += delegate(object sender, ActionBar.TabEventArgs e) {
        //              e.FragmentTransaction.Remove(view);
        //          };

        this.ActionBar.AddTab (tab);
    }

****//** This is where I am totally lost **//****

class CategoryFragment: Fragment
    {       
        //ItemAdapterVeg ad=null;
        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView (inflater, container, savedInstanceState);
            //RetainInstance = true;
            var view = inflater.Inflate (Resource.Layout.Tab, container, false);
            //var sampleListView = view.FindViewById<ListView> (Resource.Id.sampleListView); 

            return view;
        }
    }

Any help would be much appreciated.

Sign In or Register to comment.