0

I have three fragments in a view pager:

A -> B -> C

I would like to get the strings of my two edittexts in Fragment A on swipe to Fragment B to show them in Fragment B. The edittext data may be changed up until the swipe.

Someone has suggested listening for typing and sending data after each one, but the callbacks I know for that change state EVERY key click (which can be expensive). How do I this without using buttons, since their right next to each other, for a more delightful experience?

Frank B.
  • 204
  • 5
  • 17

2 Answers2

1

You can check the data of the EditText on swipe ; if it's not null, then you can send it to any other fragment using Bundle since you are dealing with fragments

Ikechukwu Kalu
  • 1,474
  • 14
  • 15
  • How do I check for a swipe of the viewpager? I have tried looking to see if the current page changed or overriding the uservisiblity hint method. – Frank B. Jan 04 '16 at 16:03
  • You can use ` viewPager.setOnPageChangeListener()` in your Activity to run code based on selected. See http://stackoverflow.com/questions/22184406/run-code-when-user-swipes-out-of-viewpager-fragment – Ikechukwu Kalu Jan 04 '16 at 16:10
0

With help from @I. Leonard I found a solution here. It was deprecated so I used the newer version. I put the below code in my fragment class because I needed access to the data without complicating things. It works like a charm!

On the page listener callback, I suggest, calling an interface for inter-fragment communication to do your actions on your home activity or to call the appropriate fragment that can do the work, from the activity.

// set content to next page on scroll start
vPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
vPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_SETTLING) {
            // ViewPager is slowing down to settle on a page
            if (vPager.getCurrentItem() == 1) {
                // The page to be settled on is the second (Preview) page
                if (!areFieldsNull(boxOne.getText().toString(), boxTwo.getText().toString()))
                    // call interface method in view pager activity using interface reference
                    communicator.preview(boxOne.getText().toString(), boxTwo.getText().toString());
            }
        }
    }
});
Community
  • 1
  • 1
Frank B.
  • 204
  • 5
  • 17