单击选项卡时如何启动新活动?

时间:2014-04-21 10:42:45

标签: java android tabs android-activity android-actionbar

我有这堂课:

public class MainActivity extends ActionBarActivity implements TabListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab TestTab = actionBar.newTab().setText("The test");
        ActionBar.Tab chatTab = actionBar.newTab().setText("Chat");

        TestTab.setTabListener(this);
        chatTab.setTabListener(this);

        actionBar.addTab(TestTab);
        actionBar.addTab(chatTab);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater actionMenue = getMenuInflater();
        actionMenue.inflate(R.menu.main_activity_bar, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.mapIcon) {
            Intent displayTheMap = new Intent(this, Map.class);
            startActivity(displayTheMap);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

    public void showTheMap(View mainView){
        Intent displayTheMap = new Intent(this, Map.class);
        startActivity(displayTheMap);
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

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

    }

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

    }
}

如您所见,我在操作栏下放置了2个标签。现在一切看起来都很好,但是,当单击一个选项卡时,如何执行一对代码?我的意思是,很明显我必须在这里编写代码:

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
}

但我如何知道选择了哪个标签?有人能给我一些线索,因为作为初学者,似乎很难理解。我知道我在这里错过了一小部分。

4 个答案:

答案 0 :(得分:2)

使用以下代码

请输入以下代码。

@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
    int position = tab.getPosition();
    switch(position){
    case 0:
       //code for test
       break;

    case 1:
      //code for chat
      break;
 }
}

答案 1 :(得分:1)

你可以试试这个:

  public class MainActivity extends TabActivity {   
            static TabHost mytabs;

        mytabs = getTabHost();
            mytabs.setOnTabChangedListener(new OnTabChangeListener() {
                @Override
                public void onTabChanged(String arg0) {         
                    Log.i("***Selected Tab", "Im currently in tab with index::" + mytabs.getCurrentTab());
                }       
            }); 

答案 2 :(得分:0)

一般来说,我会在官方文档中解决这个问题。 Here is a link

建议如何实现单独的TabListener

public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /** Constructor used each time a new tab is created.
     * @param activity  The host Activity, used to instantiate the fragment
     * @param tag  The identifier tag for the fragment
     * @param clz  The fragment's Class, used to instantiate the fragment
     */
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}

然后添加如下标签:

Tab tab = actionBar.newTab()
        .setText(R.string.artist)
        .setTabListener(new TabListener<ArtistFragment>(this, "artist", ArtistFragment.class));

在构造函数中为监听器提供的类确定了在选中选项卡时将打开的Fragment的类。

答案 3 :(得分:-2)

试试这段代码

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
 if(tab.getText().equals("The test"){
  //the test tab is selected
 }
else if(tab.getText().equals("Chat"){
  // the chat tab is selected
 }
}