Today, I was assigned the task to implement the activity where admin can view the past reservations(both which are placed through Chatbot and Walk-Ins). I was supposed to show them in Tabbed activity which is nothing but View Pager which I implemented during Reservations Module. So, there was no difficulty in that.
Here’s the output:
The challenging part was that I had to show the Categories View Pager also, inside upper View Pager(Chatbot and Walk-Ins). I went through some sources in internet. I got some idea from them. I managed everything through fragments. Because of that I got more insight into Fragments, which had always been difficult for me to understand.
In the Reservation Activity, I created a view Pager with two tabs-
public class MyPagerAdapter extends FragmentStatePagerAdapter{
String[] titles = {"Chatbot", "WalkIns"};
SparseArray<Fragment> registeredFragments = new SparseArray<>();
public MyPagerAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment;
if(position == 0) {
fragment = FragmentResvTopViewPager.newInstance(listResvChatbot);
}
else {
fragment = FragmentResvTopViewPager.newInstance(listResvWalkIn);
}
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
public Fragment getRegisteredFragment(int position){
return registeredFragments.get(position);
}
}
Inside FragmentResvTopViewPager which is a subclass of Fragment, I created another View Pager Adapter-
public class ResvCategoriesAdapter extends FragmentStatePagerAdapter {
String[] titles;
SparseArray<Fragment> registeredFragments = new SparseArray<>();
public ResvCategoriesAdapter(FragmentManager fm){
super(fm);
titles = hashMapResv.keySet().toArray(new String[hashMapResv.size()]);
}
@Override
public Fragment getItem(int position) {
listResvCategory = wholeListResv.get(position);
Fragment fragment = CategoriesResvFragment.newInstance(listResvCategory);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public int getCount() {
return hashMapResv.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
public Fragment getRegisteredFragment(int position){
return registeredFragments.get(position);
}
}