0

无法弄清楚如何从中间标签开始。这是片段活动的测试类..有什么建议吗?我有三个片段,search、neweEntry 和 editEntry。希望它的快速修复。感谢您的输入。

private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private static final int actionCompose = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;


private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;

// Tab titles
private String[] tabs = { "search", "newEntry", "editEntry" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);



    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));

    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page

            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    // android.R.id.content as the container for each fragment

    // setup action bar for tabs
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);

    return super.onCreateOptionsMenu(menu);
}

  @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case actionCompose:
                createNote();
                return true;

            case R.id.actionCompose:
                createNote();
                return true;

       }

        return super.onMenuItemSelected(featureId, item);
    }

  private void createNote() {
        Intent i = new Intent(this, editActivity.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
}
4

2 回答 2

0

在您的 onCreate 方法中,您可以通过执行以下操作来设置默认导航选项卡:

actionBar.setSelectedNavigationItem(your_fragment_position);
于 2014-07-31T17:26:31.137 回答
0

在 Initialization tabs 之后,位置总是从 0 开始,

    tabLayout.getTabAt(fragmentPosition).select();
于 2020-02-26T10:04:50.700 回答