国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程之ActionBar Tabs用法實例分析

2019-12-12 03:22:32
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 包头市| 灵寿县| 汉寿县| 沙坪坝区| 北碚区| 定兴县| 额尔古纳市| 鹤山市| 南郑县| 岳池县| 广德县| 巴林右旗| 镇沅| 宜丰县| 崇文区| 定南县| 方正县| 潞西市| 曲阜市| 泾阳县| 东至县| 徐州市| 寻甸| 淳化县| 柞水县| 荆门市| 盐池县| 大邑县| 永济市| 烟台市| 法库县| 三门县| 睢宁县| 千阳县| 岳阳市| 新巴尔虎右旗| 兴和县| 阿荣旗| 莫力| 嘉善县| 利辛县|