java - Clickable View Pager in Android -
i want set on click event in view pager. viewpageradapter
looks this-
public class viewpageradapder extends pageradapter { activity activity; string image[]; public viewpageradapder(activity act, string[] imgarra) { image = imgarra; activity = act; } public int getcount() { return image.length; } public object instantiateitem(view collection, final int position) { imageview view = new imageview(activity); view.setlayoutparams(new viewgroup.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent)); new imagedownloadertask(view).execute(image[position]); view.setscaletype(imageview.scaletype.center_crop); view.setadjustviewbounds(true); ((viewpager) collection).addview(view, 0); // view.setbackgroundresource(imagearray[position]); return view; } @override public void destroyitem(view arg0, int arg1, object arg2) { ((viewpager) arg0).removeview((view) arg2); } @override public boolean isviewfromobject(view arg0, object arg1) { return arg0 == ((view) arg1); } @override public parcelable savestate() { return null; } }
and setting onclicklistener
in main activity
mypager = (viewpager) view.findviewbyid(r.id.viewpager); mypager.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(getactivity(), "ckecking success", toast.length_short).show(); } }
my viewpager works fine. tried implement onclicklistener in both main activity in viewpageradapter class. not work either way.
use class view pager click.
clickableviewpager.java
public class clickableviewpager extends viewpager { private onitemclicklistener monitemclicklistener; public clickableviewpager(context context) { super(context); setup(); } public clickableviewpager(context context, attributeset attrs) { super(context, attrs); setup(); } private void setup() { final gesturedetector tapgesturedetector = new gesturedetector(getcontext(), new tapgesturelistener()); setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { tapgesturedetector.ontouchevent(event); return false; } }); } public void setonitemclicklistener(onitemclicklistener onitemclicklistener) { monitemclicklistener = onitemclicklistener; } public interface onitemclicklistener { void onitemclick(int position); } private class tapgesturelistener extends gesturedetector.simpleongesturelistener { @override public boolean onsingletapconfirmed(motionevent e) { if(monitemclicklistener != null) { monitemclicklistener.onitemclick(getcurrentitem()); } return true; } } }
click event
clickablepager.setonitemclicklistener(new clickableviewpager.onitemclicklistener() { @override public void onitemclick(int position) { } });
Comments
Post a Comment