package com.example.myactionbardemo2;import android.os.Bundle;import android.app.ActionBar;import android.app.Fragment;import android.app.ActionBar.Tab;import android.app.ActionBar.TabListener;import android.app.Activity;import android.app.FragmentTransaction;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;//展示了actionbar的tab模式和list模式。public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar=getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); Tab tab1=actionBar.newTab(); tab1.setText("tab1"); //必须添加监听器,否则会报错 //MyTabListener的设置可以使每个tab使用自己的监听器,显示自己不同的fragment里的内容 tab1.setTabListener(new MyTabListener(MyFragment1.class, this,R.id.container)); Tab tab2=actionBar.newTab(); tab2.setText("tab1"); tab2.setTabListener(new MyTabListener (MyFragment2.class, this,R.id.container)); Tab tab3=actionBar.newTab(); tab3.setText("tab1"); tab3.setTabListener(new MyTabListener (MyFragment3.class, this,R.id.container)); actionBar.addTab(tab1); actionBar.addTab(tab2); actionBar.addTab(tab3); Button button=(Button) findViewById(R.id.but); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(getApplicationContext(), OtherActivity.class); startActivity(intent); } }); } class MyTabListener implements TabListener{ Class fragmentClass; Activity activity; int container; Fragment fragment; public MyTabListener(Class fragmentClass, Activity activity, int container) { super(); this.fragmentClass = fragmentClass; this.activity = activity; this.container = container; } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub if (fragment==null) { //fragment.getName返回的是包名+各自fragment的名字 fragment=Fragment.instantiate(activity, fragmentClass.getName(),null); //把fragment加入到activity中,该fragment可以有界面(在onCreateView生成),也可以返回空无界面 ft.add(container, fragment); }else { ft.attach(fragment); } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub if (fragmentClass!=null) { ft.detach(fragment); } } /*actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); ArrayList list=new ArrayList (); list.add("item1"); list.add("item2"); ArrayAdapter adapter=new ArrayAdapter (getApplicationContext(), android.R.layout.simple_list_item_1, list); actionBar.setListNavigationCallbacks(adapter, new OnNavigationListener() { @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { // TODO Auto-generated method stub switch (itemPosition) { case 0: Toast.makeText(getApplicationContext(), "item1",1000).show(); break; case 1: Toast.makeText(getApplicationContext(), "item2",1000).show(); break; } return true; } });*/ /*actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(false);*/ } }
package com.example.myactionbardemo2;import android.app.ActionBar;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.MenuItem;public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); //当放回父activity时使用 ActionBar actionBar=getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); //其单击处理事件在onOptionsItemSelected里。 actionBar.setHomeButtonEnabled(true); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case android.R.id.home: Intent intent=new Intent(this,MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); break; } return true; }}
当在actionbar的不同tab进行切换时,进行fragment的切换,xml代码如下
注意是在主界面布局中使用的framelayout标签进行fragment的生成切换。
源码链接: