Tab导航:
ActinBar_TabNav.java
package com.example.actionbar;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.view.Menu;
@SuppressLint("NewApi")
public class ActionBar_TabNav extends Activity implements ActionBar.TabListener{
// ActionBar actionBar;
private static final String SELECTED_ITEM = "selected_item";
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// actionBar = getActionBar();
// actionBar.hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action_bar__tab_nav);
final ActionBar actionBar = getActionBar();
//设置ActionBar的导航方式:Tab导航
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//依次添加三个Tab页,并唯一三个Tab表现添加事件监听器
actionBar.addTab(actionBar.newTab().setText("第一页")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("第二页")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("第三页")
.setTabListener(this));
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
if(savedInstanceState.containsKey(SELECTED_ITEM))
{
//选中前面保存的索引对应的Fragment页
getActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(SELECTED_ITEM));
}
}
public void onSaveInstantceState(Bundle outState)
{
//将当前选中的Fragment页的索引保存到Bundle中
outState.putInt(SELECTED_ITEM, getActionBar().getSelectedNavigationIndex());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_bar__tab_nav, menu);
return true;
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
// TODO Auto-generated method stub
//创建一个新的Fragment对象
Fragment fragment = new DummyFragment();
//创建一个Bundle对象,用于向Fragment传入参数
Bundle args = new Bundle();
args.putInt(DummyFragment.ARG_SECTION_NUMBER, tab.getPosition()+1);
//想fragment传入参数
fragment.setArguments(args);
//获取FragmentTransaction对象
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
//使用fragment代替该Activity中的container组件
ft.replace(R.id.container, fragment);
//提交事务
ft.commit();
}
@Override
public void onTabUnselected(Tab tabarg0, FragmentTransaction fragmentTransaction) {
// TODO Auto-generated method stub
}
}
DummyFragment.java
package com.example.actionbar;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint("NewApi")
public class DummyFragment extends Fragment{
public static final String ARG_SECTION_NUMBER = "section_number";
//该方法的返回值就是该Fragment显示的View组件
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstantceState)
{
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.START);
//获取创建该Fragment时传入的参数Bundle
Bundle args = getArguments();
//设置TextView显示的文本
textView.setText(args.getInt(ARG_SECTION_NUMBER) + "");
textView.setTextSize(30);
//返回该TextView
return textView;
}
}
代码运行图片
用如下DummyFragment可以自定义每页显示的内容,如果要想显示不同的图片道理都是一样的
DummyFragment.java
package com.example.actionbar;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint("NewApi")
public class DummyFragment extends Fragment{
public static final String ARG_SECTION_NUMBER = "section_number";
//该方法的返回值就是该Fragment显示的View组件
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstantceState)
{
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.START);
//获取创建该Fragment时传入的参数Bundle
Bundle args = getArguments();
//设置TextView显示的文本
switch(args.getInt(ARG_SECTION_NUMBER))
{
case 1:
textView.setText("a");
break;
case 2:
textView.setText("b");
break;
case 3:
textView.setText("c");
break;
}
//textView.setText(args.getInt(ARG_SECTION_NUMBER) + "");
textView.setTextSize(30);
//返回该TextView
return textView;
}
}
代码运行图片
下拉式导航:
ActionBar_DropDownNav.java
package com.example.actionbardropdownnac;
import com.example.actionbardropdownnac.R;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.widget.ArrayAdapter;
@SuppressLint("NewApi")
public class ActionBar_DropDownNav extends Activity implements
ActionBar.OnNavigationListener{
private static final String SELECTED_ITEM = "selected_item";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ActionBar actionBar = getActionBar();
//
actionBar.setDisplayShowHomeEnabled(true);
//
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
//
actionBar.setListNavigationCallbacks(
new ArrayAdapter<String>(ActionBar_DropDownNav.this,
android.R.layout.simple_list_item_1,android.R.id.text1,
new String[]{"第一页","第二页","第三页"}), this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_bar__drop_down_nav, menu);
return true;
}
@Override
public boolean onNavigationItemSelected(int position, long id) {
// TODO Auto-generated method stub
//
//创建一个新的Fragment对象
Fragment fragment = new DummyFragment();
//创建一个Bundle对象,用于向Fragment传入参数
Bundle args = new Bundle();
args.putInt(DummyFragment.ARG_SECTION_NUMBER, position+1);
//向fragment传入参数
fragment.setArguments(args);
//获取FragmentTransaction对象
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
//使用fragment代替该Activity中的container组件
ft.replace(R.id.container, fragment);
//提交事务
ft.commit();
return true;
}
public void onRestoreInstanceState(Bundle savedInstanceState)
{
if(savedInstanceState.containsKey(SELECTED_ITEM))
{
//
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(SELECTED_ITEM));
}
}
public void onSaveInstanceState(Bundle outState)
{
//
outState.putInt(SELECTED_ITEM, getActionBar().getSelectedNavigationIndex());
}
}
代码运行图: