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.
Related
I have 3 fragments in the ViewPager.
PersonalFragment
CropFragment
NotesFragment
Every fragment has a RecyclerView and I was adding items using a dialog in it. However, when I go to the last fragment (i.e. NoteFragment), the first fragment's (i.e. PersonalFragment) RecyclerView gets empty (all items are removed). The same thing happens when I come back to the first fragment, the last fragment's RecyclerView resets (all items are removed).
It is difficult to understand without seeing some of your code which is related. I think you might have implemented the onResume function incorrectly. Each time you go to the third fragment in your ViewPager, the first Fragment needs to be reinitialized and you need to take care of this in your onResume function.
However, another workaround for your problem is setting the off-screen page limit of the ViewPager like the following.
viewPager.setOffscreenPageLimit(2);
Hope that helps.
I have 4 fragments attached with a viewpager. The 1st fragment has a spinner that has an item selected listener in onCreateView() method. Each time an item is picked from the spinner, a list adds that data.
Now when I swipe right to get to the other fragments, the 1st fragment is paused. Then if I come back to the 1st fragment the onCreateView() method runs again and loads the item selected listener again and adds the previosly added data again.
To avoid the situation I've taken the item selected listener inside an on touch listener. Now It's working as expected. But it just does not feel right. Any advice or help will be appreciated.
Thanks in advance.
Use this like this:
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(4); // 4 used for number of fragments like you are using 4 fragments using viewpager.
This setOffscreenPageLimit will not refresh your fragment again and again.
I have an app where consisting of a viewpager with 3 tabs, with the second tab containing a recyclerview with items.
If I were to hold onto the item, a dialogfragment would popup. There's an option to delete the item from the database. However, I'm having trouble working out how I would refresh the recyclerview. I've tried creating a new fragment (2nd tab fragment), however, the data doesn't update.
The only thing I can get working is by recreating a new activity, however, that loads the first tab of the viewpager.
Thanks in advance.
I am having a viewpager and having 3 tabs in it. All the three tabs are having their fragments. What I noticed that each fragment data is getting loaded at the first time when I am setting adapter to viewpager.
What I need is to load the fragment data of the corresponding tab only when the particular tab is getting clicked.
I have noticed one more issue in it. Like when the activity started in which I am having viewPager, then the data of the first tab's fragment get loaded at the same time the data of the second tab's fragment also get loaded and that is shown when I click on that tab.
Same way when I click on Second Tab, third Tab's Fragment date get loaded.
So what I need is that the data of the fragment should be loaded only when I click on the particular tab.
Thank you so much in advance..
You should use the ViewPager.setOffscreenPageLimit(0) to only load the currently displayed content.
Its the View-pager behaviour that it loads one page left/right to current page even if you have specified setOfflinePageLimit(0).
To load each fragment on click on tab, you should use LocalBroadcastReciever.
I create a list-view inside my fragment . i use pull to refresh view in my fragment
when i click on list item a another fragment will load with backstack .Before loading the new fragment i save details about list-view to restore the position of list view when coming back.
But when when i come back to first fragment the scroll position is not retained properly
I use this solution to restore list position .
also i use the same solution for endless scroll it works fine
Can anybody tell me what went wrong ???