Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewpager not working correctly in lollipop 5.0 when I use other fragments

This problem only ocurs in lollipop. kitkat and previous versions work correctly!

The problem is that I have implemented a slider using a pageview, in the first fragment I have the login fragment with two buttons.

When the app start there isn't a problem, but when I change the fragment using page view and back to the login fragment, the buttons not working.

The buttons load the register form or login form in the same fragment. There isn't an error, only the button does nothing.

This is the code:

public class ScreenSlideActivity extends ActionBarActivity {
    /**
     * The number of pages (wizard steps) to show in this demo.
     */
    private static final int NUM_PAGES = 5;

    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;

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

        // Instantiate a ViewPager and a PagerAdapter.
        /*postponeEnterTransition();
        mPager.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            public boolean onPreDraw() {
                mPager.getViewTreeObserver().removeOnPreDrawListener(this);
                startPostponedEnterTransition();
                return true;
            }
        });*/
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPager.setPageTransformer(true, new DepthPageTransformer());
        LinePageIndicator indicator = (LinePageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(mPager);
    }

    @Override
    public void onBackPressed() {
        if (mPager.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.
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }

    /**
     * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
     * sequence.
     */
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch(position)
            {
                case 0:
                    return new LoginSignupFragment();
                default:
                    return new SlidePageFragment();
            }
        }

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

}

The first fragment's code to load the login form or register form is:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_slide_login, container, false);

    ingresarBtn = (Button) rootView.findViewById(R.id.ingresarBtn);
    ingresarBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment frag = new LoginFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.slide_login_content, frag);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    });
    unirseBtn = (Button) rootView.findViewById(R.id.unirseBtn);
    unirseBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment frag = new SignupFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.slide_login_content, frag);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    });
    return rootView;
 }

If is necessary more information, I will give.

like image 531
Android-developer Avatar asked Apr 25 '15 09:04

Android-developer


People also ask

Is ViewPager deprecated?

This function is deprecated.

When should I use ViewPager?

ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in app. It is a widget found in the support library. To use it you'll have to put the element inside your XML layout file that'll contain multiple child views.

How do I use ViewPager?

You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .


1 Answers

I had the same problem with my ViewPager. I googled a lot but didn't find any solution. Then I remembered setting android:hardwareAccelerated="false" in the manifest of my application.

Removing android:hardwareAccelerated="false" line from the manifest did the trick. It seems having this line in the manifest causes the buttons and textviews used inside a ViewPager to not function properly for android lollipop.

So if you have android:hardwareAccelerated="false" in your manifest try removing it. Or you can also try setting android:hardwareAccelerated="true" for the specific <activity> or for the <application> tag.

As mentioned here https://stackoverflow.com/a/10339406/3090861 the attribute will be ignored on older versions of Android.

You can find out more about hardware acceleration at http://android-developers.blogspot.in/2011/03/android-30-hardware-acceleration.html.

Hope it helps you!

like image 189
rakex Avatar answered Nov 04 '22 16:11

rakex