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
Related
Facing issue while inflating different Menu in different fragment.
My application hierarchy is like:
BottomNavigationView --> Fragments(4) ---> Tab+ViewPager --> Fragments(3)
Each fragment contains ViewPager and ViewPager contains multiple fragments.
Please see the attached image for more clarity.
Issue: It's keep on adding new menu, or sometimes carry previous screen menu.
I have tried using "menu.clear()", "getActivity().invalidateOptionsMenu();"
You have ViewPager, it means that if you go to ith position, fragments that are on position i-1 and i+1 will also be created. So their onCreateOptionsMenu will be called and even if you call menu.clear() it will not work properly.
Solution is to change menu in container Fragment/Activity of ViewPager , in following way. Add ViewPager.OnPageChangeListener listener to ViewPager and in onPageSelected(int position) method change menu yourself:
#Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
int menuResourceId = getMenuResourceByPosition(position);
menu.clear();
inflater.inflate(menuResourceId, menu);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
create method getMenuResourceByPosition that will give you proper menu resource id by ViewPager's selected position.
I am using TabLayout in my project which might have more than 10 TabItems. Now I wanted to hide an other view when the user scroll the TabLayout.
Note: I am not using any ViewPager.
Every instance of View calls getViewTreeObserver(). You can add an OnScrollChangedListener() to it by using addOnScrollChangedListener().
In your case:
tabLayout.getViewTreeObserver().addOnScrollChangedListener(() -> {
int scrollX = tabLayout.getScrollX(); // Current x scrolling position
// We know that we have at least one child
int maxScrollWidth = categoryTabLayout.getChildAt(0).getMeasuredWidth() - windowSize.x;
// Do whatever you want here
});
windowSize is a Point accessed from the WindowManager
private Point windowSize = new Point();
// Calculate the position if this window
getActivity().getWindowManager().getDefaultDisplay().getSize(windowSize);
By using the approach above you will have updates about the proper scroll position you need
TabLayout has its own functionality for tab change listener callback.
Use
onPageChange Listener:
TabLayout.TabLayoutOnPageChangeListener(TabLayout tabLayout)
Check this link: OnTabChange Listener for more information
onTabSelected Listener:
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab){
int position = tab.getPosition();
}
});
Check for more: onTabSelected Listener
If you're using your TabLayout with a ViewPager like this:
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
Then you can use:
viewPager.setOnPageChangeListener(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){
}
});
And in the method onPageSelected(), you can do the action you want depending on the tab showing.
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 have Nested Fragments [1 main Fragment called "MainFrag" and many child Fragments]
the App can show MainFrag or can show other fragments.
what I want to do is to setCurrentItem of MainFrag's ViewPager every time the user selects to show that MainFrag so I tried to put it at it's onCreateView but this only works for the first time the User Shows MainFrag as if user Selects another fragment and then re-selects MainFrag the seCurrentItem is not working
I also tried to put it inside MainFrag's onResume, It worked as required but it also causes a problem that if the user moved the App to background and then reOpen it viewPager will Scroll and i don't want this
So where Exactly Can I put setCurrentItem ?
mViewPager.setCurrentItem(mCurrentWeekFragmentItemNumber);
Edit: I am using Navigation Menu to Move between 3 main fragments where MainFrag is one of them.
After your mViewPager.setAdapter(adapter); method you can put
mViewPager.setCurrentItem(mCurrentWeekFragmentItemNumber);
After setting adapter to viewpager add setCurrentItem method
mViewPager.setAdapter(adapter);
mViewPager.setCurrentItem(mCurrentWeekFragmentItemNumber);
Try this:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
viewPager.setCurrentItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
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) {
}
});