问题描述
我下面的教程 https://www.youtube.com/watch?v=VVahIc8yENk 而我得到的错误
我使用的是Android Studio来写这个计划,我已经从API 11试过21,其中没有工作。
公共类Tabtest扩展FragmentActivity实现ActionBar.TabListener {
动作条动作条;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_tab_test);
动作条= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab TAB1 = actionBar.newTab();
tab1.setText(TAB1);
tab1.setTabListener(本);
ActionBar.Tab TAB2 = actionBar.newTab();
tab2.setText(TAB2);
tab2.setTabListener(本);
ActionBar.Tab TAB3 = actionBar.newTab();
tab3.setText(TAB3);
tab3.setTabListener(本);
ActionBar.Tab TAB4 = actionBar.newTab();
tab4.setText(TAB4);
tab4.setTabListener(本);
actionBar.addTab(TAB1);
actionBar.addTab(TAB2);
actionBar.addTab(TAB3);
actionBar.addTab(TAB4);
}
@覆盖
公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction英尺){
Log.d(VIVZ,onTabReselected在+位置+ tab.getPosition()+名+ tab.getText());
}
@覆盖
公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction英尺){
Log.d(VIVZ,onTabReselected在+位置+ tab.getPosition()+名+ tab.getText());
}
@覆盖
公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction英尺){
Log.d(VIVZ,onTabReselected在+位置+ tab.getPosition()+名+ tab.getText());
}
}
我是按照YouTube的Vivz的例子,但在法德precated我不得不另谋出路。而不是添加标签的动作条试的:
修改您的适配器:
公共类CollectionPagerAdapter扩展FragmentStatePagerAdapter {
私有String []标题= {项目1,项目2,3项};
公共CollectionPagerAdapter(FragmentManager FM){
超(FM);
}
@覆盖
公共片段的getItem(int i)以{
开关(ⅰ){
情况下0:
返回新碎裂();
情况1:
返回新FragmentB();
案例2:
返回新FragmentC();
}
返回null;
}
@覆盖
公众诠释getCount将(){
返回titles.length;
}
@覆盖
公共CharSequence的getPageTitle(INT位置){
回到标题[位置]
}
}
而在你想实现的选项卡尝试活性
公共类Tabtest扩展ActionBarActivity {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_tab_test);
ViewPager寻呼机=(ViewPager)findViewById(R.id.your_view_pager);
pager.setAdapter(新CollectionPagerAdapter(getSupportFragmentManager()));
PagerSlidingTabStrip标签=(PagerSlidingTabStrip)findViewById(R.id.tabs);
tabs.setViewPager(寻呼机);
}
现在,如果你想风格你的标签像谷歌Play商店与运动指标下的标签名称和移动,同时用户滚动编译这个库
编译com.astuetz:pagerslidingtabstrip:1.0.1
和修改viewpager布局文件是这样的:
<的LinearLayout
//明显增加宽度和高度等necessery东西
机器人:方向=垂直>
< com.astuetz.PagerSlidingTabStrip
机器人:ID =@ + ID /标签
机器人:layout_width =match_parent
机器人:layout_height =48dip/>
< android.support.v4.view.ViewPager
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /寻呼机
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
< / LinearLayout中>
然后你会收到预期的效果。
希望它可以帮助!
I'm following the tutorial https://www.youtube.com/watch?v=VVahIc8yENk and I'm getting the error
I'm using Android Studio to write this program and I have tried from API 11 to 21 and none of them work.
public class Tabtest extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_test);
actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("tab1");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("tab2");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionBar.newTab();
tab3.setText("tab3");
tab3.setTabListener(this);
ActionBar.Tab tab4=actionBar.newTab();
tab4.setText("tab4");
tab4.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
actionBar.addTab(tab4);
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Log.d("VIVZ", "onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
Log.d("VIVZ", "onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
Log.d("VIVZ", "onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
}
}
I was following Vivz example in youtube but when the method deprecated i had to find another way. Instead of adding tabs to the actionbar try:
Modify your adapter:
public class CollectionPagerAdapter extends FragmentStatePagerAdapter {
private String[] titles = {"Item 1", "Item 2", "Item 3" };
public CollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch(i){
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
}
return null;
}
@Override
public int getCount() {
return titles.length;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
And in the activity that you would like to implement the tabs try
public class Tabtest extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_test);
ViewPager pager = (ViewPager) findViewById(R.id.your_view_pager);
pager.setAdapter(new CollectionPagerAdapter(getSupportFragmentManager()));
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
}
Now if you would like to style your tabs like Google Play store with a moving indicator under the tab name and move while the user scrolls compile this library
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
And modify your viewpager layout file like this:
<LinearLayout
//obviously add width and height and other necessery stuff
android:orientation="vertical">
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip" />
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
And then you are going to have the desired effect.
Hope it helps!!!
这篇关于actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)去precated的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!