首页 > 代码库 > Android ActionBar下拉选项

Android ActionBar下拉选项

package com.example.actionBarTest.actionBarList;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import com.example.actionBarTest.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Heyiyong on 2014-5-2 上午9:50.
 */
public class ListNavigationActivity extends Activity implements ActionBar.OnNavigationListener {
    private List<Fragment> fragments;
    private String[] navs = {"添加", "删除", "编辑", "查询", "更多"};
    private SpinnerAdapter spinnerAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_main);

        spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, navs);

        HomeFragment homeFragment = new HomeFragment();
        MusicFragment musicFragment = new MusicFragment();
        VideoFragment videoFragment = new VideoFragment();
        fragments = new ArrayList<Fragment>();
        fragments.add(homeFragment);
        fragments.add(musicFragment);
        fragments.add(homeFragment);
        fragments.add(videoFragment);
        fragments.add(homeFragment);

        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
            actionBar.setListNavigationCallbacks(spinnerAdapter, this);
        }
    }

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        System.out.println("itemPosition = " + itemPosition);
        getFragmentManager().beginTransaction().replace(R.id.tab_main, fragments.get(itemPosition)).commit();
        return true;
    }

}

 actionBar.setListNavigationCallbacks(spinnerAdapter, this);
这个ArrayAdapter是继承于BaseAdapter的,而BaseAdapter又实现了SpinnerAdapter接口.