溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2020-09-10 14:32:26 來源:腳本之家 閱讀:135 作者:Flying_tao 欄目:移動開發(fā)

本文實例講述了Android編程之ActionBar Tabs用法。分享給大家供大家參考,具體如下:

這里主要實現(xiàn)用Tab切換不同的Fragment,點(diǎn)擊View顯示or隱藏ActionBar,把ActionBar 設(shè)為透明,使界面更加友好,詳細(xì)代碼見資源里的ActionBarTabs。

ActionBar Tab主要用于Fragment之間的切換,其必須要設(shè)置ActionBar.TabListener,詳細(xì)代碼如下

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)));
  }
  //點(diǎn)擊顯示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實現(xiàn)如下,BFragment實現(xiàn)與這類似:

public class AFragment extends Fragment {
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    return inflater.inflate(R.layout.alayout, container, false);
    }
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》

希望本文所述對大家Android程序設(shè)計有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI