0

我正在使用 ViewPager 和操作栏选项卡。我添加了四个选项卡,然后填充到一个数组中并在开始时删除其中一个,因为它总是显示三个选项卡。单击“询问”选项卡时,“主页”选项卡将从数组“登录”中删除和添加选项卡。并且“询问”移动到第一个位置现在一切都很好但是当我单击“登录”选项卡时没有任何反应并单击“询问”它移动到连接到“登录”选项卡的片段。提前致谢。

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

1 回答 1

0

不要使用复杂的方式来调用你的Fragments,尝试使用简单的解决方案,我在下面为你写,根据需要更改片段的名称:

@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
于 2013-11-13T06:33:15.587 回答