android - Open activity or dialog box when i press image i have in my viewPAger -


in root activity have slide show displayed viewpager . in case want open new activity or dialog box or when pressed image on slide show . used onclicklistener , assign picture id , don't feedback .

rootactivity.java

    public class rootactivity extends activity {         @override         protected void oncreate(bundle savedinstancestate) {              super.oncreate(savedinstancestate);             setcontentview(r.layout.activity_root);          // image slide show          viewpager viewpager = (viewpager) findviewbyid(r.id.view_pager);         imageadapter adapter = new imageadapter(this);         viewpager.setadapter(adapter);         viewpager.setcurrentitem(0);         imageadapter.settimer(viewpager, 3, 0);          // end image slide show          viewpager vp = (viewpager)findviewbyid(r.drawable.ax1);         vp.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 // todo auto-generated method stub                 system.out.print("ok ...");              }         }); }  } 

imageadapter.java

public class imageadapter extends pageradapter {      final static handler handler = new handler();     public static timer swipetimer ;     context context;     private int[] galimages = new int[] {         r.drawable.one,         r.drawable.two,         r.drawable.three,         r.drawable.ax1,         r.drawable.ax2,     };     imageadapter(context countdowntimer){         this.context=countdowntimer;     }     @override     public int getcount() {       return galimages.length;     }      @override     public boolean isviewfromobject(view view, object object) {       return view == ((imageview) object);     }      @override     public object instantiateitem(viewgroup container, int position) {       imageview imageview = new imageview(context);       int padding = context.getresources().getdimensionpixelsize(r.dimen.padding_medium);       imageview.setpadding(padding, padding, padding, padding);       imageview.setscaletype(imageview.scaletype.center_inside);       imageview.setimageresource(galimages[position]);       ((viewpager) container).addview(imageview, 0);       return imageview;     }      @override     public void destroyitem(viewgroup container, int position, object object) {       ((viewpager) container).removeview((imageview) object);      }       public static void settimer(final viewpager mypager, int time, final int curpage){          final runnable update = new runnable() {             int num_pages =5;             int currentpage = curpage ;             public void run() {                 if (currentpage == num_pages ) {                     currentpage = 0;                 }                 mypager.setcurrentitem(currentpage++, true);             }         };          swipetimer = new timer();         swipetimer.schedule(new timertask() {              @override             public void run() {                 handler.post(update);             }         }, 1000, time*1000);  } } 

okay, first things first...

1. line, in rootactivity.oncreate() not make lot of sense:

viewpager vp = (viewpager)findviewbyid(r.drawable.ax1);

you're trying find viewpager object in layout using drawable resource id. delete line , in oncreate() comes after it.

2. can attach onclicklistener existing view , since there no onitemclicklistener pageradapters, need attach onclicklistener appropriate views within adapter's instantiateitem() method.

i have attached modified version of code representing these points.

rootactivity.java

public class rootactivity extends actionbaractivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_root);         // image slide show         viewpager viewpager = (viewpager) findviewbyid(r.id.view_pager);         imageadapter adapter = new imageadapter(this);         viewpager.setadapter(adapter);         viewpager.setcurrentitem(0);         imageadapter.settimer(viewpager, 3, 0);         // end image slide show     } } 

imageadapter.java

look the // begin added spacelord comments

public class imageadapter extends pageradapter {      final static handler handler = new handler();     public static timer swipetimer;     context context;     private int[] galimages = new int[]{             r.drawable.one,             r.drawable.two,             r.drawable.three,             r.drawable.ax1,             r.drawable.ax2,     };      imageadapter(context countdowntimer) {         this.context = countdowntimer;     }      @override     public int getcount() {         return galimages.length;     }      @override     public boolean isviewfromobject(view view, object object) {         return view == object;     }      @override     public object instantiateitem(viewgroup container, int position) {         imageview imageview = new imageview(context);         int padding = context.getresources().getdimensionpixelsize(r.dimen.padding_medium);         imageview.setpadding(padding, padding, padding, padding);         imageview.setscaletype(imageview.scaletype.center_inside);         imageview.setimageresource(galimages[position]);          // begin added spacelord         imageview.setonclicklistener(itemclicklistener);         imageview.settag(position);         // end added spacelord          container.addview(imageview, 0);         return imageview;     }      // begin added spacelord     private final view.onclicklistener itemclicklistener = new view.onclicklistener() {         @override         public void onclick(view v) {             final int tag = (integer) v.gettag();             switch (tag) {                 // whatever need each slide click here                 case 0:                     log.d(imageadapter.class.getsimplename(), "clicked on one");                     break;                 case 1:                     log.d(imageadapter.class.getsimplename(), "clicked on two");                     break;                 case 2:                     log.d(imageadapter.class.getsimplename(), "clicked on three");                     break;                 case 3:                     log.d(imageadapter.class.getsimplename(), "clicked on ax1");                     break;                 case 4:                     log.d(imageadapter.class.getsimplename(), "clicked on ax2");                     break;             }         }     };     // end added spacelord      @override     public void destroyitem(viewgroup container, int position, object object) {         container.removeview((imageview) object);      }       public static void settimer(final viewpager mypager, int time, final int curpage) {          final runnable update = new runnable() {             int num_pages = 5;             int currentpage = curpage;              public void run() {                 if (currentpage == num_pages) {                     currentpage = 0;                 }                 mypager.setcurrentitem(currentpage++, true);             }         };          swipetimer = new timer();         swipetimer.schedule(new timertask() {              @override             public void run() {                 handler.post(update);             }         }, 1000, time * 1000);      } } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -