I want to display the menu categories of restaurant in tabs. But the number of tabs will depend upon how many categories are there in a particular restaurant and the menu of that category will be displayed in ListView
at the corresponding page. Is this possible in android? And please get me some idea how to achieve it.

- 10,213
- 3
- 52
- 73

- 11
-
1are you asking if dynamic tabs are possible or not? and will you be changing the tabs once the tabs are already loaded? – himanshu1496 Dec 02 '15 at 07:18
-
check http://stackoverflow.com/questions/13664155/dynamically-add-and-remove-view-to-viewpager – Jaiprakash Soni Dec 02 '15 at 07:26
-
Yes I am asking about dynamic tabs. – AMBIKA GUPTA Dec 02 '15 at 07:32
-
yes it's possible, you have to initialize the adapter once your data is fetched. – himanshu1496 Dec 02 '15 at 08:30
-
Yes I am asking about dynamic tabs. Actually what I want is that, I am fetching menus of different restaurants from server. All the categories will be displayed in tab layout and the dishes in that category will be displayed in corresponding page. So thats why I dont know what will be the number of tabs. Please help me out. – AMBIKA GUPTA Dec 02 '15 at 09:30
-
that's absolutely fine because once you receive the list of categories, initialize the adapter, that's it. The code for a sample adapter is given below, you have any doubt please ask. – himanshu1496 Dec 02 '15 at 09:36
1 Answers
Yes you can. After you fetch your data (probably as JSON) and save it to SQLite db you can load that data and create dynamic tabs. Lets say you will need only one fragment that represents the category fragment for particular category. You can generalize that fragment to support and load data depending on your category ID or other parameters that you pass.
You have lets say HomeTabsFragment
that represents home fragment tabs. In that fragment when you fetch your categories you can define your TabsAdapter
where you have ArrayList<Category> categories
and assign that adapter to your viewPager mViewPager.setAdapter(mAdapter)
. For every category you create one tab with title and fragment that fetch data only for that tab like in adapter :
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<Category> categories;
public TabsPagerAdapter(FragmentManager fm, Context context,ArrayList<Category> categories) {
super(fm);
this.categories=categories;
}
@Override
public Fragment getItem(int position) {
return CategoryFragment.newInstance(categories.get(position),position);
}
@Override
public int getCount() {
return categories.size();
}
@Override
public CharSequence getPageTitle(int position){
return categories.get(position).getTitle();
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
if (observer != null) {
super.unregisterDataSetObserver(observer);
}
}
}
And CategoryFragment :
public class CategoryFragment extends Fragment {
public CategoryFragment(){
// required empty constructor
}
public static CategoryFragment newInstance(Category category,int position){
CategoryFragment fragment=new CategoryFragment();
Bundle args=new Bundle();
args.putParcelable("category", category);
args.putInt("position", position);
fragment.setArguments(args);
return fragment;
}
public Object getCategory(){
return getArguments().getParcelable("category");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
//Fetch data for categoryID
return view;
}
}

- 1,228
- 11
- 21