Android界面编程--使用活动条(ActionBar)--实现Tab导航

使用ActionBar结合fragment实现导航

  1,调用ActionBar的setNavigationModel(ActionBar.NAVIGATION_MODE_TABS)设置使用tabs导航

  2,调用ActionBar的addTab()添加多个Tab标签

  #################承载Fragment的Activity需要实现implements FirstFragment.OnFragmentInteractionListener这个接口,要不然就报错###############第二次碰到这个问题了

代码:包含原来的实现ActionBar中显示View组件(还需要在仔细看一些方法,细节)

 package com.example.administrator.myapplication;

 import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater; import layout.FirstFragment; public class MainActivity extends AppCompatActivity implements FirstFragment.OnFragmentInteractionListener, ActionBar.TabListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//依次添加3个标签
actionBar.addTab(actionBar.newTab().setText("first").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("second").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("third").setTabListener(this)); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.menu_main,menu);
MenuItemCompat.setShowAsAction(menu.findItem(R.id.search),MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setShowAsAction(menu.findItem(R.id.progess),MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu); } @Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Fragment fragment = new FirstFragment();
Bundle args = new Bundle();
args.putInt("zhang",tab.getPosition()+1);
fragment.setArguments(args); android.app.FragmentTransaction ft1 = getFragmentManager().beginTransaction();
ft.replace(R.id.container,fragment);
//ft.commit();//这代码不注释掉会保存############################################################################################################
} @Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { } @Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { } @Override
public void onFragmentInteraction(Uri uri) { }
}
上一篇:[转]向facebook学习,通过协程实现mysql查询的异步化


下一篇:Python--map()函数