0

My application uses tabs with swipe navigation defined in developer.google.com where I am using the FragmentPagerAdapter.

When trying to make sure my application is accessible, I came across the following problem:

When the application is started, the onCreateView(...) of the 1st fragment is called, followed by those of the 2nd and 3rd fragments. As a consequence of this, the third tab's title has focus when the application starts and is read out even though the first tab is currently visible to the user and it's title should be read out.

While swiping through the tabs, the tab titles are read out correctly, depending on which tab is currently visible to the user.

Is there any way to fix this so that on startup, the first tab title has focus and is read out.

P.S. I tried fixing this by sending an accessibility event from the first fragment's setUserVisibleHint(...) method but this doesn't work, at least on android 4.4. The accessibility event I am sending is a call to one of the 1st fragment's textview's sendAccessibilityEvent function. Again, to clarify, the issue is not with the accessibility event, this works just fine while the application is running and I am swiping between tabs. The issue is that on startup, app focus is not on the currently displayed fragment but is instead on the fragment whose onCreateView() was called last.

Dustfinger
  • 1,013
  • 1
  • 18
  • 27

1 Answers1

0

Do this in your mainactivity getItem method.. something like this ..

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a Fragment 

        if (position == 0) {
            return //Fragment 1;
        }
        if (position == 1) {
            return //Fragment 2;
        }

        if (position == 2) {
            return //Fragment 3;
        }
        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

You should implement something like above in following part of your code..

public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
    public DemoCollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DemoObjectFragment();
        Bundle args = new Bundle();
        // Our object is just an integer :-P
        args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
}

above code is from google developer site you mentioned.

Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
  • I should have clarified that I did indeed implement the FragmentPagerAdapter not the FragmentStatePagerAdapter. Thanks for the answer anyway. – Dustfinger Sep 15 '14 at 17:48
  • While creating a new project, in Eclipse you can auto generate code for the tabbed view. In Create Activity Section choose Tabbed Activity and in the next Dialog select the Navigation style as desired.. You can build your app on top of it .. very easy and time saving .. and pls mark my ans as correct if it hellped you (idk if you can or not .. i am new and i need some points i think..) Thanks :D – Kushal Sharma Sep 15 '14 at 17:59