Zafir Stojanovski
Zafir Stojanovski

Reputation: 481

Changing tabs in TabLayout through code, with one obstacle

The idea is that I have a listener FAQFragment.OnQuestionSelectedListener that is attached to the main activity which holds a TabLayout. Now, I want every time I receive the event, to pass the event arguments to one of the fragments in the TabLayout. This is my code so far:

public class MainActivity extends AppCompatActivity implements FAQFragment.OnQuestionSelectedListener{

    private SectionsPagerAdapter sectionsPagerAdapter;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    public void onQuestionSelected(String question) {
        sectionsPagerAdapter.setFaq(question);
        sectionsPagerAdapter.notifyDataSetChanged();
        selectPage(1);
    }

    private void selectPage(int pageIndex) {
        tabLayout.setScrollPosition(pageIndex,0f,true);
        viewPager.setCurrentItem(pageIndex);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter{

        private String faq;

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
            faq = null;
        }

        public void setFaq(String faq) {
            this.faq = faq;
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) return FAQFragment.newInstance();
            else if (position == 1) {
                ChatFragment chatFragment = ChatFragment.newInstance(faq);
                faq = null;
                return chatFragment;
            }
            else if (position == 2) return SettingsFragment.newInstance();
            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0: return "FAQ";
                case 1: return "Morty";
                case 2: return "Settings";
                default: return null;
            }
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupTabLayout();
    }

    private void setupTabLayout() {
        // adapter
        sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // viewpager
        viewPager = findViewById(R.id.container);
        viewPager.setAdapter(sectionsPagerAdapter);

        // tablayout
        tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

        // load chat initially
        viewPager.setCurrentItem(1);

        // listeners
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
    }
}

The problem with this code is that it doesn't go through the FragmentPagerAdapter's getItemMethods() when tabs are swapped through code. Now, I know that I could do this using fragmentManager() to swap the Fragments when I receive the event, but when I did that, the transition was not smooth (there is a small flickering that grinds my gears). So is there any way to implement a similar logic that would pass a new Fragment Instance every time a tab is selected through viewPager.setCurrentItem or tabLayout.getItem(1).select ?

Upvotes: 2

Views: 50

Answers (1)

MeLean
MeLean

Reputation: 3421

You can get FAQFragment, ChatFragment or SettingsFragment from ViewPager as described here. Once you got the needed Fragment you can manipulate whatever you want.

Upvotes: 1

Related Questions