Android:adding swipe functionality to existing tab menu? -
is possible add swipe functionality existing tab menu? i.e. swipe left or right move between tabs rather clicking on tab.
i have exisiting 3 tab menu , not want alter it's structure, love add swipe functionality.
how can so?
main menu/tabhost:
public class mainmenu extends tabactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tabhost tabhost = (tabhost)findviewbyid(android.r.id.tabhost); tabspec tab1 = tabhost.newtabspec("games"); intent tab1intent = new intent(this, firstactivity.class); tab1.setcontent(tab1intent); tab1.setindicator("games"); tabspec tab2 = tabhost.newtabspec("search"); intent tab2intent = new intent(this, secondactivity.class); tab2.setcontent(tab2intent); tab2.setindicator("search"); tabspec tab3 = tabhost.newtabspec("summary"); intent tab3intent = new intent(this, thirdactivity.class); tab3.setcontent(tab3intent); tab3.setindicator("summary"); tabhost.addtab(tab1); tabhost.addtab(tab2); tabhost.addtab(tab3); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } } example of tab (middle tab):
public class secondactivity extends actionbaractivity implements view.onclicklistener { textview instruction; button date; button game; button med; button att; button score; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.second_tab); initialisevars(); } /** * initialises vars , sets onclick listeners buttons */ public void initialisevars() { // setting vars instruction = (textview) findviewbyid(r.id.tvsearchhome); date = (button) findviewbyid(r.id.btnsearchhomedate); game = (button) findviewbyid(r.id.btnsearchhomegame); med = (button) findviewbyid(r.id.btnsearchhomemedvalues); att = (button) findviewbyid(r.id.btnsearchhomeattvalues); score = (button) findviewbyid(r.id.btnsearchhomescore); // set on click listeners buttons date.setonclicklistener(this); game.setonclicklistener(this); med.setonclicklistener(this); att.setonclicklistener(this); score.setonclicklistener(this); } public void onclick(view v) { switch (v.getid()) { case r.id.btnsearchhomedate: intent opendatesearch = new intent( "com.example.brianapp.searchdate"); // start activity startactivity(opendatesearch); break; case r.id.btnsearchhomegame: intent opengamesearch = new intent( "com.example.brianapp.searchgame"); // start activity startactivity(opengamesearch); break; case r.id.btnsearchhomemedvalues: // change make sure opens med screen intent openmedsearch = new intent( "com.example.brianapp.meditationsearchhome"); // start activity startactivity(openmedsearch); break; case r.id.btnsearchhomeattvalues: // change make sure opens med screen intent openattsearch = new intent( "com.example.brianapp.attentionsearchhome"); // start activity startactivity(openattsearch); break; case r.id.btnsearchhomescore: // change make sure opens med screen intent openscoresearch = new intent( "com.example.brianapp.searchscore"); // start activit startactivity(openscoresearch); break; }// switch end } }
i can provide following snippet. please note haven't tested it. wanted show how approach problem.
public class mainmenu extends tabactivity implements gesturedetector.ongesturelistener{ ... @override public boolean onfling(motionevent e0, motionevent e1, float arg2, float arg3) { ... int current = getcurrenttab(); // tabhost.getcurrenttab() int next; if(e0.getrawx() - e1.getrawx() < 0 ){ //fling right next = current + 1; //todo check next = n, n=number of tabs } else{ next = current - 1; //todo check next = -1 } setcurrenttab(next); // tabhost.setcurrenttab() } }
Comments
Post a Comment