gpt4 book ai didi

java - Android Tab Layout如何从第二个tab开始

转载 作者:太空宇宙 更新时间:2023-11-03 12:54:09 25 4
gpt4 key购买 nike

嗨,我正在关注这个图 http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

我希望应用程序以“游戏”选项卡开始,而不是“评分最高”选项卡!你能帮我吗:)

这是我的代码

package com.scorebeyond.android.tabswithswipeexample;

import com.scorebeyond.android.tabswithswipeexample.adapter.TabsPagerAdapter;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements TabListener {

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

// Tab Titles
private String[] tabs = { "Top Rated", "Games", "Movies" };

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

// Initialize
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setCurrentItem(-1);
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));

}

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) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}
});

}

@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, FragmentTransaction ft) {
// TODO Auto-generated method stub

}

@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) {
// TODO Auto-generated method stub

}

}

非常感谢

最佳答案

您应该使用(在您的 onCreate 方法的末尾):

action bar selectTab method使用您之前添加的选项卡作为参数

使用Action Bar setSelectedNavigationItem 按位置选择标签项

actionBar.setSelectedNavigationItem(1);

关于java - Android Tab Layout如何从第二个tab开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24041293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com