java - onClick not working in viewpager android -
i have made slideshow using viewpager , disabled default swipe functionality. instead want change slide when clicked on image.so add code in viewpageradapter class when click on image doesnt anything. viewpageradapter class is:
package com.dekton.dektonapp; import android.content.context; import android.graphics.typeface; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.animation.animation; import android.view.animation.animationutils; import android.view.animation.linearinterpolator; import android.view.animation.translateanimation; import android.widget.imageview; import android.widget.relativelayout; import android.widget.textview; public class viewpageradapter extends pageradapter implements animation.animationlistener { //declare variables context context; int[] background; int[] icon; string[] title; string[] title_2; string[] description; layoutinflater inflater; public viewpageradapter(context context, int[] background, int[] icon, string[] title, string[] title_2, string[] description) { this.context = context; this.background = background; this.icon = icon; this.title = title; this.title_2 = title_2; this.description = description; } @override public boolean isviewfromobject(view view, object object) { return view == ((relativelayout) object); } @override public int getcount() { return background.length; } @override public object instantiateitem(viewgroup container, final int position) { //declare variables imageview iconimage, whitebox, bgimage, slidedownimage; textview titletext, title_2text, descriptiontext; final viewpager viewpager; animation translate, slide; inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view itemview = inflater.inflate(r.layout.slidescreen_item, container, false); //locate textviews in slidescreen_item.xml titletext = (textview) itemview.findviewbyid(r.id.title); title_2text = (textview) itemview.findviewbyid(r.id.title_2); descriptiontext = (textview) itemview.findviewbyid(r.id.description); //capture position , set textviews titletext.settext(this.title[position]); title_2text.settext(this.title_2[position]); descriptiontext.settext(this.description[position]); //locate imageview in slidescreen_item.xml iconimage = (imageview) itemview.findviewbyid(r.id.icon); whitebox = (imageview) itemview.findviewbyid(r.id.whitebox); bgimage = (imageview) itemview.findviewbyid(r.id.bgimage); //capture position , set imageview bgimage.setbackgroundresource(this.background[position]); iconimage.setimageresource(this.icon[position]); //apply font typeface bryantprobold = typeface.createfromasset(this.context.getassets(), "bryantprobold.otf"); typeface pfhighwaysansproregular = typeface.createfromasset(this.context.getassets(), "pfhighwaysansproregular.ttf"); titletext.settypeface(pfhighwaysansproregular); title_2text.settypeface(pfhighwaysansproregular); descriptiontext.settypeface(bryantprobold); //apply animations translate = new translateanimation(translateanimation.absolute, 0f, translateanimation.absolute, 0f, translateanimation.absolute, -48f, translateanimation.absolute, 150f); translate.setduration(5000); translate.setrepeatcount(-1); translate.setrepeatmode(animation.reverse); translate.setinterpolator(new linearinterpolator()); bgimage.setanimation(translate); slide = animationutils.loadanimation(this.context, r.anim.whiteboxanim); slide.setanimationlistener(this); whitebox.startanimation(slide); iconimage.startanimation(slide); titletext.startanimation(slide); title_2text.startanimation(slide); descriptiontext.startanimation(slide); viewpager = (viewpager) itemview.findviewbyid(r.id.pager); slidedownimage = (imageview) itemview.findviewbyid(r.id.slidedownimage); slidedownimage.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (viewpager.getcurrentitem() < viewpager.getchildcount()) { viewpager.setcurrentitem(viewpager.getcurrentitem() + 1, true); } else { viewpager.setcurrentitem(0); } } }); ((viewpager) container).addview(itemview); return itemview; } @override public void destroyitem(viewgroup container, int position, object object) { //remove slidescreen_item.xml viewpager ((viewpager) container).removeview((relativelayout) object); } @override public void onanimationstart(animation animation) { } @override public void onanimationend(animation animation) { } @override public void onanimationrepeat(animation animation) { } }
thanks :)
<imageview ... android:clickable="true" />
check this. if want set onclick listener on imageview, should set clickable true. hope helps. :)
Comments
Post a Comment