Android: data not loading in fragmentstateviewpager while setting current page - android

I am using fragmentstateviewpager where the fragments are created dynamically. Each page loads data by calling a web service. My viewpager will have 5 items, by default I am setting the current item as the 5th item using:
mPager.setCurrentItem(4);
The 5th item is getting displayed but data is not loaded in it. When I swipe two items back and comes again to the 5th item, now the data is loaded. Can anyone help me to solve this issue.

Related

Updated items on recyclerView are lost when view pager is used

When i change recyclerView data of one tab,data is being reflected(working fine), but when i switch to other tabs and go back to updated tab , the updated items on recyclerView are lost .how to notify viewpager adapter that i have changed data in recyclerAdapter.
Actually, ViewPager loads the next and previous (if any) fragment on initialization and if you will go to the second tab it will load 3rd automatically. You need to use:
viewpager.setOffscreenPageLimit(3);//3 is the limit not refreshing the fragment till 2 swipes
Hope this will solve your problem.

how to make vertical scrolling list in sync with view pager

I have vertical scrolling list in Activity 'A' and view pager in Activity 'B'. When a user clicks on an item in Activity 'A' it goes to Activity 'B' for detail. Now user can swipe left or right to view details of next item inside list directly from Activity 'B'.
List in Activity 'A' is endless scrolling, when it reaches to end it loads more items automatically.
I am using fragment which represents one page inside view pager in Activity 'B' which takes the same object as a parameter as in RecyclerView item in Activity 'A'.
This pattern is commonly used for reading articles.
Now my question has two parts:
First is how to use the same data list for both activities? I want to keep data at one place as both activities using same data, only representation and navigation is different, one inside RecyclerView and other is inside ViewPager and data list is already loaded for RecyclerView in Activity A. Moreover data is also large so i don't want to duplicate data in memory for each activity.
Second is when i swipe next from view pager and go to next item's detail in activity 'B' the recycler view in Activity 'A' should also scroll to next item synchronously, although it's not visible to user. so that RecyclerView can load more items when reaches till end and ViewPager can also behave endless swipe like endless scrolling list. Is there any way to achieve this behavior?
Thanks in advance.
You can use Local Database for this purpose. So both activity will access data from DB. Only pass index of item to second activity and onPageScroll listener you can change that index and get data from DB according to page swipe(left/right).

ViewPager show empty screen initially but displays data on swiping back

Android ViewPager shows empty screen initially but displays data on swiping back.
Scenario:
I am using 3 tabs with recyclerview. I am using volley library to get server data. On loading the app all tabs are empty but on swiping to the 3rd tab and back to the first the data displays on the 1st and 3rd tab. The Second tab remains empty.
Any solution is appreciated.

How to change content of fragment in pagerview

I have a Page which has three buttons (teens, adult, old) and same page contains view pager which has 7 tabs for week days. When this page loaded it show list of all programs for all (teens, adult and old) categories in all 7 tabs according to which day is. What I want to implement is that when user select anyone one of the above button the contents of all the tabs populated by new filtered data list according to the selected button.
How I can implement this?.
You just need to set the selection in your Adapter and then call notifyDataChanged. That will reload the adapter and go back through the instantiateItem calls again. In instantiateItem you can look at the selection and change the content accordingly.

Saving tab information after swipe

I have 3 tabs in my application.
In my first tab I need to read JSON data from URL and using the user location. It takes about 2-3 seconds to load the page.
My problem starts when I implemented tabs, and now every time im swiping next and back, the info is re-fetched.
I tried the following solution:
I moved my method to the MainActivity, and created a variable named "isUpdated" and updated it to 1 if the info has been fetched successfully. since then, the tab data is gone after swiping and it shows blank empty tab.
I understand that the tab data is wiped after swiping to another tab, but I need to save the tab content.
How I can save the tab information and show it after the user swiped to the next tabs, without re-fetching the data over and over again?
if you are using ViewPager then set your off screen page limit to something different
for example
mViewPager.setOffscreenPageLimit(2);
should be enough for 3 tabs

Categories

Resources