user2673161
user2673161

Reputation: 1695

Android ViewPager Bug on Swipe

I have this weird issue with the viewPager. When I swipe from one fragment to the next and then swipe back, the layout is correct, but when pressing an element (Listview item for example), the previous java code still did not update with the view. I don't know if I can explain this any better, as it works when I play with the swipe until the fragment works like its supposed to. here is my code:

Main.Java:

public class Main extends FragmentActivity {

SectionsPagerAdapter mAdapter;

ViewPager mViewPager;

// for alert transitions
AlertDialog dialog;

// share button
private ShareActionProvider mShareActionProvider;

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

    // set up button
    getActionBar().setDisplayHomeAsUpEnabled(true);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAdapter);
    mViewPager.setPageTransformer(true, new DepthPageTransformer());



}

@Override
public void onBackPressed() {
    if (mViewPager.getCurrentItem() == 0) {
        // If the user is currently looking at the first step, allow the system to handle the
        // Back button. This calls finish() on this activity and pops the back stack.
        super.onBackPressed();
    } else {
        // Otherwise, select the previous step.
        mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
    }
}
}

SectionsPagerAdapter.java:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

@Override
public Fragment getItem(int position) {

    switch (position){

    case 0:
        return new Important();
    case 1:
        return new Propositions();
    case 2:
        return new Information();
    }
    return null;
}

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



@Override
public CharSequence getPageTitle(int position) {
    Locale l = Locale.getDefault();
    switch (position) {
    case 0:
        return ("Important Dates").toUpperCase(l);
    case 1:
        return ("Props").toUpperCase(l);
    case 2:
        return ("Information").toUpperCase(l);
    }
    return null;
}
}

The Fragments all use the onCreateView() method.

Thanks in advance for your help!

Upvotes: 1

Views: 1357

Answers (1)

A. Ly
A. Ly

Reputation: 33

Maybe a late response, but I've experienced the same issue. The problem was the DepthPageTransformer you are using from the Android Developers example for PageTransformer.

There is a bug with stacking fragments and layer priority, I assume. By fixing the custom PageTransformer will fix your "swipe back" issue. Or if you don't mind the animation, just remove your custom PageTransformer and use the default animation (by adding nothing).

Upvotes: 1

Related Questions