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.
Related
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 want to use ViewPager to swipe through songs in my Media Player ( going forward or backward like SoundCloud app for android ).
I have created a fragment which I inflate and set inside information like song name, author etc.
I have read some topics on StackOverflow about the ViewPager displays wrong items ( Viewpager shows wrong page and Android Viewpager show wrong pages and a lot more ) but I don't have fixed number of XML's to use with the ViewPager.
My Data is displayed in a RecyclerView, when I am pressing the item on index 4 for example I call the following method:
mPager.setCurrentItem(position);
So the mPager is set to position 4.
#Override
public Fragment getItem(int position) {
Log.i("TEST","Returnin a fragment on position "+position);
return fragment = new MusicSliderFragment();
}
But on my Adapter when it's changing I get the following output:
02-23 10:39:57.131 7599-7599/************** I/TEST: Returning a fragment on position 4
02-23 10:39:57.131 7599-7599/************** I/TEST: Returning a fragment on position 3
02-23 10:39:57.131 7599-7599/************** I/TEST: Returning a fragment on position 5
And when my application is launched I get the following output:
I/TEST: Returning a fragment on position 0
I/TEST: Returning a fragment on position 1
The main problem is when I am opening the app first time and set the song on position 4 the fragment information are displayed in the next fragment.
Is there a way how can I fix this?
Adapter Class:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
private MusicSliderFragment fragment;
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Log.i("TEST","Returnin a fragment on position "+position);
return fragment = new MusicSliderFragment();
}
#Override
public int getCount() {
return NUM_PAGES;
}
public void updateSongName(String name) {
fragment.setSongName(name);
}
}
The RecyclerView Listener :
musicList.addOnItemTouchListener(
new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, final int position) {
Log.i("TEST", "Setting the mPager position to: "+position);
mPager.setCurrentItem(position);
}
}
And the `Listener` of the `mPager` where I update the song name for example:
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
play(position);
ScreenSlidePagerAdapter adapter = (ScreenSlidePagerAdapter) mPager.getAdapter();
adapter.updateSongName(splitName(myDataList.get(position))[0]);
Log.i("TEST", "Playing the position: "+position);
mPagerAdapter.notifyDataSetChanged();
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
View pager can load neighbour fragments when you choose current. Method setOffscreenPageLimit() defines how many neighbours you want to preload.
onPageSelected(int position) should work correct and returns current item.
Viewpager indexing starts from zero if you want to load the fourth Fragment then you need to set the position 3.
mPager.setCurrentItem(3); //Load the 4th fragment according to you.(0-3)
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) {
}
});
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
I am using a ViewPager, which has two pages and it is being reused. The total count what i have is 156 pages. I want to disable left swipe for page 2.
Please help me out, thanks,
Here is my code..
public class SlidePagerActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setOnPageChangeListener(new ViewPageChangeListener(){
// set current slide index
AbcController.getInstance().getSlideController().setCurrentSlide(_nNextSlideIndex);
// set Current Cycle Index
AbcController.getInstance().getSlideController().setCurrentCycle(_nNextSlideCycle);
// set pager index
_mPager_in.setCurrentItem(_nNextSlideIndex);
});
}
}
I wanted to to prevent the user to go swipe back to previous page, but didn't want the page neither to disappear (to allow him to swipe back if some condition are fulfilled).
I finally use OnPageChangeListener:
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position < pageActuelle) {
viewPager.setCurrentItem( pageActuelle );
return;
}
pageActuelle = position;
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
Now I am forcing the user to go back to the his current page when he is swiping left. This solution was really adapted to my problem since I could display a Toast when the user tries to swipe back.
Hope it could help someone.
Can you specify your problem more detailed?
You can always remove all pages before the specific postion, in that case there is nothing to swipe too. (sorry cant comment)