android - Get Current Position of ViewPager -
in program, showing gridview , when user tap on of grid item showing viewpager
here want know, how can show current position of total position, see in image gallery
for example: show current position of viewpager total of grid items in textview
3 of 11
this trying get:
mainactivity.java: gridview
public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // set title gridview settitle("gridview"); // view grid_view.xml setcontentview(r.layout.grid_view); // set images imageadapter.java gridview gridview gridview = (gridview) findviewbyid(r.id.gridview); gridview.setadapter(new imageadapter(this)); // listening gridview item click gridview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { // launch imageviewpager.java on selecting gridview item intent = new intent(getapplicationcontext(), imageviewpager.class); // show simple toast message item position toast.maketext(mainactivity.this, "" + position, toast.length_short).show(); // send click position imageviewpager.java using intent i.putextra("id", position); // start imageviewpager startactivity(i); } }); }
imageviewpager.java:
public class imageviewpager extends activity { // declare variable int position; textview textview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // set title viewpager settitle("viewpager"); // view view_pager.xml setcontentview(r.layout.view_pager); // retrieve data mainactivity on item click event intent p = getintent(); position = p.getextras().getint("id"); textview = (textview) findviewbyid(r.id.textview1); imageadapter imageadapter = new imageadapter(this); list<imageview> images = new arraylist<imageview>(); // retrieve images (int = 0; < imageadapter.getcount(); i++) { imageview imageview = new imageview(this); imageview.setimageresource(imageadapter.mthumbids[i]); imageview.setscaletype(imageview.scaletype.center); images.add(imageview); } // set images viewpager imagepageradapter pageradapter = new imagepageradapter(images); viewpager viewpager = (viewpager) findviewbyid(r.id.pager); viewpager.setadapter(pageradapter); // show images following position viewpager.setcurrentitem(position); } }
you shoul set text onpageselected.
viewpager.setonpagechangelistener(new onpagechangelistener() { @override public void onpageselected(int arg0) { // todo auto-generated method stub system.out.println("current position=="+num); textview.settext((arg0+1)+" of "+ images.size()); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { // todo auto-generated method stub system.out.println("onpagescrolled"); } @override public void onpagescrollstatechanged(int num) { // todo auto-generated method stub } });