android - how to disable viewpager adapter on touching specific views? -
i have viewpager switches between tabs when swiping left/right.in second tab, have custom views have listeners pinching , dragging when try pinch or drag, viewpager starts swipe page.
a solution comes mind disable swiping when touching specific views , swipe when touching outside views.is possible?
updated: @asok kindly provided solution. updated code wouldnt work in case post previous piece of code worked me:
public class customviewpager extends viewpager { private boolean swipeable = true; public customviewpager(context context) { super(context); } public customviewpager(context context, attributeset attrs) { super(context, attrs); } // call method in motion events when want disable or enable // should work desired. public void setswipeable(boolean swipeable) { this.swipeable = swipeable; } @override public boolean onintercepttouchevent(motionevent arg0) { return (this.swipeable) ? super.onintercepttouchevent(arg0) : false; }
lets suppose have draggable view , need disable swipping when dragging start , re enable when dragging finished in touchevent of called view:
@override public boolean ontouchevent(motionevent event) { switch(event.getaction()) { case motionevent.action_down: //disable swiping when button touched ((activityoriginal) getactivity()).setswipeable(false); //the rest of code... break; case motionevent.action_move: break; case motionevent.action_up: //re enable swipping when touch stopped //the rest of code... ((activityoriginal) getactivity()).setswipeable(true); break; } return true; }
this first thing comes mind me have custom viewpager
in which, when touch listeners notified of specific event set swipeable
boolean
in viewpager
false , set true whichever way best fits application.
public class customviewpager extends viewpager { private boolean swipeable = true; public customviewpager(context context) { super(context); } public customviewpager(context context, attributeset attrs) { super(context, attrs); } // call method in motion events when want disable or enable // should work desired. public void setswipeable(boolean swipeable) { this.swipeable = swipeable; } @override public boolean onintercepttouchevent(motionevent arg0) { return (this.swipeable) ? super.onintercepttouchevent(arg0) : false; } }
make sure change layout file show:
<com.your.package.customviewpager .. />
instead of:
<android.support.v4.view.viewpager .. />
edit 2
here setup (working above customviewpager
):
customviewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // set action bar. final actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // create adapter return fragment each of 3 // primary sections of app. msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); // set customviewpager sections adapter. mviewpager = (customviewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); mviewpager.setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); } }); // each of sections in app, add tab action bar. (int = 0; < msectionspageradapter.getcount(); i++) { actionbar.addtab(actionbar.newtab() .settext(msectionspageradapter.getpagetitle(i)) .settablistener(this)); } } public void swipeon(view v) { mviewpager.setswipeable(true); } public void swipeoff(view v) { mviewpager.setswipeable(false); }
the above shown oncreate
in mainactivity
class extends fragmentactivity
, implements actionbar.tablistener
Comments
Post a Comment