ActionBar Tab

申屠涛
2023-12-01

标签:

为了使用ActionBar实现Tab导航,按如下步骤进行即可。

1.调用ActionBar的setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)方法设置使用Tab导航方式。

2.调用ActionBar的addTab方法添加多个Tab标签,并为每个Tab标签添加事件监听。

Fragment相当于activity片段,通常使用单独的activity组合多个fragment,这样既可以在一个activity创建多个用户界面,又可以让多个activity复用同一个fragment,总之,fragment相当于activity的模块化区域。

fragment有自己的生命周期,可以接受处理属于它自身的事件,并允许activity运行期间动态的添加删除fragment。

fragment允许定义自己的布局,也可以通过生命周期回调方法定义自己的行为,这一点非常像activity。

实例的布局文件:

StandardWidget\res\layout\tabnavigation.xml

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" 
      android:id="@+id/container"
      > </LinearLayout>
activity代码:

  package com.example.standardwidget;


   import android.os.Bundle;
   import android.support.v4.app.Fragment;
   import android.support.v4.app.FragmentTransaction;
   import android.support.v7.app.ActionBar;
   import android.support.v7.app.ActionBar.TabListener;
   import android.support.v7.app.ActionBarActivity;
  import android.support.v7.app.ActionBar.Tab;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.widget.Toast;

  public class MyActionBarActivity extends ActionBarActivity implements
          TabListener {

      private static final String SELECTED_ITEM = "selected_item";

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub

          super.onCreate(savedInstanceState);
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          setContentView(R.layout.tabnavigation);
          // 设置是否显示app的图标
          getSupportActionBar().setDisplayShowHomeEnabled(true);
          // 设置是否将app图标转变成成可点击的按钮
          getSupportActionBar().setHomeButtonEnabled(true);

          getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
          getSupportActionBar().addTab(
                  getSupportActionBar().newTab().setText("第一页")
                          .setTabListener(this));

          getSupportActionBar().addTab(
                  getSupportActionBar().newTab().setText("第二页")
                          .setTabListener(this));

          getSupportActionBar().addTab(
                  getSupportActionBar().newTab().setIcon(R.drawable.p13)
                          .setText("第三页").setTabListener(this));


      }

      @Override
      protected void onRestoreInstanceState(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onRestoreInstanceState(savedInstanceState);

          if (savedInstanceState.containsKey(SELECTED_ITEM)) {
          //选中前面保存的索引对应的fragment页
              getSupportActionBar().setSelectedNavigationItem(
                      savedInstanceState.getInt(SELECTED_ITEM));
          }

      }

     @Override
      protected void onSaveInstanceState(Bundle outState) {
          // TODO Auto-generated method stub
          super.onSaveInstanceState(outState);
          //将当前选中的fragment页的索引保存到bundle中
          outState.putInt(SELECTED_ITEM, getSupportActionBar()
                  .getSelectedNavigationIndex());
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // TODO Auto-generated method stub
          // 为ActionBar扩展菜单项
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.main_activity_actions, menu);
          return super.onCreateOptionsMenu(menu);

      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // TODO Auto-generated method stub
          // 处理动作按钮的点击事件
          switch (item.getItemId()) {
          case R.id.action_search:
              Toast.makeText(this, "openSearch", 2000).show();
              return true;
          case R.id.action_settings:

              Toast.makeText(this, "openSettings", 2000).show();
              return true;

          case android.R.id.home:
              Toast.makeText(this, "App Icon", 2000).show();
              return true;
          default:
              return super.onOptionsItemSelected(item);
         }
     }

     @Override
     public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
         // TODO Auto-generated method stub

     }

     @Override
     public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
         // TODO Auto-generated method stub
         Fragment fragment = new DummyFragment();
         //用于向fragment传入参数
         Bundle argsBundle = new Bundle();
         argsBundle.putInt(DummyFragment.ARG_SELECTION_NUMBER, arg0.getPosition()+1);
         //向fragment传入参数
         fragment.setArguments(argsBundle);
         //获取fragmenttransaction对象
         FragmentTransaction fTransaction = getSupportFragmentManager().beginTransaction();
         //使用fragment代替该activity中的container组件
         fTransaction.replace(R.id.container, fragment);
         fTransaction.commit();
     }

     @Override
     public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
         // TODO Auto-generated method stub

     }



 }
fragment代码:

  package com.example.standardwidget;

  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.Gravity;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
 import android.widget.TextView;

 public class DummyFragment extends Fragment {

     protected static final String ARG_SELECTION_NUMBER ="selection_number";
     //该方法的返回值就是该fragment显示的view组件
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
         // TODO Auto-generated method stub
         TextView textView = new TextView(getActivity());
         textView.setGravity(Gravity.START);
         //获取创建该fragment时传入的参数bundle
         Bundle args = getArguments();
         //设置TextView显示的文本
         textView.setText(args.getInt(ARG_SELECTION_NUMBER)+"");
         textView.setTextSize(30);


         return textView;
     }
 }
 类似资料:

相关阅读

相关文章

相关问答