我正在使用ViewPager和操作栏标签。我添加了四个选项卡,然后填充数组并在开始时删除其中一个,因为它总是显示三个选项卡。点击“询问” 选项卡“主页”选项卡将删除并添加数组“登录”中的选项卡。并且“Ask”移动到第一个位置现在每件事都很好但是当我点击“登录”选项卡时,没有发生并单击“询问”它会移动到连接到“登录”选项卡的片段。 提前谢谢。
public class MainActivity extends SherlockFragmentActivity{
public static ActionBar actionbar;
static Context thisContext;
public static MyAdapter mAdapter;
public static ViewPager mPager;
public static String user_id = "";
Boolean doubleBackToExitPressedOnce = false;
public static ArrayList<Tab> motor_tabs = new ArrayList<ActionBar.Tab>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// construct the tabhost
setContentView(R.layout.activity_main);
thisContext = this;
// setup action bar for tabs
actionbar = getSupportActionBar();
// actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); --> Doing this after adding the tabs now for checking
// actionbar.setDisplayShowTitleEnabled(false);
}
// code for enabling scrolling
mAdapter = new MyAdapter(getSupportFragmentManager());
mAdapter.setActionBar(actionbar);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
//mPager.setOffscreenPageLimit(3);
Tab tab1 = actionbar
.newTab()
.setText("Home")
.setTabListener(
(com.actionbarsherlock.app.ActionBar.TabListener) new TabListener<android.support.v4.app.Fragment>(
this, 0 + "", mPager));
motor_tabs.add(tab1);
actionbar.addTab(tab1);
Tab tab2 = actionbar
.newTab()
.setText("Ask")
.setTabListener(
(com.actionbarsherlock.app.ActionBar.TabListener) new TabListener<android.support.v4.app.Fragment>(
this, 1 + "", mPager));
motor_tabs.add(tab2);
actionbar.addTab(tab2);
Tab tab3 = actionbar
.newTab()
.setText("Login")
.setTabListener(
(com.actionbarsherlock.app.ActionBar.TabListener) new TabListener<android.support.v4.app.Fragment>(
this, 2 + "", mPager));
motor_tabs.add(tab3);
actionbar.addTab(tab3);
Tab tab4 = actionbar
.newTab()
.setText("About")
.setTabListener(
(com.actionbarsherlock.app.ActionBar.TabListener) new TabListener<android.support.v4.app.Fragment>(
this, 3 + "", mPager));
motor_tabs.add(tab4);
actionbar.addTab(tab4);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//actionbar.removeTabAt(2);
//actionbar.getTabAt(3).setTag(2);
MainActivity.actionbar.removeTabAt(2);
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
Log.d("ViewPager", "onPageSelected: " + arg0);
if (arg0 == 1 && actionbar.getTabAt(1).getText().equals("AskRoz")){
MainActivity.actionbar.removeTabAt(0);
MainActivity.actionbar.addTab(MainActivity.motor_tabs.get(2), 1);
Globals.ismotorSports = false;
mPager.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
mPager.setCurrentItem(0);
}
actionbar.getTabAt(arg0).select();
}
});
}
private void enableEmbeddedTabs(Object actionBar) {
try {
Method setHasEmbeddedTabsMethod = actionBar.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
setHasEmbeddedTabsMethod.setAccessible(true);
setHasEmbeddedTabsMethod.invoke(actionBar, true);
} catch (Exception e) {
Log.e("enableEmbeddedTabsException", "Error marking actionbar embedded", e);
}
}
public static class MyAdapter extends FragmentStatePagerAdapter {
ActionBar mActionBar;
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return MainActivity.actionbar.getTabCount();
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case 0:
if(Globals.ismotorSports){
MotorSearchActivity motorsearch = new MotorSearchActivity();
return motorsearch;
}
else{
SearchActivity search = new SearchActivity();
return search;
}
case 1:
if(Globals.ismotorSports){
SearchActivity search = new SearchActivity();
return search;
}
else{
LoginFragment login = LoginFragment.newInstance(false);
//LoginFragment login = new LoginFragment();
return login;
}
case 2:
AboutActivity about = new AboutActivity();
return about;
}
return null;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
public void setActionBar(ActionBar bar) {
mActionBar = bar;
}
}
}
答案 0 :(得分:0)
请勿使用复杂的方法来调用您的 片段 ,尝试使用简单的解决方案,我正在为您编写以下内容,根据需要更改片段名称:
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
// Open FragmentTab1.java
case 0:
FragmentTab1 fragmenttab1 = new FragmentTab1();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
FragmentTab2 fragmenttab2 = new FragmentTab2();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
FragmentTab3 fragmenttab3 = new FragmentTab3();
return fragmenttab3;
}
return null;
}
更多请参阅:
http://stackoverflow.com/questions/19922080/using-separated-intent-for-each-fragmented-tabs-in-android