Fragment tab listener - android

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

Related

How do I not refresh the list when we use navigation?

I use Android jitpack navigation in the project.
In the first fragment, I show the list in recyclerview.
When I click on the item, I show the second fragment.
But when I go back to the first fragment, recyclerview refreshes the old list.
How do I not refresh the list?
You should use ViewModel to keep list through navigation. Provide the list to your fragment using LiveData and observe it in the fragment's onViewCreated() method

Restore RecyclerView's scroll position after performing Fragment replace()

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.

Is there a more efficient way to implement event listeners in nested fragments?

Right now, my application has a single activity and 4 fragments:
ViewPager fragment.
2X fragments that contain a RecyclerView each.
A details fragment populated on the basis of the item chosen.
I read that it's in good practice to implement listeners to handle events in fragments and let the activity handle events and communication between the fragments. But to make it possible, I've used too many levels of listeners and my code seems to complicated. Is there a better way ? One example of the sequence of listeners is:
Listener in RecyclerView Adapter --> Listener in RecyclerView Fragment -->
Listener in ViewPager --> Listener in MainActivity --> Listener in info Fragment.
And my ViewPager's in a fragment so I could create a flexible UI to support tablets as well.
Look at Eventbus: https://github.com/greenrobot/EventBus
Have all your fragments and activity register themselves on the event bus. Whenever you want to trigger an event, simply post an event and any fragment/activity that is accepts that event will receive it and react accordingly.
Just name your events appropriately and your code should be easier to understand.

Sharing an adapter between two fragments

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

Setting ViewPager's current item from fragment

I'm using a ViewPager in my app, with the initial fragment being a ListView and the other fragments being details of the ListView. The user can either manually select an item in the ListView or swipe though the various details. I have the swipe working, but I'm not sure how to get the item selection working. I'm assuming I need to do something in onListItemClick of the ListView and I know I can use setCurrentItem of the ViewPager, but the ViewPager is in the Activity and the ListView is in the fragment so I'm not sure how to get both working.
This might be something obvious, but I'm new to using a ViewPager and there's doesn't seem to be alot of documentation out there, mostly tutorials on getting a basic ViewPager working.
You have three ways of setting up fragment/activity communication:
You can define an interface aka a Listener that the activity implements.
Reference: http://developer.android.com/guide/components/fragments.html Check out the section called "Creating event callbacks to the activity"
Use an EventBus library such as Otto or EventBus to create a publisher/subscriber system where your activity is the subscriber and your fragments publish events.
You can use the LocalBroadcastManager to send out a message through intents that the activity intercepts by creating a BroadcastReceiver with an action string specified by you. Heres an example of a typical use case: how to use LocalBroadcastManager?

Categories

Resources