0

How do I call a fragment onpageselected method? I have 20 tabs. In every tab I call the same fragment. However, when I click different tab I want it to reload itself. How can I do that? Thanks.

I want to make an app like Google Play News.

MyViewPagerAdapter

 ViewPager viewPager = materialViewPager.getViewPager();
    viewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
        @Override
        public Fragment getItem(int position) {
            switch (position % 21) {
                case 0:
                    return NewsContentFragment.newInstance();
                case 1:
                    return NewsContentFragment.newInstance();
                case2:
                continues in the same way to 21...
nuhkoca
  • 1,777
  • 4
  • 20
  • 44
  • Hard to tell, but this will probably help: http://stackoverflow.com/a/36504458/4409409 – Daniel Nugent Aug 12 '16 at 22:59
  • Thanks but do I have to create 20 fragments? This is so weird no more solution :( – nuhkoca Aug 12 '16 at 23:08
  • I don't understand exactly what you're asking, can you be more specific? – Daniel Nugent Aug 12 '16 at 23:12
  • You know Google Play News app right? It has over 15-20 tabs and they use only one Fragment for 20 tabs. I am working on a project like Google's. I added 20 tabs to viewpager. I also use one Fragment and one RecyclerView for these categories. Think about it, for instance I am on 'kid category'. Program shows me news related to kids. When I click 'fashion category' program shows me news related to fashion. However the app needs to do these in only one Fragment. I mean fragment needs to update itself when tabs are changed. How do I do this? What function should I do this? onPageSelected or? – nuhkoca Aug 12 '16 at 23:27
  • It sounds like the approach in the answer I linked to would work. You have one Fragment class, and then 20 Fragment references in your FragmentPagerAdapter, correct? If you post your current FragmentPagerAdapter code it would be much easier to help..... – Daniel Nugent Aug 12 '16 at 23:31
  • @DanielNugent I have added my codes to the question. Please check them out. I am looking forward to your comment :) – nuhkoca Aug 12 '16 at 23:38
  • Ok, looks good. You can definitely use the technique from the linked answer, just add the `instantiateItem()` override in the FragmentPagerAdapter, add the `fragments` array, and add the ViewPager OnPageChangedListener. – Daniel Nugent Aug 13 '16 at 00:42
  • If you don't mind can you please transcribe for my project? – nuhkoca Aug 13 '16 at 00:44

0 Answers0