我开始了,这是我的第一篇文章。任何帮助将不胜感激!
我已经成功地使用 ABS 实现了选项卡。我想更改选项卡并通过单击另一个选项卡中的按钮重新加载片段活动。从搜索帖子看来,每个人都使用 TabHost 完成了此操作。没有 TabHost 有没有办法做到这一点?
请看我想做的图表图片:http: //i.imgur.com/wwDBj.png
请参阅下面的 MainActivity 代码:
public class MainActivity extends SherlockFragmentActivity {
private int tabSelected = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
getSupportActionBar().addTab(
getTab(new TabListener<FragmenOne>(this, FragmenOne.class
.getName(), FragmenOne.class), "TabOne"));
getSupportActionBar().addTab(
getTab(new TabListener<FragmenTwo>(this, FragmenTwo.class
.getName(), FragmenTwo.class), "TabTwo"));
getSupportActionBar().addTab(
getTab(new TabListener<FragmenThree>(this, FragmenThree.class
.getName(), FragmenThree.class), "TabThree"));
}
private Tab getTab(TabListener listener, String title) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setTabListener(listener);
tab.setText(title);
return tab;
}
public class TabListener<T extends Fragment> implements
ActionBar.TabListener {
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
public Fragment mFragment;
public TabListener(SherlockFragmentActivity activity, String tag,
Class<T> clz) {
this(activity, tag, clz, null);
}
public TabListener(SherlockFragmentActivity activity, String tag,
Class<T> clz, Bundle args) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
mFragment = mActivity.getSupportFragmentManager()
.findFragmentByTag(tag);
if (mFragment != null && !mFragment.isDetached()) {
FragmentTransaction ft = mActivity.getSupportFragmentManager()
.beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getPosition()==0)
{
tabSelected = 0;
FragmenOne frag = new FragmenOne();
ft.replace(android.R.id.content, frag);
}
else if(tab.getPosition()==1)
{
tabSelected = 1;
FragmenTwo frag = new FragmenTwo();
items = 1;
invalidateOptionsMenu();
ft.replace(android.R.id.content, frag);
}
else if(tab.getPosition()==2)
{
tabSelected = 2;
FragmenThree frag = new FragmenThree();
ft.replace(android.R.id.content, frag);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
items = 0;
invalidateOptionsMenu();
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
public void setCurrentItem() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
FragmenTwo frag = new FragmenTwo();
ft.replace(android.R.id.content, frag);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
这是来自其他活动的代码,例如,我希望单击按钮以加载 tab2
public class TabThree extends Activity implements OnItemSelectedListener {
// Add button
Button btnAdd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabthree);
// add button
btnAdd = (Button) findViewById(R.id.button);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//What to write here??????
}
});
}