Domanda

I have a ViewPager with pages of WebView. A page has a text field allowing the user to type in contents. I'd like to save the page's input text to a Java variable when moving between pages (by sliding forward/backward).

Unfortunately, it seems onPageSelected only tells me the page number that is about to display, and not the page it came from, which means I can't tell which page I needed to save at the time when the user changes page.

Any ideas? Many thanks in advance!

È stato utile?

Soluzione

You can store the last known page index in a field at the end of onPageSelected (initialize it with an appropriate default when your activity starts). Then, inside onPageSelected, you check that field to see what page the user was previous on.

private int mLastPage;

@Override
public void onPageSelected(int position) {
    // save the information from the page at mLastPage

    mLastPage = position;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top