Android编程之ActionBar Tabs用法实例分析
程序员文章站
2023-11-17 12:13:52
本文实例讲述了android编程之actionbar tabs用法。分享给大家供大家参考,具体如下:
这里主要实现用tab切换不同的fragment,点击view显示or...
本文实例讲述了android编程之actionbar tabs用法。分享给大家供大家参考,具体如下:
这里主要实现用tab切换不同的fragment,点击view显示or隐藏actionbar,把actionbar 设为透明,使界面更加友好,详细代码见资源里的actionbartabs。
actionbar tab主要用于fragment之间的切换,其必须要设置actionbar.tablistener,详细代码如下
actionbaractivity.java:
import android.app.actionbar; import android.app.activity; import android.app.fragmenttransaction; import android.app.actionbar.tab; import android.os.bundle; import android.os.countdowntimer; import android.view.motionevent; import android.view.window; public class actionbaractivity extends activity { /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //使actionbar变得透明 requestwindowfeature(window.feature_action_bar_overlay); setcontentview(r.layout.main); final actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // remove the activity title to make space for tabs actionbar.setdisplayshowtitleenabled(false); afragment afragment = new afragment(); actionbar.addtab(actionbar.newtab().settext("tab-a") .settablistener(new listenera(afragment))); bfragment bfragment = new bfragment(); actionbar.addtab(actionbar.newtab().settext("tab-b") .settablistener(new listenerb(bfragment))); } //点击显示or隐藏actionbar public boolean ontouchevent(motionevent event){ actionbar bar = getactionbar(); switch(event.getaction()){ case motionevent.action_up: if(bar.isshowing()) bar.hide(); else bar.show(); break; default: break; } return true; } private class listenera implements actionbar.tablistener { private afragment mfragment; // called to create an instance of the listener when adding a new tab public listenera(afragment fragment) { mfragment = fragment; } public void ontabselected(tab tab, fragmenttransaction ft) { ft.add(r.id.fragment, mfragment, null); } public void ontabunselected(tab tab, fragmenttransaction ft) { ft.remove(mfragment); } public void ontabreselected(tab tab, fragmenttransaction ft) { // do nothing } } } private class listenerb implements actionbar.tablistener { private bfragment mfragment; // called to create an instance of the listener when adding a new tab public listenerb(bfragment fragment) { mfragment = fragment; } public void ontabselected(tab tab, fragmenttransaction ft) { ft.add(r.id.fragment, mfragment, null); } public void ontabunselected(tab tab, fragmenttransaction ft) { ft.remove(mfragment); } public void ontabreselected(tab tab, fragmenttransaction ft) { // do nothing } } } }
其中涉及到两个fragment,在前面fragment的笔记中讲过,这里就不再赘述。类afragment实现如下,bfragment实现与这类似:
public class afragment extends fragment { public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.alayout, container, false); } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。