Android: Detect when Fragment from ViewPager gets visible inside Activity - android

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.

Related

how to call method form inside fragment holded by view pager using interface?

on my app I have a button in the mainActivity and viewPager2 with some fragments where each fragment has textsViews that must be field with information.
I want to check if the fields are filled when I click on the button " the activity button"
I'm trying to use interfaces to fire the click event inside the fragment but I don't know how
I tried this answer Dynamically updating a fragment but it doesn't fit my problem

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.

Should a fragment be able to change the State of a View inside the host Activity?

If I need to hide a view inside the Activity when a certain fragment is inflated, is it ok to let the Fragment do the State Change?
For example I have a three Fragments (FragmentA, FragmentB, FragmentC) and one Activty. The Activity have a BottomNavigation View but its visibility should be set to Gone if FragmentB is inflated inside the Activity.
If I placed the managing of the BottomNavigation Visibility inside the fragment then, I am sure that whenever that fragment is inflated the view will certainly be set to gone.
My only problem is that, if there come a time that I need to reuse that fragment and show the BottomNavigation at the same time. I wont be able to do so because the Fragment will automatically set the Visibility of the BottomNavigation to Gone.
Can anyone give me some tips? Thanks in advance.
In your case, do not control the visibility of the BottomNavigation inside the Fragment, do it inside the Activity with a callback.
read about callback this in part "Creating event callbacks to the activity"
Fragments should be self-sufficient and should not know anything about other Fragments and Activites.

GlobalLayoutLIstener behavior for Fragment?

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?

Android: ViewPager, intercept on fragment load

I have a ViewPager which usually preloads some of its elements.
The user can interact with one page which should affects a part of all other elements of the ViewPager.
Currently all my logic is in onCreateView(). I need to get a handle when element is actually shown and change the common part.
I tried with onResume() but this function is called earlier. I don't want to redraw the whole element because this is not effective.
I am not sure but try using this method:
viewPager.setOffscreenPageLimit(0);
This method will call only one fragment at a time, while you are on that fragment.
Or
Another way is that you can get position of view pager's fragment at run time which is front of screen and according to that particular fragment position you can simply call your method by checking position Like: if(position == 1){}.

Categories

Resources