[英]How do I start a new activity when a tab is clicked?
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. 如您所见,我在操作栏下放置了2个标签。 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? 但是我怎么知道选择了“ wich”选项卡? 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: 建议在这里实现一个单独的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.
}
}
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. 您在构造函数中提供给侦听器的类确定了Fragment
的类,如果选择了选项卡,它将打开。
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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.