I want to implement a wizard with Material Design Support library.
I'm using ViewPager
and Tablayout
but I have a problem adding new tabs dynamically.
First, I set up my widgets in onCreate
method:
viewPager = (ViewPager) findViewById(R.id.tab_viewpager);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new DummyFragment(), "1");
adapter.addFrag(new DummyFragment(), "2");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
Then I want to add another card, when an element in the list is clicked:
adapter.addFrag(new DummyFragment(), "3");
adapter.notifyDataSetChanged();
tabLayout.setTabsFromPagerAdapter(adapter);
The problem is: after adding, selected card was resets to first.
How to prevent this behaviour?