Frank B.
Frank B.

Reputation: 204

Get EditText data on swipe to next Fragment

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?

Upvotes: 0

Views: 230

Answers (2)

Frank B.
Frank B.

Reputation: 204

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());
            }
        }
    }
});

Upvotes: 0

Ikechukwu Kalu
Ikechukwu Kalu

Reputation: 1644

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

Upvotes: 1

Related Questions