編輯:關於 Android 開發
上一節講完了 ActionBar 的概述與建立,這一節要讓它真正變得好用:加上 Tabs 標籤頁,以及下拉式導航(Drop-down Navigation)。這兩者都是 ActionBar 作為「導航元件」的核心能力。
要在 ActionBar 裡實作標籤頁,做法是實作 android.app.ActionBar.TabListener,並重寫 onTabSelected、onTabUnselected、onTabReselected 三個方法,用它們來關聯 Fragment。程式碼如下:
private class MyTabListener implements ActionBar.TabListener {
private TabContentFragment mFragment;
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) {
}
}接著在 Activity 裡建立 ActionBar 並掛上 Tab:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ActionBar actionBar = getActionBar(); // 注意: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)));
}這裡有個新手常踩的坑:getActionBar() 必須在 setContentView() 之後呼叫,否則會拿到 null。
下拉導航的實作方式與 Tab 類似,但要多準備一個 SpinnerAdapter 來提供下拉選項。差異主要在 setNavigationMode() 的模式:把 ActionBar.NAVIGATION_MODE_TABS 換成 ActionBar.NAVIGATION_MODE_LIST 即可。調整後的程式碼為:
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
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;
}
}小技巧:這裡透過 getTag() 取出 Fragment 的標籤並直接當成顯示文字,因為前面在 ft.replace() 時已把字串陣列中對應的項目作為 tag 傳入。這種做法能省下一份 Bundle 傳值的工,很適合用來做快速原型。
整體來說,Tab 適合「同一層級、數量固定」的分類切換;下拉導航則適合選項較多、不宜全部攤在畫面上的情境。實際開發時依資訊結構挑選即可。
ListView 詳解:Android 列表控制項完整指南
ListView 詳解:Android 列表控制項完整指南 ListView 是 Android 開發中最常見的列表控制項之一,特別適合展示大量規則排列的資料。它具備記憶
手機衛士實戰:簽名文件與包名對升級安裝的影響
簽名與包名:App 升級安裝必懂的規則 在此前的文章中,我們實現了檢測新版本、提示更新以及通過 xutils 從服務端下載 apk 的功能。本文則聚焦下載完成後的安裝環節
仿 QQ 空間的 PopupWindow:依據點擊位置自動朝上或朝下彈出
要做出的效果 打開 QQ 空間時有個箭頭按鈕,點下去會彈出一個 PopupWindow,而且它會依按鈕在畫面上的位置,自動決定顯示在箭頭上方還是下方。這比一律彈在螢幕正中
ViewPager 與 Fragment 預加載問題的解法:MyLazyFragment 可見性懶加載
ViewPager 與 Fragment 預加載問題的解法 ViewPager 與 Fragment 搭配時,預設可能會建立相鄰頁面,造成非當前頁也提前載入資料。若頁面資