Reputation: 6023
I want to know the current page position of the ViewPager
. I create an adapter and set this adapter to the ViewPager
. Now I want to know the current page number or position of the current view, because I want to start some extra events on that particular page.
I used viewPager.setCurrentItem(1);
for setting the first item.
Is there a similar method for getting the current page?
Upvotes: 87
Views: 122388
Reputation: 126563
The setOnPageChangeListener() method is deprecated. Use addOnPageChangeListener(OnPageChangeListener) instead.
You can use OnPageChangeListener and getting the position inside onPageSelected()
method, this is an example:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Log.d(TAG, "my position is : " + position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Or just use getCurrentItem() to get the real position:
viewPager.getCurrentItem();
Upvotes: 6
Reputation: 979
If you only want the position, vp.getCurrentItem()
will give it to you, no need to apply the onPageChangeListener() for that purpose alone.
Upvotes: 18
Reputation: 4040
You will figure out that setOnPageChangeListener is deprecated, use addOnPageChangeListener, as below:
ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if(position == 1){ // if you want the second page, for example
//Your code here
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Upvotes: 11
Reputation: 1442
For this problem Onpagechange listener is the best one But it will also have one small mistake that is it will not detect the starting time time of 0th position Once you will change the page it will starts to detect the Page selected position...For this problem I fount the easiest solution
1.You have to maintain the selected position value then use it....
2. Case 1: At the starting of the position is always Zero....
Case 2: Suppose if you set the current item means you will set that value into maintain position
3.Then do your action with the use of that maintain in your activity...
Public int maintain=0;
myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
//Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
}
@Override
public void onPageSelected( int i) {
// here you will get the position of selected page
maintain = i;
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
updateButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
data.set(maintain, "Replaced "+maintain);
myViewPager.getAdapter().notifyDataSetChanged();
}
});
Upvotes: 7
Reputation: 95
getCurrentItem()
, doesn't actually give the right position for the first and the last page I fixed it adding this code:
public void CalcPostion() {
current = viewPager.getCurrentItem();
if ((last == current) && (current != 1) && (current != 0)) {
current = current + 1;
viewPager.setCurrentItem(current);
}
if ((last == 1) && (current == 1)) {
last = 0;
current = 0;
}
display();
last = current;
}
Upvotes: 6
Reputation: 29131
in the latest packages you can also use
vp.getCurrentItem()
or
vp is the viewPager ,
pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);
you have to put a page change listener for your viewPager. There is no method on viewPager to get the current page.
private int currentPage;
private static class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
Log.i(TAG, "page selected " + position);
currentPage = position;
}
}
Upvotes: 182
Reputation: 1662
There is a method object_of_ViewPager.getCurrentItem()
which returns the position of currently Viewed page of view pager
Upvotes: 47
Reputation: 335
There is no any method getCurrentItem() in viewpager.i already checked the API
Upvotes: 4