ActionBar使用方法 - Android活动栏(二)

有关Android活动栏-ActionBar的功能除了添加活动条目 Action Items和添加活动视图 Action View外今天Android123和大家一起来看下支持多Fragment切换的ActionBar标签页和下拉导航。

  三、添加标签 Tabs

   在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
    }
 
}

 接下来我们创建ActionBar在Activity中,代码如下

  @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)));
}

 四、添加下拉导航 Drop-down Navigation

 创建一个SpinnerAdapter提供下拉选项,和Tab方式不同的是Drop-down只需要修改下setNavigationMode的模式,将ActionBar.NAVIGATION_MODE_TABS改为ActionBar.NAVIGATION_MODE_LIST,最终改进后的代码为

ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);You should perform this during your activity's onCreate() method.

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开发网继续解析。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值