简体   繁体   中英

ViewPager: Fire event before page change

I'm using Android Studio 1.2.1.1 and I've implemented a ViewPager with a fair few fragments. I'm trying to get the data from each of the fragments into a public class via an interface. The classa nd interface are not the issue though, I want to export the data from the current page before a new page is selected, unfortunately it appears these are the only options and none really suit my needs:

    _mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrollStateChanged(int position) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int position) {
        }

    });

I would be able to invoke the call to export the data easily on checkboxes etc, but my fragments include imageviews (exported as base64) and edittexts. Has anyone else come across this in the past? Is it an easy one or is it going to be changing my code entirely?

Any help would be greatly appreciated

Can you not just keep a reference to what page you're currently on and set that index every time onPageSelected is called. You can then see what the index was before changing it and update the old Fragment with that?

Example:

int oldPosition = -1;

            @Override
            public void onPageSelected(int position) {
                if (oldPosition != -1) {
                     extractInformationFromFragment(oldPosition);
                }
                oldPosition = position;
            }

private void extractInformationFromFragment(int position) {
    switch (position) {
        case 0:
            fragment1.getInformation();
            break;
        case 1:
            fragment2.getInformation();
            break;
        case 2:
            fragment3.getInformation();
    }
}

I'm handling it this way :

private int mCurrentSelectedPage = 0;

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout) {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            // Get data from last selected fragment
            EditFragment oldFragment = getFragment(mCurrentSelectedPage);
            if (oldFragment != null) {
               oldFragment.getData();
            }
            // Set data to the new fragment
            EditFragment newFragment = getFragment(position);
            if (newFragment != null) {
                newFragment.setData();
            }
            mCurrentSelectedPage = position;
        }
    });


public EditFragment getFragment(int position) {
    String fragmentTag = "android:switcher:" + mViewPager.getId() + ":" + position;
    return (EditFragment) getSupportFragmentManager().findFragmentByTag(fragmentTag);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM