//我在我的应用程序中使用操作栏选项卡,当我按下选项卡时,我想在选项卡中从一个活动切换到另一个活动,如何在选项卡中调用活动。我想在操作栏标签中显示任何活动。
public class MainActivity extends Activity implements TabListener {
// Refresh menu item
private MenuItem action_search;
Tab tab1, tab2, tab3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//creating tabs
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//adding tabs to actionbar
tab1 = actionBar.newTab();
tab1.setText("camera");
tab1.setTabListener(this);
actionBar.addTab(tab1);
tab2 = actionBar.newTab();
tab2.setText("contacts");
tab2.setTabListener(this);
actionBar.addTab(tab2);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
switch (tab.getPosition()) {
case 0:
Intent i = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(i);
break;
case 1:
Intent i = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(i);
break;
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
MainActivity.java
package com.example.moviesswipe;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Menu;
@SuppressWarnings("unused")
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "English", "Tamil", "Hindi" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
TabsPagerAdapter.java
package com.example.moviesswipe;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new English();
case 1:
return new Tamil();
case 2:
return new Hindi();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
这里我放置了三个标签,每个标签我创建了三个活动。 English.java,Tamil.java和Hindi.java
样本一:
package com.example.moviesswipe;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressWarnings("unused")
public class English extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.english, container, false);
return rootView;
}
}
对于每个课程,您需要创建一个布局:
无需在清单中输入这些类,这些都是片段。
尝试像这样的家伙:)快乐的编码:)
说是否有任何疑问。