I have a ViewPager that contains some Fragments.
I need to update the title on the Activity Toolbar when the fragment changes.
In which lifecycle method of the Fragment should I call the Activity to set the new title?
It would be nice to use setUserVisibleHint(true), but sometimes it gets called when the Activity is not actually ready (Toolbar is null).
And additionally I need to manage the config change and need to makes sure that the title in the Toolbar is the one of the Fragment currently displayed.
Thanks
I think the better option is do that in onPageSelected()
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Related
I have a viewpager and used fragmentpageradapter, now I have 3 fragments and in every fragment I need to make a http request (i.e, hit a api),
I don't want to hit all the api for 3 fragments at a time instead I require to hit the api only when the respective page is the current item visible.
I have already tried setting offscreenlimit, but it did not help. What else can I try?
viewPager.setOffscreenLimit(0);
This can be easily done by using OnPageChangeListener provided by ViewPager class
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// communicate with Fragment here
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
First remove the API calls from onCreateView of fragments, then use this event listener to check which page is selected, according to the selected position you can communicate to the fragment to call the required API.
Use this in your Fragment
private boolean hasBeenVisibleOnce = false;
#Override
public void setUserVisibleHint(boolean visible) {
super.setUserVisibleHint(true);
if (this.isVisible()) {
if (visible && !hasBeenVisibleOnce) {
new NetCheck().execute();
hasBeenVisibleOnce= true;
}
}
}
you can use setUserVisibleHint to check if a fragment is visible to the user. You want to add another boolean (called hasBeenVisibleOnce) to determine if the fragment has been visible at least once. This is to prevent the HTTP Request to be fired multiple times
Consider there is an activity with a title. Viewpager fragment is inside the activity. Fragment will be loaded with data from network API.
Now I need to update the activity title after getting the data from API.
Problem:
Since Viewpager is used, it loads the (prev), current, (next) fragments too. So activity is not aware of which title it is for.
Each title is different for each fragment, so I need to update the title only when the user views that fragment.
You need to create object of your Activity, By using this Object you are able to use activity method.
For more detail; visit this link.How to use method from another Class?
Use this with addOnPageChangeListener method of pager, in it you will get your current postion of pager.
In you Activity add the listener on ViewPager:
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
#Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
#Override public void onPageSelected(int position)
{
//You can use switch case too
if (position== 0)
{
//set title
}
else if (position== 1)
{
//set title
}...
}
#Override public void onPageScrollStateChanged(int state)
{
}
});
You can call the Activity method from Fragment by
Make method in Activity public
In fragment use getActivty() -> cast it to corresponding Activity then call this method
If you want to call Activity method only when fragment visible you can try https://stackoverflow.com/a/42019934/5381331
In Activity, you want to get the current visible fragment, you can try https://stackoverflow.com/a/47789367/5381331
Well, I think the title is quite self explanatory. I have a ViewPager in my HomeActivity, the ViewPager contains 5 fragments at the moment.
When one of the fragments is visible by calling ViewPager's onPageScrolled I want to modify some views inside the current displayed fragment according to some conditions in the HomeActiviy.
After some research, it seems like I cannot find a good way to communicate in the direction HomeActivity --> Fragments inside ViewPager.
I have easily solved the communication in the direction Fragments in ViewPager --> HomeActivity using an Interfacebut this trick seems to not be working on the other direction.
I can access each time the current displayed fragment using a method on my FragmentStatePagerAdapter
public Fragment getActiveFragment(int position){
return myFragmentsList.get(position);
}
However, by doing that, I would have to cast each Fragment into its class MyFragment1 MyFragment2 MyFragment3....
Any easy clean way to achieve that?. Here is the relevant portion of the code:
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (currentStatus == 1){
Fragment activeFrag = mPagerAdapter.getActiveFragment(mPager.getCurrentItem());
//here I would like to modify one of the 5 fragments
}
#Override
public void onPageSelected(int position) {}
#Override
public void onPageScrollStateChanged(int state) {}
});
Define an interface,
interface FragmentListenerInterface {
public void onFragmentSelected();
}
Implement this interface in all of your fragments. change the communication between fragment and viewpager. write a method to return fragmentlistenerinterface instead of fragment.
public FragmentListenerInterface getActiveFragment(int position){
return (FragmentListenerInterface) myFragmentsList.get(position);
}
After this just say, in your code
public void onPageSelected(int position) {
mViewPager.getActiveFragment(position).onFragmentSelected();
}
And implement onFragmentSelected() in every fragment.
I'm beginner with Android, I use ViewPager with 5 fragments, and I want to show position of fragment like "1 of 5" when fragment 1 is selected, how can I do?
Thanks for all support!
you can use viewPager.getCurrentItem() to get current item of view pager and then you can use that value in textview as
textview.setText(viewPager.getCurrentItem()+" of 5");
You can add to your ViewPager a OnPageChangeListener, onPageSelected(int position) will be called when a page is shown in the pager. There you should access your pages indicator in order to change the current page.
addOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Not used
}
#Override
public void onPageSelected(int position) {
// Logic to update indicator labels.
}
#Override
public void onPageScrollStateChanged(int state) {
// Not used
}
});
You can have a textview at the bottom of your viewpager and there you can use
viewpager.getCurrentItem();
If you have a pageListener attached to your viewpager , you can also change it in onPageSelected method.
I all want is when I slide to other page on viewpager, specified navigaton drawer item should be checked. I know that I have to do it in viewpager listener, but how? I couldnt manage it. Thanks in advance.
You need to add addOnPageChangeListener to your ViewPager and in your onPageSelected(int position) you have to enable the checked state of your navigation menu item.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
navigationView.getMenu().getItem(0).setChecked(true); // where 0,1,2.. etc. are the indexes/positions for your menu items
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
So based on the viewpager tab selected you want to make the navigation drawer item selected so you can do the following:
1.Get onTabSelected() listener
2.inside that method check which tab is selected
3.Based on the item selected you can use this method navigationView.getMenu().getItem(0).setChecked(true);
Comment below if you need any further info