編輯:中階開發
除了加入活動條目 Action Items 和活動視圖 Action View 之外,Android 的活動欄 ActionBar 今天要和大家一起看的是:支援多 Fragment 切換的標籤頁,以及下拉導航。
要在 ActionBar 中實現標籤頁,可以實作 android.app.ActionBar.TabListener,並重寫 onTabSelected、onTabUnselected 與 onTabReselected 三個方法來關聯 Fragment,程式碼如下:
private class MyTabListener implements ActionBar.TabListener {
private TabContentFragment mFragment;
// Called to create an instance of the listener when adding a new tab
public TabListener(TabContentFragment fragment) {
mFragment = fragment;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragment_content, mFragment, null);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// do nothing
}
}
接著我們在 Activity 中建立 ActionBar,程式碼如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ActionBar actionBar = getActionBar(); //Android 開發網提示 getActionBar 方法一定要在 setContentView 後面
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
Fragment artistsFragment = new ArtistsFragment();
actionBar.addTab(actionBar.newTab().setText(R.string.tab_artists)
.setTabListener(new TabListener(artistsFragment)));
Fragment albumsFragment = new AlbumsFragment();
actionBar.addTab(actionBar.newTab().setText(R.string.tab_albums)
.setTabListener(new TabListener(albumsFragment)));
}
先建立一個 SpinnerAdapter 提供下拉選項。和 Tab 方式不同的是,Drop-down 只需把 setNavigationMode 的模式由 ActionBar.NAVIGATION_MODE_TABS 改成 ActionBar.NAVIGATION_MODE_LIST,改進後的程式碼為:
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);//應在 Activity 的 onCreate() 方法中執行
actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);
上面我們透過 setListNavigationCallbacks 方法綁定一個 SpinnerAdapter 控制項,具體的 OnNavigationListener 程式碼範例為:
mOnNavigationListener = new OnNavigationListener() {
String[] strings = getResources().getStringArray(R.array.action_list);
@Override
public boolean onNavigationItemSelected(int position, long itemId) {
ListContentFragment newFragment = new ListContentFragment();
FragmentTransaction ft = openFragmentTransaction();
ft.replace(R.id.fragment_container, newFragment, strings[position]);
ft.commit();
return true;
}
};
而其中的 ListContentFragment 程式碼為:
public class ListContentFragment extends Fragment {
private String mText;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mText = getTag();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setText(mText);
return text;
}
}
有關 Android ActionBar 的四種方式我們已經大致了解,具體的示例工程程式碼,明天 Android 開發網繼續為大家解析。
Android 的 Java 虛擬機原理與記憶體分配:看懂 GC 日誌與 Heap 分析
想開發出高效穩定的 Android 應用,繞不開 Java 虛擬機的原理與記憶體分配機制。Android 使用的是 Google 針對行動裝置最佳化過的 Dalvik V
Linux BFS 調度器解析:設計理念、效能爭議與實作原理
摘要:BFS 是一款專為 Linux 桌面環境設計的核心調度器,基於 Staircase Deadline 與 EEVDF 演算法,支援 Linux 2.6.31 之後的
HTML 5 本地儲存實戰:讓行動 Web 應用擺脫 4KB 的枷鎖
HTML 5 最實用的新能力之一,就是把「用戶端儲存」這件事真正標準化。過去十幾年,Web 開發者只能把資料硬塞進 4KB 的 Cookie 裡;現在,透過一組再簡單不過
混合式應用程式模型打造 WebSphere Commerce 行動 App(下)
接續上篇,本文說明 iPhone 版本的四個進階能力,並完整介紹 Android 版本的實作方式。 可翻頁的產品詳情畫面 為了讓使用者能左右滑動瀏覽產品,我們自訂了一個