GlobalLayoutLIstener behavior for Fragment? - android

I am using GlobalLayoutLIstener in an fragment that is associate with an Activity, and this activity has other associative fragment as well, what I am observing is global layout listener calling for every fragment, even I didn't set it in those fragment.
any one have idea how to use GlobalLayoutLIstener using single context in different-2 fragment?

Related

How can I pass a listener from a Fragment to a DialogFragment?

I'm currently working in a single activity application that is using fragments for each screen. I'm also using MVP design pattern.
Context
I have a Fragment (Fragment-A) in which I have a List of Items.
This Fragment handles the actions for each of the items. Because it has access to the presenter.
I have a DialogFragment (Fragment-B) in which you can fill some checkboxes and complete an action (This is action is handled in the Fragment-A which implements an interface for this)
I'm using a bundle to create DialogFragment. (I can't pass the listener as an argument)
What I want?
How can I pass Fragment-A as a Listener to the DialogFragment (Fragment-B), so I can call the actions from the DialogFragment?
Assuming your DialogFragment is a child fragment of the other Fragment (you're passing in getChildFragmentManager() to show()) as it should be, then your FragmentA will get a callback to onAttachFragment():
Called when a fragment is attached as a child of this fragment.
This gives you a reference to the child DialogFragment, where you can then set any listener you want.

Android: Detect when Fragment from ViewPager gets visible inside Activity

I have Activity with ViewPager. There is a button in the Activity that should change it's visibility depending on which item (fragment) is shown on the screen.
Problem: I can't access the button from within fragment because findViewById() call returns null when invoked from the inside of fragment code. I also thought I could notify activity about fragment state change but the code from the documentation is already deprecated.
Question: How to change a button within an activity when a particular fragment appears/disappears on the screen?
Note: Getting a current fragment is not a problem. I need to detect that fragment is changed.
Create a subclass of SimpleOnPageChangeListener inside your activity (e.g., as an anonymous inner class). In that subclass, override onPageSelected() and update the button visibility based on the selected page index. Then call addOnPageChangeListener() on your ViewPager, supplying an instance of the SimpleOnPageChangeListener subclass.
Now, on every page change in the pager, onPageSelected() will be called, and you can update the button visibility based on the page.

Fragment tab listener

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

Binding fragments in listview

I have a MvvmCross Android application and let's say it contains some Fragment and ViewModel. I set the ViewModel for the Fragment inside OnCreate call of the Activity which hosts the fragment as it is shown at N+1 Fragments Sample All bindings inside the fragment work fine.
I also have another Activity with a view that contains MvxListView. The row template of the listview contains the fragment I described above. How can I set the ViewModel of the fragment so that bindings work correctly?
We have found that you can't get hold of the MvxListView in the OnCreateView method to bind to the ViewModel.
We have managed to get it work work if you use OnActivityCreated method and bind your ViewModel to the View there.

Fragment basics

I am working on a application for which I have some issues with fragments. First of all , I want to know if it is possible to make one fragment inside another fragment. And the second one is when to call onCreateView() and onActivityCreated() and which is best?
Per the Android sources:
onCreateView is called by the Activity during the construction of the view hierarchy. This is where the Fragment has the opportunity to instantiate its own user interface view.
onActivityCreated is called when the Activity has been fully created and finished instantiating the view hierarchy. At this point it's safe for the Fragment to access the its views and restore itself from some saved state.
You cannot create a fragment inside another fragment. You should communicate between fragments thru activity.See http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
I didn't understand your second question clearly. But I generally leave onActivityCreated() blank without changing it. And use onCreateView() in a similar way with onCreate().

Categories

Resources