質問

I have this class:

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

    }
}

As you can see I have placed 2 tabs under the action bar. Now everything looks just fine, but, how do I execute a pair of code, when a tab is clicked? I mean, it is clear that I have to write my code here:

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

But how do I know wich tab is selected? Can someone give me a clue, because as a beginner, it is seems hard to understand. I know that I'm missing a small part here.

役に立ちましたか?

解決

Use the below code

Please us the below code.

@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;
 }
}

他のヒント

you can try this :

  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());
                }       
            }); 

Generally I would solve this like in the official docs. Here is a link

What is suggested there is to implement a separate TabListener like this:

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.
    }
}

And then add tabs like this:

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

The class you give the listener in the constructor determines the class of the Fragment which will be opened if the tab is selected.

try this code

@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
 }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top