Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

ActionBar實現下拉式導航的兩步做法

編輯:關於 Android 程式設計

ActionBar 除了 Tab 導航,也能做成下拉列表式的導航,適合選項較多、橫向空間不夠的場景。實現只需兩步:

1) 設定 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST)。
2) 呼叫 setListNavigationCallbacks(SpinnerAdapter adapter, ActionBar.OnNavigationListener callback)。

MyFragment.java

每個下拉項對應一個 Fragment,透過 arguments 接收位置參數:

 package ccom.app.main;

 import android.annotation.SuppressLint;
 import android.app.Fragment;
 import android.content.Context;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewGroup.LayoutParams;
 import android.widget.TextView;

 @SuppressLint("NewApi")
 public class MyFragment extends Fragment {

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
         Context context = this.getActivity();
         TextView tv = new TextView(context);
         Bundle arc = this.getArguments();
         int tabs = arc.getInt("key");
         tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                 LayoutParams.WRAP_CONTENT));
         tv.setText("hello actionbar " + tabs);
         return tv;
     }
 }

main.xml

一個空容器,用來裝 Fragment:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
     xmlns:tools="https://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".Main" >
     <LinearLayout
         android:id="@+id/content"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical" />
 </RelativeLayout> 

mytextview.xml

下拉列表項的樣式,用系統提供的 textAppearanceListItemSmall 與 listPreferredItemHeightSmall 保持與系統一致的觀感:

 <?xml version="1.0" encoding="utf-8"?>
 <TextView xmlns:android="https://schemas.android.com/apk/res/android"
     android:id="@+id/text1"
     android:textColor="#fff"
     android:background="#696969"
     android:layout_width="60sp"
     android:layout_height="match_parent"
     android:textAppearance="?android:attr/textAppearanceListItemSmall"
     android:gravity="center_vertical"
     android:paddingStart="?android:attr/listPreferredItemPaddingStart"
     android:minHeight="?android:attr/listPreferredItemHeightSmall" /> 

Main.java

 package ccom.app.main;

 import android.annotation.SuppressLint;
 import android.app.ActionBar;
 import android.app.Activity;
 import android.app.FragmentTransaction;
 import android.os.Bundle;
 import android.widget.ArrayAdapter;

 @SuppressLint("NewApi")
 public class Main extends Activity implements ActionBar.OnNavigationListener {

     ActionBar actionBar = null;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         actionBar = this.getActionBar();
         actionBar.setDisplayShowTitleEnabled(true);
         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

         actionBar.setListNavigationCallbacks(
                 new ArrayAdapter(Main.this, R.layout.mytextview, R.id.text1,
                         new String[]{"tab1", "tab2", "tab3"}),
                 this);
     }

     @Override
     public boolean onNavigationItemSelected(int itemPosition, long itemId) {
         MyFragment mf = new MyFragment();
         Bundle bundle = new Bundle();
         bundle.putInt("key", itemPosition + 1);
         mf.setArguments(bundle);

         FragmentTransaction action = this.getFragmentManager().beginTransaction();
         action.replace(R.id.content, mf);
         action.commit();
         return true;
     }
 }

效果圖:

對比 Tab 導航,兩者的差別主要在回調方式:Tab 導航透過 ActionBar.TabListener 的 onTabSelected 取得 tab.getPosition();下拉式導航則由 OnNavigationListener.onNavigationItemSelected 直接給出 itemPosition。其餘 Fragment 替換邏輯完全一致,因此兩種模式之間切換的成本很低。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved