0

I create Navigation Drawer with tab view, and thatis ok. But, when I swipe itens on tabView by ViewPager the function getItem don't return what occurs.

My Activity:

    setupNavDrawer(4);

    FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.container);

    getLayoutInflater().inflate(R.layout.activity_lte, contentFrameLayout);

    String[] titles = {"700Hz", "1800Hz", "2500Hz"};
    tabAdapter = new TabAdapter4(getSupportFragmentManager(), this,titles);
    mviewPager = (ViewPager) findViewById(R.id.vp);
    mviewPager.setAdapter(tabAdapter);

    getSupportActionBar().setElevation(0);
    tabAdapter = new TabAdapter4(getSupportFragmentManager(), this,titles);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mviewPager);

MyTabAdapter:

public TabAdapter4(FragmentManager fm, Context c, String[] titles) {
    super(fm);

    this.fm = fm;
    this.titles = titles;
    this.mContext = c;
}

@Override
public Fragment getItem(int position) {
    Fragment frag = null;

    switch (position) {
        case 0:
            //frag = new Lte700Fragment();
            frag = Fragment.instantiate(mContext, Lte700Fragment.class.getName());
            break;
        case 1:
            //frag = new Lte18Fragment();
            frag = Fragment.instantiate(mContext, Lte18Fragment.class.getName());
            break;
        case 2:
            //frag = new Lte25Fragment();
            frag = Fragment.instantiate(mContext, Lte25Fragment.class.getName());
            break;
        default:

    }

    Bundle b = new Bundle();
    b.putInt("position", position);

    frag.setArguments(b);

    return frag;
}

@Override
public int getCount() {
    return 3;//titles.length;
}


@Override
public CharSequence getPageTitle(int position){
    return (titles[position]);
}

Frangments were changed okay, but 'position' in getItem not corresponds at the reality. When I'm first fragment the position return 0 and 1, when I'm second fragment return 2, and return nothing on third fragment.

And this is harmful to my application because I need control AsyncTask by fragment life cycle, because the thread will change when fragment be onPause or onResume.

My doubt is: how return right position for close, for example, fragment 1 and 2 when I'm using 3.

lidiaxp
  • 9
  • 6
  • You realize you set `mviewPager` twice with the exact same information, right? – OneCricketeer Sep 20 '17 at 01:26
  • ViewPagers by default will always load the current page and **both** pages next to the current item. e.g. Page 1 will load position 0 and 2. Thats just how they work – OneCricketeer Sep 20 '17 at 01:28
  • "You realize you set mviewPager twice with the exact same information, right?" = I really did not realize it, but it does not work or call just once – lidiaxp Sep 20 '17 at 01:38
  • How would I do if I wanted to just call the current fragment and the others would be paused? – lidiaxp Sep 20 '17 at 01:38
  • It is needed to have both... [*one page to each side of the viewed page. This is necessary to have the animation effects work -- you see parts of two fragments (original and new) at the same time*](https://stackoverflow.com/questions/11650152/viewpager-offscreen-page-limit) – OneCricketeer Sep 20 '17 at 01:42
  • You can get use a Tab selection event listener on `TabLayout` if all you want is the position of the selected tab. It's not really clear where the NavDrawer is part of this problem – OneCricketeer Sep 20 '17 at 01:43
  • navDrawer not is a problem, the problem it is not return the real position of tabs, so the life cycle don't works how I wanna – lidiaxp Sep 20 '17 at 01:52
  • It does return the real position. It just also returns the other positions... You need to move the Bundle and position values only into the `case` statements – OneCricketeer Sep 20 '17 at 01:58
  • this not work, and not return the real position because on the first fragment return 0 and 1, on the second return 2 and return nothing on the third – lidiaxp Sep 20 '17 at 02:03
  • The fact that you have any code at all after `switch (position)` is the problem. That code *will get called* multiple times. If you simply replace `break` with `return frag`, and set the position between creating frag and returning it, you will get the needed position – OneCricketeer Sep 20 '17 at 02:05
  • not work, persist in the same error – lidiaxp Sep 20 '17 at 02:14
  • How are you even checking what's returned here? Add the code that gets the Fragment arguments – OneCricketeer Sep 20 '17 at 02:17
  • I'm using oncreateview, onresume, onstop and onpause on fragments with Log. When I click on 700Hz (default) Log return onresume of 700 and 1800 hz, when I click on 1800Hz log return onresume of 2500 hz and when I click on 2500Hz log return onstop of 700hz, but the views correspond to tabs – lidiaxp Sep 20 '17 at 02:28

1 Answers1

0

You're returning the Fragment and setting the position too late since getItem is called multiple times in any given ViewPager change depending on the current page and whats been loaded already.

The following code should set the position argument, then return the Fragment with the correct argument.

@Override
public Fragment getItem(int position) {
    Fragment frag = null;

    final Bundle b = new Bundle();
    b.putInt("position", position);

    switch (position) {
        case 0:
            frag = Fragment.instantiate(mContext, Lte700Fragment.class.getName());
            frag.setArguments(b);
            return frag;
        case 1:
            frag = Fragment.instantiate(mContext, Lte18Fragment.class.getName());
            frag.setArguments(b);
            return frag;
        case 2:
            frag = Fragment.instantiate(mContext, Lte25Fragment.class.getName());
            frag.setArguments(b);
            return frag;
        default:
            return null;
    } 
    // End the switch, end the method. 
}

Might seem like duplicate code, but there is a slight difference in what is actually happening

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • My answer is the correct way to address the positions. There isn't a way around the Fragment lifecycle as far as I know – OneCricketeer Sep 20 '17 at 02:33