I have a ViewPager with 2 Fragments, FragA and FragB. Inside each Fragment I have a RecyclerView. When a user presses a button inside the RecyclerView in FragB I would like to remove the respective Object from that RecyclerView's ArrayList and add it to the RecyclerView contained in FragA.
Can I directly pass the Object from one RecyclerView's Adapter to the other? Or do I need to pass the Object from the RecyclerView Adapter to FragB then to the Activity containing the ViewPager then to FragA before finally passing to the RecyclerView inside FragA?
Your fragments and activity is just to present data.
I think the best solution is to use good design, like MVC pattern, or at least some model class (model-view kind of pattern), wich will have referens to both lists and can have public method for moving object between lists.
Giving both lists to both fragments so they can move objects is bad design choise IMHO.
This question is not for adapter solution at all.
Related
I have a simple activity with two fragment: main (contains a list of items) and detail. Details screen is a fragment, which is displayed when an item is clicked in main list. DetailFragment is being shown using replace().
After navigating back from detail screen I would like to have exact same scroll position in RecyclerView that I left it with.
As long as replace() initiates onDestroyView() and onCreateView() cycle to happen for MainFragment, after coming back from DetailFragment the RecyclerView has a scroll position of 0 (at the top), because this is a brand new RecyclerView which has no connection to the one that was there before leaving to DetailFragment.
replace()ing fragment does not initiate onSaveInstanceState() to be called, that's why using techniques outlined here are not applicable.
I wonder what is the correct way of handling this use-case?
Of course I can save the position on onDestroyView() and later manually scroll to that position, but maybe I'm missing some obvious solution?
An answer, that will propose to use add() instead of replace() is not welcome.
I've had RecyclerView declared as a child of NestedScrollView. This was the issue.
When I got rid of NestedScrollView everything was handled automatically.
As onSaveInstanceState() is not possible, try using ViewModel to save the required data.
When you declare and set the adapter to your first recyclerview, try having a call back to your actvity passing the adapter instance , where in your activity you can try saving the adapter instance or any required data in the ViewModel of your activity.
Once you come back from your details fragment you can get the saved instance of your reycycler data from the activity`s ViewModel.
Question:
How do I persist the RecyclerView's data
Scenario:
RecyclerView inside of a Fragment(say A) coped with ViewPager along with 4 other Fragments(B,C,D,E)
RecyclerView is populated with PostRecylcerViewAdapter adapter class. And the data is fetched from Firebase.
Problem:
Screen rotations, flipping between the Fragments reloads everything resulting in multiple connections/downloading from Firebase.
What I already know:
That I have to store the states maybe using savedInstanceState, and handle screen rotations, bundle/parcelables etc.
But in this case, how?
Components and structure:
A, B, C, D, E - 5 Fragments with ViewPager in their Parent Activity's layout(say HomeActivity).
RecylerView inside of a Fragment say B.
RecyclerView Adapter is attached inside the onDataChanged() method of Firebase Database Reference.
So how?
Try add that in fragment B onCreate():
setRetainInstance(true);
It will keep your fragment alive during rotation changes.
I got a page view with 3 fragments, and all 3 use some part of the same list, should I use a listener in my activity so I can update the list, or should I make the list public and access it by a method?
updated
And if i use a observer listener, how would i implement it.
Have the activity the listener and the fragments as observers? or do i have the fragments be an observer aswell?
Prefer the listener way.
If you change the list directly, other fragments will not know to update their UI
I have two fragments in same activity class.
On first fragment I made a public object which have two lists inside it.
and populate using gson library from a json string.
then I showed first list on listview in first fragment.
Now I want to show second list on another fragment.
how can I get the same object in second fragment, so that I can use its second list.
Second fragment will open after clicking on a button which is in first fragment.
You probably could create whatever objects you need and are common for your fragments in your activity and access them from fragment by ((YourActivity)getActivity()).yourObject
Sorry that is not possible way you are trying to do.
Because it violates the OOPS principle that one child can not have multiple parent.
So how to do this.
1) Create a common fragment with listview.
2) From activity load data as you are loading.
3) And pass data to fragment and that you want show in first and second and so on...
4) No need of two listview
5) Just change the data for listvew
I have two fragments nested within another fragment. One is a list view, and one is a ViewPager, with the viewpager's content being details of a selected list view item. Obviously, they both use the same adapter. The contents of the adapter is refreshed every, say, 5 seconds, so I need to keep the adapter used by both fragments in sync.
What would be the best way to go about keeping the adapter in sync between the two fragments?
I was thinking of storing the adapter in a retained non-ui fragment, making all the update calls within this fragment. This would however require I constantly make pass the updated adapter to the child fragments and call notifyDataSetHasChanged in each fragment