I am making dynamic tabs with dynamic list view within each tab fragment. The problem is when I am switching between two consecutive tabs the xml of previous tab is displayed but when I click non-consecutive tabs it's working fine. Don't know where the problem is so I am attaching my fragment class and HomeActivity:
public class HomeActivity2 extends AppCompatActivity {
public ViewPagerAdapter adapter;
private Toolbar toolbar;
private TabLayout tabLayout;
private NonSwipeableViewPager viewPager;
public ArrayList<String> mappingOfCategoryWithIndex = new ArrayList<>();
public ArrayList<MenuItem> allMenuItemsList = new ArrayList<MenuItem>();
ArrayList<MenuItemCategory> allMenuItemCategoryList = new ArrayList<MenuItemCategory>();
//Getters
public NonSwipeableViewPager getViewPager() {
return viewPager;
}
public ArrayList<String> getMappingOfCategoryWithIndex() {
return mappingOfCategoryWithIndex;
}
public ArrayList<MenuItem> getAllMenuItemsList() {
return allMenuItemsList;
}
public TabLayout getTabLayout() {
return tabLayout;
}
public ArrayList<MenuItemCategory> getAllMenuItemCategoryList() {return allMenuItemCategoryList;}
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home2);
findViewById(R.id.tabs).bringToFront();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Getting All Menu Items
GetAllMenuItems menuItemsReader = null;
try {
menuItemsReader = new GetAllMenuItems();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
allMenuItemsList = menuItemsReader.getMenuItemsList();
// Getting All Categories
GetAllMenuItemCategories menuItemsCategoriesReader = null;
try {
menuItemsCategoriesReader = new GetAllMenuItemCategories();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
allMenuItemCategoryList = menuItemsCategoriesReader.getCategoryList();
// toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (NonSwipeableViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//THIS!!
if (viewPager != null) {
viewPager.setCurrentItem(tab.getPosition());
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
for (int i=0; i<allMenuItemCategoryList.size() ; i++){
TextView tab = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tab.setText(allMenuItemCategoryList.get(i).Menu_Item_Category_Name);
mappingOfCategoryWithIndex.add(i, allMenuItemCategoryList.get(i).Menu_Item_Category_Name) ;
tabLayout.getTabAt(i).setCustomView(tab);
}
// int j = 0;
// for (int i=0; i<allMenuItemCategoryList.size() ; i++){
// TextView tab = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
// if(i%2==0){
// tab.setText(allMenuItemCategoryList.get(j).Menu_Item_Category_Name);
// j++;
// }
// else{
// tab.setText("");
//
// }
// mappingOfCategoryWithIndex.add(i, allMenuItemCategoryList.get(i).Menu_Item_Category_Name) ;
// tabLayout.getTabAt(i).setCustomView(tab);
//
//
//
//// tab.setCompoundDrawablesWithIntrinsicBounds(0, drawable.ic_launcher, 0, 0);
//
//
//
// }
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
for (int i=0 ; i<allMenuItemCategoryList.size() ; i++) {
adapter.addFrag(new TabCategory(), allMenuItemCategoryList.get(i).Menu_Item_Category_Name);
}
// int j=0;
// for (int i=0 ; i<allMenuItemCategoryList.size()*2 ; i++) {
// if (i % 2 == 0) {
// adapter.addFrag(new TabCategory(), allMenuItemCategoryList.get(j).Menu_Item_Category_Name);
// j++;
// }
// else{
// adapter.addFrag(new dummyClass(), "");
// }
// }
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
// private final List<String> tabPositionCategory = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title ) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
public class TabCategory extends Fragment{
private TextView nameOfItem;
HomeActivity2 activity;
public HomeActivity2.ViewPagerAdapter adapter;
private TabLayout tabLayout;
LinearLayout container1;
public ArrayList<String> mappingOfCategoryWithIndex = new ArrayList<>();
public ArrayList<MenuItem> allMenuItemsList1 = new ArrayList<MenuItem>();
public ArrayList<MenuItemCategory> allCategoryList1 = new ArrayList<MenuItemCategory>();
private NonSwipeableViewPager viewPager;
private String selectedCategory;
private int selectedCategoryId;
private ArrayList<MenuItem> menuItemsOfSelectedCategory = new ArrayList<>();
View rootView;
public TabCategory() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = (HomeActivity2) getActivity();
allMenuItemsList1 = activity.getAllMenuItemsList();
mappingOfCategoryWithIndex =activity.getMappingOfCategoryWithIndex();
tabLayout = activity.getTabLayout();
viewPager = activity.getViewPager();
viewPager.clearOnPageChangeListeners();
adapter = activity.adapter;
tabLayout.setupWithViewPager(viewPager);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.tab_category, container, false);
menuItemsOfSelectedCategory.removeAll(menuItemsOfSelectedCategory);
menuItemsOfSelectedCategory.clear();
activity = (HomeActivity2) getActivity();
allMenuItemsList1 = activity.getAllMenuItemsList();
mappingOfCategoryWithIndex =activity.getMappingOfCategoryWithIndex();
tabLayout = activity.getTabLayout();
viewPager = activity.getViewPager();
adapter = activity.adapter;
allCategoryList1 = activity.getAllMenuItemCategoryList();
int tab_position=tabLayout.getSelectedTabPosition();
selectedCategory = adapter.getPageTitle(tab_position).toString();
System.out.println("Tab Category: " + selectedCategory );
for (int i = 0 ; i<allMenuItemsList1.size();i++){
if(allMenuItemsList1.get(i).getCategoryName().equals(selectedCategory)){
menuItemsOfSelectedCategory.add(allMenuItemsList1.get(i));
}
}
System.out.println("Size of Category Items: " + menuItemsOfSelectedCategory.size() );
nameOfItem = (TextView) rootView.findViewById(R.id.nameofitem);
System.out.println("Menu Size:"+ allMenuItemsList1.size());
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
container1 = (LinearLayout) rootView.findViewById(R.id.container1);
container1.removeAllViews();
for (int i =0 ; i<menuItemsOfSelectedCategory.size();i++) {
final View addView = layoutInflater.inflate(R.layout.menu_item, null);
nameOfItem = (TextView) addView.findViewById(R.id.nameofitem);
nameOfItem.setText(menuItemsOfSelectedCategory.get(i).getName());
container1.addView(addView);
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
// not cleaning up.
menuItemsOfSelectedCategory.removeAll(menuItemsOfSelectedCategory);
menuItemsOfSelectedCategory.clear();
container1.removeAllViews();
container1.invalidate();
System.out.println("destroyed!!!");
}
}`
Thanks in advance.
So, I put some mock data for testing, here's a working example (I modified your code, it still needs some enhancement but it works).
HomeActivity2.java:
public class HomeActivity2 extends AppCompatActivity {
public ViewPagerAdapter adapter;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private ArrayList<String> mappingOfCategoryWithIndex = new ArrayList<>();
private ArrayList<MenuItem> allMenuItemsList = new ArrayList<MenuItem>();
private ArrayList<MenuItemCategory> allMenuItemCategoryList = new ArrayList<MenuItemCategory>();
//Getters
public ViewPager getViewPager() {
return viewPager;
}
public ArrayList<String> getMappingOfCategoryWithIndex() {
return mappingOfCategoryWithIndex;
}
public ArrayList<MenuItem> getAllMenuItemsList() {
return allMenuItemsList;
}
public TabLayout getTabLayout() {
return tabLayout;
}
public ArrayList<MenuItemCategory> getAllMenuItemCategoryList() {
return allMenuItemCategoryList;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home2);
findViewById(R.id.tabs).bringToFront();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
allMenuItemsList = new ArrayList<>();
allMenuItemsList.add(new MenuItem("Item 1", "Category 1"));
allMenuItemsList.add(new MenuItem("Item 2", "Category 1"));
allMenuItemsList.add(new MenuItem("Item 3", "Category 2"));
allMenuItemsList.add(new MenuItem("Item 4", "Category 3"));
allMenuItemsList.add(new MenuItem("Item 5", "Category 3"));
allMenuItemCategoryList = new ArrayList<>();
allMenuItemCategoryList.add(new MenuItemCategory("Category 1"));
allMenuItemCategoryList.add(new MenuItemCategory("Category 2"));
allMenuItemCategoryList.add(new MenuItemCategory("Category 3"));
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
for (int i=0; i<allMenuItemCategoryList.size() ; i++){
TextView tab = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tab.setText(allMenuItemCategoryList.get(i).Menu_Item_Category_Name);
mappingOfCategoryWithIndex.add(i, allMenuItemCategoryList.get(i).Menu_Item_Category_Name) ;
tabLayout.getTabAt(i).setCustomView(tab);
}
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
for (int i=0 ; i<allMenuItemCategoryList.size() ; i++) {
String category = allMenuItemCategoryList.get(i).Menu_Item_Category_Name;
adapter.addFrag(TabCategory.getTabCategoryFragment(category), category);
}
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title ) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
TabCategory.java:
public class TabCategory extends Fragment {
private static final String KEY_CATEGORY = "category";
private TextView nameOfItem;
private HomeActivity2 activity;
private LinearLayout container1;
public ArrayList<String> mappingOfCategoryWithIndex = new ArrayList<>();
public ArrayList<MenuItem> allMenuItemsList1 = new ArrayList<MenuItem>();
private String selectedCategory;
private ArrayList<MenuItem> menuItemsOfSelectedCategory = new ArrayList<>();
private View rootView;
public TabCategory() {
}
public static TabCategory getTabCategoryFragment(String selectedCategory) {
TabCategory tabCategory = new TabCategory();
Bundle args = new Bundle();
args.putString(KEY_CATEGORY, selectedCategory);
tabCategory.setArguments(args);
return tabCategory;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
selectedCategory = getArguments().getString(KEY_CATEGORY);
activity = (HomeActivity2) getActivity();
allMenuItemsList1 = activity.getAllMenuItemsList();
mappingOfCategoryWithIndex = activity.getMappingOfCategoryWithIndex();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.tab_category, container, false);
container1 = (LinearLayout) rootView.findViewById(R.id.container1);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
container1.removeAllViews();
menuItemsOfSelectedCategory.clear();
for (int i = 0; i < allMenuItemsList1.size(); i++) {
if (allMenuItemsList1.get(i).getCategoryName().equals(selectedCategory)) {
menuItemsOfSelectedCategory.add(allMenuItemsList1.get(i));
}
}
for (int i = 0; i < menuItemsOfSelectedCategory.size(); i++) {
final View addView = layoutInflater.inflate(R.layout.menu_item, null);
nameOfItem = (TextView) addView.findViewById(R.id.nameofitem);
nameOfItem.setText(menuItemsOfSelectedCategory.get(i).getName());
container1.addView(addView);
}
}
}