Questions when implementing Android viewpager and tap functions

Asked 1 years ago, Updated 1 years ago, 106 views

We are currently using the toolbar, tabs, and viewpager, and each is shown in the fragments. There are three tabs, and each time you turn over the screen, the data is being retrieved from the server. But I'm loading data from the first tab, going to the third tab, and then coming back, I'm loading data again, going back to the third tab, and so I'm loading data again.

Is there a way to save the data that was already loaded at first, so if the tab is selected again or if it is turned over the screen, it is left without loading again? Should I do OnTabSelected or OnTabReselected here?

And if there are more than three tabs, it doesn't go smoothly on the viewpager, but it goes completely over when you drag it a couple of times. What should I do? ㅜ<

android android-viewpager tablayout fragment

2022-09-22 13:56

1 Answers

If you are using a fragment, you can use the fragment lifecycle to resolve it. Ask the server for the required data in the 'onCreate()' or 'onCreateView()' function of the fragment, and keep the page in the view page in memory. Use the 'setOffscreenPageLimit(int)' function in the view page to set the number of tabs.

If you do not set the number of off-screen pages in the viewpager, be careful as the fragments can be repeatedly generated/disappeared and requested to the server (depending on the implementation).


2022-09-22 13:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.